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 "HTMLButtonElement.h"
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>
41 using namespace HTMLNames;
43 inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
44 : HTMLFormControlElement(tagName, document, form)
46 , m_isActivatedSubmit(false)
48 ASSERT(hasTagName(buttonTag));
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(RenderArena* arena, RenderStyle*)
63 return new (arena) RenderButton(this);
66 void HTMLButtonElement::willAddAuthorShadowRoot()
70 const AtomicString& HTMLButtonElement::formControlType() const
74 DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit"));
78 DEFINE_STATIC_LOCAL(const AtomicString, button, ("button"));
82 DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset"));
91 bool HTMLButtonElement::isPresentationAttribute(const QualifiedName& name) const
93 if (name == alignAttr) {
94 // Don't map 'align' attribute. This matches what Firefox and IE do, but not Opera.
95 // See http://bugs.webkit.org/show_bug.cgi?id=12071
99 return HTMLFormControlElement::isPresentationAttribute(name);
102 void HTMLButtonElement::parseAttribute(const Attribute& attribute)
104 if (attribute.name() == typeAttr) {
105 if (equalIgnoringCase(attribute.value(), "reset"))
107 else if (equalIgnoringCase(attribute.value(), "button"))
111 setNeedsWillValidateCheck();
113 HTMLFormControlElement::parseAttribute(attribute);
116 void HTMLButtonElement::defaultEventHandler(Event* event)
118 if (event->type() == eventNames().DOMActivateEvent && !disabled()) {
119 if (form() && m_type == SUBMIT) {
120 m_isActivatedSubmit = true;
121 form()->prepareForSubmission(event);
122 event->setDefaultHandled();
123 m_isActivatedSubmit = false; // Do this in case submission was canceled.
125 if (form() && m_type == RESET) {
127 event->setDefaultHandled();
131 if (event->isKeyboardEvent()) {
132 if (event->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
133 setActive(true, true);
134 // No setDefaultHandled() - IE dispatches a keypress in this case.
137 if (event->type() == eventNames().keypressEvent) {
138 switch (static_cast<KeyboardEvent*>(event)->charCode()) {
140 dispatchSimulatedClick(event);
141 event->setDefaultHandled();
144 // Prevent scrolling down the page.
145 event->setDefaultHandled();
149 if (event->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
151 dispatchSimulatedClick(event);
152 event->setDefaultHandled();
157 HTMLFormControlElement::defaultEventHandler(event);
160 bool HTMLButtonElement::willRespondToMouseClickEvents()
162 if (!disabled() && form() && (m_type == SUBMIT || m_type == RESET))
164 return HTMLFormControlElement::willRespondToMouseClickEvents();
167 bool HTMLButtonElement::isSuccessfulSubmitButton() const
169 // HTML spec says that buttons must have names to be considered successful.
170 // However, other browsers do not impose this constraint.
171 return m_type == SUBMIT && !disabled();
174 bool HTMLButtonElement::isActivatedSubmit() const
176 return m_isActivatedSubmit;
179 void HTMLButtonElement::setActivatedSubmit(bool flag)
181 m_isActivatedSubmit = flag;
184 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
186 if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit)
188 formData.appendData(name(), value());
192 void HTMLButtonElement::accessKeyAction(bool sendMouseEvents)
195 // Send the mouse button events if the caller specified sendMouseEvents
196 dispatchSimulatedClick(0, sendMouseEvents);
199 bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const
201 return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute);
204 String HTMLButtonElement::value() const
206 return getAttribute(valueAttr);
209 bool HTMLButtonElement::recalcWillValidate() const
211 return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate();