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, 2010 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 "core/html/HTMLButtonElement.h"
28
29 #include "HTMLNames.h"
30 #include "core/dom/Attribute.h"
31 #include "core/events/KeyboardEvent.h"
32 #include "core/events/ThreadLocalEventNames.h"
33 #include "core/html/FormDataList.h"
34 #include "core/html/HTMLFormElement.h"
35 #include "core/rendering/RenderButton.h"
36 #include "wtf/StdLibExtras.h"
37
38 namespace WebCore {
39
40 using namespace HTMLNames;
41
HTMLButtonElement(Document & document,HTMLFormElement * form)42 inline HTMLButtonElement::HTMLButtonElement(Document& document, HTMLFormElement* form)
43 : HTMLFormControlElement(buttonTag, document, form)
44 , m_type(SUBMIT)
45 , m_isActivatedSubmit(false)
46 {
47 ScriptWrappable::init(this);
48 }
49
create(Document & document,HTMLFormElement * form)50 PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(Document& document, HTMLFormElement* form)
51 {
52 return adoptRef(new HTMLButtonElement(document, form));
53 }
54
setType(const AtomicString & type)55 void HTMLButtonElement::setType(const AtomicString& type)
56 {
57 setAttribute(typeAttr, type);
58 }
59
createRenderer(RenderStyle *)60 RenderObject* HTMLButtonElement::createRenderer(RenderStyle*)
61 {
62 return new RenderButton(this);
63 }
64
formControlType() const65 const AtomicString& HTMLButtonElement::formControlType() const
66 {
67 switch (m_type) {
68 case SUBMIT: {
69 DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit", AtomicString::ConstructFromLiteral));
70 return submit;
71 }
72 case BUTTON: {
73 DEFINE_STATIC_LOCAL(const AtomicString, button, ("button", AtomicString::ConstructFromLiteral));
74 return button;
75 }
76 case RESET: {
77 DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset", AtomicString::ConstructFromLiteral));
78 return reset;
79 }
80 }
81
82 ASSERT_NOT_REACHED();
83 return emptyAtom;
84 }
85
isPresentationAttribute(const QualifiedName & name) const86 bool HTMLButtonElement::isPresentationAttribute(const QualifiedName& name) const
87 {
88 if (name == alignAttr) {
89 // Don't map 'align' attribute. This matches what Firefox and IE do, but not Opera.
90 // See http://bugs.webkit.org/show_bug.cgi?id=12071
91 return false;
92 }
93
94 return HTMLFormControlElement::isPresentationAttribute(name);
95 }
96
parseAttribute(const QualifiedName & name,const AtomicString & value)97 void HTMLButtonElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
98 {
99 if (name == typeAttr) {
100 if (equalIgnoringCase(value, "reset"))
101 m_type = RESET;
102 else if (equalIgnoringCase(value, "button"))
103 m_type = BUTTON;
104 else
105 m_type = SUBMIT;
106 setNeedsWillValidateCheck();
107 } else
108 HTMLFormControlElement::parseAttribute(name, value);
109 }
110
defaultEventHandler(Event * event)111 void HTMLButtonElement::defaultEventHandler(Event* event)
112 {
113 if (event->type() == EventTypeNames::DOMActivate && !isDisabledFormControl()) {
114 if (form() && m_type == SUBMIT) {
115 m_isActivatedSubmit = true;
116 form()->prepareForSubmission(event);
117 event->setDefaultHandled();
118 m_isActivatedSubmit = false; // Do this in case submission was canceled.
119 }
120 if (form() && m_type == RESET) {
121 form()->reset();
122 event->setDefaultHandled();
123 }
124 }
125
126 if (event->isKeyboardEvent()) {
127 if (event->type() == EventTypeNames::keydown && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
128 setActive(true);
129 // No setDefaultHandled() - IE dispatches a keypress in this case.
130 return;
131 }
132 if (event->type() == EventTypeNames::keypress) {
133 switch (toKeyboardEvent(event)->charCode()) {
134 case '\r':
135 dispatchSimulatedClick(event);
136 event->setDefaultHandled();
137 return;
138 case ' ':
139 // Prevent scrolling down the page.
140 event->setDefaultHandled();
141 return;
142 }
143 }
144 if (event->type() == EventTypeNames::keyup && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
145 if (active())
146 dispatchSimulatedClick(event);
147 event->setDefaultHandled();
148 return;
149 }
150 }
151
152 HTMLFormControlElement::defaultEventHandler(event);
153 }
154
willRespondToMouseClickEvents()155 bool HTMLButtonElement::willRespondToMouseClickEvents()
156 {
157 if (!isDisabledFormControl() && form() && (m_type == SUBMIT || m_type == RESET))
158 return true;
159 return HTMLFormControlElement::willRespondToMouseClickEvents();
160 }
161
canBeSuccessfulSubmitButton() const162 bool HTMLButtonElement::canBeSuccessfulSubmitButton() const
163 {
164 return m_type == SUBMIT;
165 }
166
isActivatedSubmit() const167 bool HTMLButtonElement::isActivatedSubmit() const
168 {
169 return m_isActivatedSubmit;
170 }
171
setActivatedSubmit(bool flag)172 void HTMLButtonElement::setActivatedSubmit(bool flag)
173 {
174 m_isActivatedSubmit = flag;
175 }
176
appendFormData(FormDataList & formData,bool)177 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
178 {
179 if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit)
180 return false;
181 formData.appendData(name(), value());
182 return true;
183 }
184
accessKeyAction(bool sendMouseEvents)185 void HTMLButtonElement::accessKeyAction(bool sendMouseEvents)
186 {
187 focus();
188
189 dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
190 }
191
isURLAttribute(const Attribute & attribute) const192 bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const
193 {
194 return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute);
195 }
196
value() const197 const AtomicString& HTMLButtonElement::value() const
198 {
199 return getAttribute(valueAttr);
200 }
201
recalcWillValidate() const202 bool HTMLButtonElement::recalcWillValidate() const
203 {
204 return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate();
205 }
206
isInteractiveContent() const207 bool HTMLButtonElement::isInteractiveContent() const
208 {
209 return true;
210 }
211
212 } // namespace
213