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)
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.
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.
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.
27 #include "core/html/HTMLButtonElement.h"
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"
40 using namespace HTMLNames;
42 inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
43 : HTMLFormControlElement(tagName, document, form)
45 , m_isActivatedSubmit(false)
47 ASSERT(hasTagName(buttonTag));
48 ScriptWrappable::init(this);
51 PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document& document, HTMLFormElement* form)
53 return adoptRef(new HTMLButtonElement(tagName, document, form));
56 void HTMLButtonElement::setType(const AtomicString& type)
58 setAttribute(typeAttr, type);
61 RenderObject* HTMLButtonElement::createRenderer(RenderStyle*)
63 return new RenderButton(this);
66 const AtomicString& HTMLButtonElement::formControlType() const
70 DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit", AtomicString::ConstructFromLiteral));
74 DEFINE_STATIC_LOCAL(const AtomicString, button, ("button", AtomicString::ConstructFromLiteral));
78 DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset", AtomicString::ConstructFromLiteral));
87 bool HTMLButtonElement::isPresentationAttribute(const QualifiedName& name) const
89 if (name == alignAttr) {
90 // Don't map 'align' attribute. This matches what Firefox and IE do, but not Opera.
91 // See http://bugs.webkit.org/show_bug.cgi?id=12071
95 return HTMLFormControlElement::isPresentationAttribute(name);
98 void HTMLButtonElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
100 if (name == typeAttr) {
101 if (equalIgnoringCase(value, "reset"))
103 else if (equalIgnoringCase(value, "button"))
107 setNeedsWillValidateCheck();
109 HTMLFormControlElement::parseAttribute(name, value);
112 void HTMLButtonElement::defaultEventHandler(Event* event)
114 if (event->type() == EventTypeNames::DOMActivate && !isDisabledFormControl()) {
115 if (form() && m_type == SUBMIT) {
116 m_isActivatedSubmit = true;
117 form()->prepareForSubmission(event);
118 event->setDefaultHandled();
119 m_isActivatedSubmit = false; // Do this in case submission was canceled.
121 if (form() && m_type == RESET) {
123 event->setDefaultHandled();
127 if (event->isKeyboardEvent()) {
128 if (event->type() == EventTypeNames::keydown && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
129 setActive(true, true);
130 // No setDefaultHandled() - IE dispatches a keypress in this case.
133 if (event->type() == EventTypeNames::keypress) {
134 switch (toKeyboardEvent(event)->charCode()) {
136 dispatchSimulatedClick(event);
137 event->setDefaultHandled();
140 // Prevent scrolling down the page.
141 event->setDefaultHandled();
145 if (event->type() == EventTypeNames::keyup && toKeyboardEvent(event)->keyIdentifier() == "U+0020") {
147 dispatchSimulatedClick(event);
148 event->setDefaultHandled();
153 HTMLFormControlElement::defaultEventHandler(event);
156 bool HTMLButtonElement::willRespondToMouseClickEvents()
158 if (!isDisabledFormControl() && form() && (m_type == SUBMIT || m_type == RESET))
160 return HTMLFormControlElement::willRespondToMouseClickEvents();
163 bool HTMLButtonElement::isSuccessfulSubmitButton() const
165 // HTML spec says that buttons must have names to be considered successful.
166 // However, other browsers do not impose this constraint.
167 return m_type == SUBMIT && !isDisabledFormControl();
170 bool HTMLButtonElement::isActivatedSubmit() const
172 return m_isActivatedSubmit;
175 void HTMLButtonElement::setActivatedSubmit(bool flag)
177 m_isActivatedSubmit = flag;
180 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
182 if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit)
184 formData.appendData(name(), value());
188 void HTMLButtonElement::accessKeyAction(bool sendMouseEvents)
192 dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
195 bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const
197 return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute);
200 String HTMLButtonElement::value() const
202 return getAttribute(valueAttr);
205 bool HTMLButtonElement::recalcWillValidate() const
207 return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate();
210 bool HTMLButtonElement::isInteractiveContent() const