• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "EventNames.h"
30 #include "FormDataList.h"
31 #include "HTMLFormElement.h"
32 #include "HTMLNames.h"
33 #include "ScriptEventListener.h"
34 #include "KeyboardEvent.h"
35 #include "MappedAttribute.h"
36 #include "RenderButton.h"
37 #include <wtf/StdLibExtras.h>
38 
39 namespace WebCore {
40 
41 using namespace HTMLNames;
42 
HTMLButtonElement(const QualifiedName & tagName,Document * doc,HTMLFormElement * form)43 HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* form)
44     : HTMLFormControlElement(tagName, doc, form)
45     , m_type(SUBMIT)
46     , m_activeSubmit(false)
47 {
48     ASSERT(hasTagName(buttonTag));
49 }
50 
~HTMLButtonElement()51 HTMLButtonElement::~HTMLButtonElement()
52 {
53 }
54 
createRenderer(RenderArena * arena,RenderStyle *)55 RenderObject* HTMLButtonElement::createRenderer(RenderArena* arena, RenderStyle*)
56 {
57     return new (arena) RenderButton(this);
58 }
59 
formControlType() const60 const AtomicString& HTMLButtonElement::formControlType() const
61 {
62     switch (m_type) {
63         case SUBMIT: {
64             DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit"));
65             return submit;
66         }
67         case BUTTON: {
68             DEFINE_STATIC_LOCAL(const AtomicString, button, ("button"));
69             return button;
70         }
71         case RESET: {
72             DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset"));
73             return reset;
74         }
75     }
76 
77     ASSERT_NOT_REACHED();
78     return emptyAtom;
79 }
80 
parseMappedAttribute(MappedAttribute * attr)81 void HTMLButtonElement::parseMappedAttribute(MappedAttribute* attr)
82 {
83     if (attr->name() == typeAttr) {
84         if (equalIgnoringCase(attr->value(), "reset"))
85             m_type = RESET;
86         else if (equalIgnoringCase(attr->value(), "button"))
87             m_type = BUTTON;
88         else
89             m_type = SUBMIT;
90     } else if (attr->name() == alignAttr) {
91         // Don't map 'align' attribute.  This matches what Firefox and IE do, but not Opera.
92         // See http://bugs.webkit.org/show_bug.cgi?id=12071
93     } else if (attr->name() == onfocusAttr) {
94         setAttributeEventListener(eventNames().focusEvent, createAttributeEventListener(this, attr));
95     } else if (attr->name() == onblurAttr) {
96         setAttributeEventListener(eventNames().blurEvent, createAttributeEventListener(this, attr));
97     } else
98         HTMLFormControlElement::parseMappedAttribute(attr);
99 }
100 
defaultEventHandler(Event * evt)101 void HTMLButtonElement::defaultEventHandler(Event* evt)
102 {
103     if (evt->type() == eventNames().DOMActivateEvent && !disabled()) {
104         if (form() && m_type == SUBMIT) {
105             m_activeSubmit = true;
106             form()->prepareSubmit(evt);
107             m_activeSubmit = false; // in case we were canceled
108         }
109         if (form() && m_type == RESET)
110             form()->reset();
111     }
112 
113     if (evt->isKeyboardEvent()) {
114         if (evt->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(evt)->keyIdentifier() == "U+0020") {
115             setActive(true, true);
116             // No setDefaultHandled() - IE dispatches a keypress in this case.
117             return;
118         }
119         if (evt->type() == eventNames().keypressEvent) {
120             switch (static_cast<KeyboardEvent*>(evt)->charCode()) {
121                 case '\r':
122                     dispatchSimulatedClick(evt);
123                     evt->setDefaultHandled();
124                     return;
125                 case ' ':
126                     // Prevent scrolling down the page.
127                     evt->setDefaultHandled();
128                     return;
129                 default:
130                     break;
131             }
132         }
133         if (evt->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(evt)->keyIdentifier() == "U+0020") {
134             if (active())
135                 dispatchSimulatedClick(evt);
136             evt->setDefaultHandled();
137             return;
138         }
139     }
140 
141     HTMLFormControlElement::defaultEventHandler(evt);
142 }
143 
isSuccessfulSubmitButton() const144 bool HTMLButtonElement::isSuccessfulSubmitButton() const
145 {
146     // HTML spec says that buttons must have names to be considered successful.
147     // However, other browsers do not impose this constraint.
148     return m_type == SUBMIT && !disabled();
149 }
150 
isActivatedSubmit() const151 bool HTMLButtonElement::isActivatedSubmit() const
152 {
153     return m_activeSubmit;
154 }
155 
setActivatedSubmit(bool flag)156 void HTMLButtonElement::setActivatedSubmit(bool flag)
157 {
158     m_activeSubmit = flag;
159 }
160 
appendFormData(FormDataList & formData,bool)161 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
162 {
163     if (m_type != SUBMIT || name().isEmpty() || !m_activeSubmit)
164         return false;
165     formData.appendData(name(), value());
166     return true;
167 }
168 
accessKeyAction(bool sendToAnyElement)169 void HTMLButtonElement::accessKeyAction(bool sendToAnyElement)
170 {
171     focus();
172     // send the mouse button events iff the caller specified sendToAnyElement
173     dispatchSimulatedClick(0, sendToAnyElement);
174 }
175 
accessKey() const176 String HTMLButtonElement::accessKey() const
177 {
178     return getAttribute(accesskeyAttr);
179 }
180 
setAccessKey(const String & value)181 void HTMLButtonElement::setAccessKey(const String &value)
182 {
183     setAttribute(accesskeyAttr, value);
184 }
185 
value() const186 String HTMLButtonElement::value() const
187 {
188     return getAttribute(valueAttr);
189 }
190 
setValue(const String & value)191 void HTMLButtonElement::setValue(const String &value)
192 {
193     setAttribute(valueAttr, value);
194 }
195 
196 } // namespace
197