WebCore/bindings/js/JSHTMLInputElementCustom.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 #include <runtime/Error.h>
       
    33 
       
    34 using namespace JSC;
       
    35 
       
    36 namespace WebCore {
       
    37 
       
    38 static bool needsGmailQuirk(HTMLInputElement* input)
       
    39 {
       
    40     Document* document = input->document();
       
    41 
       
    42     const KURL& url = document->url();
       
    43     if (url.host() != "mail.google.com")
       
    44         return false;
       
    45 
       
    46     // As with other site-specific quirks, allow website developers to turn this off.
       
    47     // In theory, this allows website developers to check if their fixes are effective.
       
    48     Settings* settings = document->settings();
       
    49     if (!settings)
       
    50         return false;
       
    51     if (!settings->needsSiteSpecificQuirks())
       
    52         return false;
       
    53 
       
    54     return true;
       
    55 }
       
    56 
       
    57 JSValue JSHTMLInputElement::type(ExecState* exec) const
       
    58 {
       
    59     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
    60     const AtomicString& type = input->type();
       
    61 
       
    62     DEFINE_STATIC_LOCAL(const AtomicString, url, ("url"));
       
    63     DEFINE_STATIC_LOCAL(const AtomicString, text, ("text"));
       
    64 
       
    65     if (type == url && needsGmailQuirk(input))
       
    66         return jsString(exec, text);
       
    67     return jsString(exec, type);
       
    68 }
       
    69 
       
    70 JSValue JSHTMLInputElement::selectionStart(ExecState* exec) const
       
    71 {
       
    72     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
    73     if (!input->canHaveSelection())
       
    74         return throwTypeError(exec);
       
    75 
       
    76     return jsNumber(exec, input->selectionStart());
       
    77 }
       
    78 
       
    79 void JSHTMLInputElement::setSelectionStart(ExecState* exec, JSValue value)
       
    80 {
       
    81     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
    82     if (!input->canHaveSelection())
       
    83         throwTypeError(exec);
       
    84 
       
    85     input->setSelectionStart(value.toInt32(exec));
       
    86 }
       
    87 
       
    88 JSValue JSHTMLInputElement::selectionEnd(ExecState* exec) const
       
    89 {
       
    90     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
    91     if (!input->canHaveSelection())
       
    92         return throwTypeError(exec);
       
    93 
       
    94     return jsNumber(exec, input->selectionEnd());
       
    95 }
       
    96 
       
    97 void JSHTMLInputElement::setSelectionEnd(ExecState* exec, JSValue value)
       
    98 {
       
    99     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
   100     if (!input->canHaveSelection())
       
   101         throwTypeError(exec);
       
   102 
       
   103     input->setSelectionEnd(value.toInt32(exec));
       
   104 }
       
   105 
       
   106 JSValue JSHTMLInputElement::setSelectionRange(ExecState* exec)
       
   107 {
       
   108     HTMLInputElement* input = static_cast<HTMLInputElement*>(impl());
       
   109     if (!input->canHaveSelection())
       
   110         return throwTypeError(exec);
       
   111 
       
   112     int start = exec->argument(0).toInt32(exec);
       
   113     int end = exec->argument(1).toInt32(exec);
       
   114 
       
   115     input->setSelectionRange(start, end);
       
   116     return jsUndefined();
       
   117 }
       
   118 
       
   119 } // namespace WebCore