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-2018 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 "DOMFormData.h"
30#include "EventNames.h"
31#include "HTMLFormElement.h"
32#include "HTMLNames.h"
33#include "KeyboardEvent.h"
34#include "RenderButton.h"
35#include <wtf/IsoMallocInlines.h>
36#include <wtf/SetForScope.h>
37#include <wtf/StdLibExtras.h>
38
39namespace WebCore {
40
41WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLButtonElement);
42
43using namespace HTMLNames;
44
45inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
46 : HTMLFormControlElement(tagName, document, form)
47 , m_type(SUBMIT)
48 , m_isActivatedSubmit(false)
49{
50 ASSERT(hasTagName(buttonTag));
51}
52
53Ref<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
54{
55 return adoptRef(*new HTMLButtonElement(tagName, document, form));
56}
57
58void HTMLButtonElement::setType(const AtomicString& type)
59{
60 setAttributeWithoutSynchronization(typeAttr, type);
61}
62
63RenderPtr<RenderElement> HTMLButtonElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
64{
65 return createRenderer<RenderButton>(*this, WTFMove(style));
66}
67
68const AtomicString& HTMLButtonElement::formControlType() const
69{
70 switch (m_type) {
71 case SUBMIT: {
72 static NeverDestroyed<const AtomicString> submit("submit", AtomicString::ConstructFromLiteral);
73 return submit;
74 }
75 case BUTTON: {
76 static NeverDestroyed<const AtomicString> button("button", AtomicString::ConstructFromLiteral);
77 return button;
78 }
79 case RESET: {
80 static NeverDestroyed<const AtomicString> reset("reset", AtomicString::ConstructFromLiteral);
81 return reset;
82 }
83 }
84
85 ASSERT_NOT_REACHED();
86 return emptyAtom();
87}
88
89bool HTMLButtonElement::isPresentationAttribute(const QualifiedName& name) const
90{
91 if (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 return false;
95 }
96
97 return HTMLFormControlElement::isPresentationAttribute(name);
98}
99
100void HTMLButtonElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
101{
102 if (name == typeAttr) {
103 Type oldType = m_type;
104 if (equalLettersIgnoringASCIICase(value, "reset"))
105 m_type = RESET;
106 else if (equalLettersIgnoringASCIICase(value, "button"))
107 m_type = BUTTON;
108 else
109 m_type = SUBMIT;
110 if (oldType != m_type) {
111 setNeedsWillValidateCheck();
112 if (form() && (oldType == SUBMIT || m_type == SUBMIT))
113 form()->resetDefaultButton();
114 }
115 } else
116 HTMLFormControlElement::parseAttribute(name, value);
117}
118
119void HTMLButtonElement::defaultEventHandler(Event& event)
120{
121 if (event.type() == eventNames().DOMActivateEvent && !isDisabledFormControl()) {
122 RefPtr<HTMLFormElement> protectedForm(form());
123
124 if (protectedForm) {
125 // Update layout before processing form actions in case the style changes
126 // the Form or button relationships.
127 document().updateLayoutIgnorePendingStylesheets();
128
129 if (auto currentForm = form()) {
130 if (m_type == SUBMIT) {
131 SetForScope<bool> activatedSubmitState(m_isActivatedSubmit, true);
132 currentForm->prepareForSubmission(event);
133 }
134
135 if (m_type == RESET)
136 currentForm->reset();
137 }
138
139 if (m_type == SUBMIT || m_type == RESET)
140 event.setDefaultHandled();
141 }
142 }
143
144 if (is<KeyboardEvent>(event)) {
145 KeyboardEvent& keyboardEvent = downcast<KeyboardEvent>(event);
146 if (keyboardEvent.type() == eventNames().keydownEvent && keyboardEvent.keyIdentifier() == "U+0020") {
147 setActive(true, true);
148 // No setDefaultHandled() - IE dispatches a keypress in this case.
149 return;
150 }
151 if (keyboardEvent.type() == eventNames().keypressEvent) {
152 switch (keyboardEvent.charCode()) {
153 case '\r':
154 dispatchSimulatedClick(&keyboardEvent);
155 keyboardEvent.setDefaultHandled();
156 return;
157 case ' ':
158 // Prevent scrolling down the page.
159 keyboardEvent.setDefaultHandled();
160 return;
161 }
162 }
163 if (keyboardEvent.type() == eventNames().keyupEvent && keyboardEvent.keyIdentifier() == "U+0020") {
164 if (active())
165 dispatchSimulatedClick(&keyboardEvent);
166 keyboardEvent.setDefaultHandled();
167 return;
168 }
169 }
170
171 HTMLFormControlElement::defaultEventHandler(event);
172}
173
174bool HTMLButtonElement::willRespondToMouseClickEvents()
175{
176 return !isDisabledFormControl();
177}
178
179bool HTMLButtonElement::isSuccessfulSubmitButton() const
180{
181 // HTML spec says that buttons must have names to be considered successful.
182 // However, other browsers do not impose this constraint.
183 return m_type == SUBMIT && !isDisabledFormControl();
184}
185
186bool HTMLButtonElement::matchesDefaultPseudoClass() const
187{
188 return isSuccessfulSubmitButton() && form() && form()->defaultButton() == this;
189}
190
191bool HTMLButtonElement::isActivatedSubmit() const
192{
193 return m_isActivatedSubmit;
194}
195
196void HTMLButtonElement::setActivatedSubmit(bool flag)
197{
198 m_isActivatedSubmit = flag;
199}
200
201bool HTMLButtonElement::appendFormData(DOMFormData& formData, bool)
202{
203 if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit)
204 return false;
205 formData.append(name(), value());
206 return true;
207}
208
209void HTMLButtonElement::accessKeyAction(bool sendMouseEvents)
210{
211 focus();
212
213 dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
214}
215
216bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const
217{
218 return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute);
219}
220
221const AtomicString& HTMLButtonElement::value() const
222{
223 return attributeWithoutSynchronization(valueAttr);
224}
225
226bool HTMLButtonElement::computeWillValidate() const
227{
228 return m_type == SUBMIT && HTMLFormControlElement::computeWillValidate();
229}
230
231} // namespace
232