• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "JSHTMLInputElement.h"
28 
29 #include "Document.h"
30 #include "HTMLInputElement.h"
31 #include "Settings.h"
32 
33 using namespace JSC;
34 
35 namespace WebCore {
36 
needsGmailQuirk(HTMLInputElement * input)37 static bool needsGmailQuirk(HTMLInputElement* input)
38 {
39     Document* document = input->document();
40 
41     const KURL& url = document->url();
42     if (url.host() != "mail.google.com")
43         return false;
44 
45     // As with other site-specific quirks, allow website developers to turn this off.
46     // In theory, this allows website developers to check if their fixes are effective.
47     Settings* settings = document->settings();
48     if (!settings)
49         return false;
50     if (!settings->needsSiteSpecificQuirks())
51         return false;
52 
53     return true;
54 }
55 
type(ExecState * exec) const56 JSValue JSHTMLInputElement::type(ExecState* exec) const
57 {
58     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
59     const AtomicString& type = input->type();
60 
61     DEFINE_STATIC_LOCAL(const AtomicString, url, ("url"));
62     DEFINE_STATIC_LOCAL(const AtomicString, text, ("text"));
63 
64     if (type == url && needsGmailQuirk(input))
65         return jsString(exec, text);
66     return jsString(exec, type);
67 }
68 
selectionStart(ExecState * exec) const69 JSValue JSHTMLInputElement::selectionStart(ExecState* exec) const
70 {
71     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
72     if (!input->canHaveSelection())
73         return throwError(exec, TypeError);
74 
75     return jsNumber(exec, input->selectionStart());
76 }
77 
setSelectionStart(ExecState * exec,JSValue value)78 void JSHTMLInputElement::setSelectionStart(ExecState* exec, JSValue value)
79 {
80     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
81     if (!input->canHaveSelection())
82         throwError(exec, TypeError);
83 
84     input->setSelectionStart(value.toInt32(exec));
85 }
86 
selectionEnd(ExecState * exec) const87 JSValue JSHTMLInputElement::selectionEnd(ExecState* exec) const
88 {
89     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
90     if (!input->canHaveSelection())
91         return throwError(exec, TypeError);
92 
93     return jsNumber(exec, input->selectionEnd());
94 }
95 
setSelectionEnd(ExecState * exec,JSValue value)96 void JSHTMLInputElement::setSelectionEnd(ExecState* exec, JSValue value)
97 {
98     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
99     if (!input->canHaveSelection())
100         throwError(exec, TypeError);
101 
102     input->setSelectionEnd(value.toInt32(exec));
103 }
104 
setSelectionRange(ExecState * exec,const ArgList & args)105 JSValue JSHTMLInputElement::setSelectionRange(ExecState* exec, const ArgList& args)
106 {
107     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
108     if (!input->canHaveSelection())
109         return throwError(exec, TypeError);
110 
111     int start = args.at(0).toInt32(exec);
112     int end = args.at(1).toInt32(exec);
113 
114     input->setSelectionRange(start, end);
115     return jsUndefined();
116 }
117 
118 } // namespace WebCore
119