WebCore/html/HTMLButtonElement.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
       
     3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
       
     4  *           (C) 2001 Dirk Mueller (mueller@kde.org)
       
     5  * Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
       
     6  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
       
     7  * Copyright (C) 2007 Samuel Weinig (sam@webkit.org)
       
     8  *
       
     9  * This library is free software; you can redistribute it and/or
       
    10  * modify it under the terms of the GNU Library General Public
       
    11  * License as published by the Free Software Foundation; either
       
    12  * version 2 of the License, or (at your option) any later version.
       
    13  *
       
    14  * This library is distributed in the hope that it will be useful,
       
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    17  * Library General Public License for more details.
       
    18  *
       
    19  * You should have received a copy of the GNU Library General Public License
       
    20  * along with this library; see the file COPYING.LIB.  If not, write to
       
    21  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    22  * Boston, MA 02110-1301, USA.
       
    23  *
       
    24  */
       
    25 
       
    26 #include "config.h"
       
    27 #include "HTMLButtonElement.h"
       
    28 
       
    29 #include "Attribute.h"
       
    30 #include "EventNames.h"
       
    31 #include "FormDataList.h"
       
    32 #include "HTMLFormElement.h"
       
    33 #include "HTMLNames.h"
       
    34 #include "KeyboardEvent.h"
       
    35 #include "RenderButton.h"
       
    36 #include "ScriptEventListener.h"
       
    37 #include <wtf/StdLibExtras.h>
       
    38 
       
    39 namespace WebCore {
       
    40 
       
    41 using namespace HTMLNames;
       
    42 
       
    43 inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
       
    44     : HTMLFormControlElement(tagName, document, form)
       
    45     , m_type(SUBMIT)
       
    46     , m_activeSubmit(false)
       
    47 {
       
    48     ASSERT(hasTagName(buttonTag));
       
    49 }
       
    50 
       
    51 PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
       
    52 {
       
    53     return adoptRef(new HTMLButtonElement(tagName, document, form));
       
    54 }
       
    55 
       
    56 RenderObject* HTMLButtonElement::createRenderer(RenderArena* arena, RenderStyle*)
       
    57 {
       
    58     return new (arena) RenderButton(this);
       
    59 }
       
    60 
       
    61 const AtomicString& HTMLButtonElement::formControlType() const
       
    62 {
       
    63     switch (m_type) {
       
    64         case SUBMIT: {
       
    65             DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit"));
       
    66             return submit;
       
    67         }
       
    68         case BUTTON: {
       
    69             DEFINE_STATIC_LOCAL(const AtomicString, button, ("button"));
       
    70             return button;
       
    71         }
       
    72         case RESET: {
       
    73             DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset"));
       
    74             return reset;
       
    75         }
       
    76     }
       
    77 
       
    78     ASSERT_NOT_REACHED();
       
    79     return emptyAtom;
       
    80 }
       
    81 
       
    82 void HTMLButtonElement::parseMappedAttribute(Attribute* attr)
       
    83 {
       
    84     if (attr->name() == typeAttr) {
       
    85         if (equalIgnoringCase(attr->value(), "reset"))
       
    86             m_type = RESET;
       
    87         else if (equalIgnoringCase(attr->value(), "button"))
       
    88             m_type = BUTTON;
       
    89         else
       
    90             m_type = SUBMIT;
       
    91     } else if (attr->name() == alignAttr) {
       
    92         // Don't map 'align' attribute.  This matches what Firefox and IE do, but not Opera.
       
    93         // See http://bugs.webkit.org/show_bug.cgi?id=12071
       
    94     } else
       
    95         HTMLFormControlElement::parseMappedAttribute(attr);
       
    96 }
       
    97 
       
    98 void HTMLButtonElement::defaultEventHandler(Event* evt)
       
    99 {
       
   100     if (evt->type() == eventNames().DOMActivateEvent && !disabled()) {
       
   101         if (form() && m_type == SUBMIT) {
       
   102             m_activeSubmit = true;
       
   103             form()->prepareSubmit(evt);
       
   104             m_activeSubmit = false; // in case we were canceled
       
   105         }
       
   106         if (form() && m_type == RESET)
       
   107             form()->reset();
       
   108     }
       
   109 
       
   110     if (evt->isKeyboardEvent()) {
       
   111         if (evt->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(evt)->keyIdentifier() == "U+0020") {
       
   112             setActive(true, true);
       
   113             // No setDefaultHandled() - IE dispatches a keypress in this case.
       
   114             return;
       
   115         }
       
   116         if (evt->type() == eventNames().keypressEvent) {
       
   117             switch (static_cast<KeyboardEvent*>(evt)->charCode()) {
       
   118                 case '\r':
       
   119                     dispatchSimulatedClick(evt);
       
   120                     evt->setDefaultHandled();
       
   121                     return;
       
   122                 case ' ':
       
   123                     // Prevent scrolling down the page.
       
   124                     evt->setDefaultHandled();
       
   125                     return;
       
   126                 default:
       
   127                     break;
       
   128             }
       
   129         }
       
   130         if (evt->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(evt)->keyIdentifier() == "U+0020") {
       
   131             if (active())
       
   132                 dispatchSimulatedClick(evt);
       
   133             evt->setDefaultHandled();
       
   134             return;
       
   135         }
       
   136     }
       
   137 
       
   138     HTMLFormControlElement::defaultEventHandler(evt);
       
   139 }
       
   140 
       
   141 bool HTMLButtonElement::isSuccessfulSubmitButton() const
       
   142 {
       
   143     // HTML spec says that buttons must have names to be considered successful.
       
   144     // However, other browsers do not impose this constraint.
       
   145     return m_type == SUBMIT && !disabled();
       
   146 }
       
   147 
       
   148 bool HTMLButtonElement::isActivatedSubmit() const
       
   149 {
       
   150     return m_activeSubmit;
       
   151 }
       
   152 
       
   153 void HTMLButtonElement::setActivatedSubmit(bool flag)
       
   154 {
       
   155     m_activeSubmit = flag;
       
   156 }
       
   157 
       
   158 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
       
   159 {
       
   160     if (m_type != SUBMIT || name().isEmpty() || !m_activeSubmit)
       
   161         return false;
       
   162     formData.appendData(name(), value());
       
   163     return true;
       
   164 }
       
   165 
       
   166 void HTMLButtonElement::accessKeyAction(bool sendToAnyElement)
       
   167 {
       
   168     focus();
       
   169     // send the mouse button events iff the caller specified sendToAnyElement
       
   170     dispatchSimulatedClick(0, sendToAnyElement);
       
   171 }
       
   172 
       
   173 String HTMLButtonElement::value() const
       
   174 {
       
   175     return getAttribute(valueAttr);
       
   176 }
       
   177 
       
   178 } // namespace