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)
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 "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>
41 using namespace HTMLNames
;
43 HTMLButtonElement::HTMLButtonElement(const QualifiedName
& tagName
, Document
* doc
, HTMLFormElement
* form
)
44 : HTMLFormControlElement(tagName
, doc
, form
)
46 , m_activeSubmit(false)
48 ASSERT(hasTagName(buttonTag
));
51 HTMLButtonElement::~HTMLButtonElement()
55 RenderObject
* HTMLButtonElement::createRenderer(RenderArena
* arena
, RenderStyle
*)
57 return new (arena
) RenderButton(this);
60 const AtomicString
& HTMLButtonElement::formControlType() const
64 DEFINE_STATIC_LOCAL(const AtomicString
, submit
, ("submit"));
68 DEFINE_STATIC_LOCAL(const AtomicString
, button
, ("button"));
72 DEFINE_STATIC_LOCAL(const AtomicString
, reset
, ("reset"));
81 void HTMLButtonElement::parseMappedAttribute(MappedAttribute
* attr
)
83 if (attr
->name() == typeAttr
) {
84 if (equalIgnoringCase(attr
->value(), "reset"))
86 else if (equalIgnoringCase(attr
->value(), "button"))
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
));
98 HTMLFormControlElement::parseMappedAttribute(attr
);
101 void HTMLButtonElement::defaultEventHandler(Event
* evt
)
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
109 if (form() && m_type
== RESET
)
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.
119 if (evt
->type() == eventNames().keypressEvent
) {
120 switch (static_cast<KeyboardEvent
*>(evt
)->charCode()) {
122 dispatchSimulatedClick(evt
);
123 evt
->setDefaultHandled();
126 // Prevent scrolling down the page.
127 evt
->setDefaultHandled();
133 if (evt
->type() == eventNames().keyupEvent
&& static_cast<KeyboardEvent
*>(evt
)->keyIdentifier() == "U+0020") {
135 dispatchSimulatedClick(evt
);
136 evt
->setDefaultHandled();
141 HTMLFormControlElement::defaultEventHandler(evt
);
144 bool HTMLButtonElement::isSuccessfulSubmitButton() const
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();
151 bool HTMLButtonElement::isActivatedSubmit() const
153 return m_activeSubmit
;
156 void HTMLButtonElement::setActivatedSubmit(bool flag
)
158 m_activeSubmit
= flag
;
161 bool HTMLButtonElement::appendFormData(FormDataList
& formData
, bool)
163 if (m_type
!= SUBMIT
|| name().isEmpty() || !m_activeSubmit
)
165 formData
.appendData(name(), value());
169 void HTMLButtonElement::accessKeyAction(bool sendToAnyElement
)
172 // send the mouse button events iff the caller specified sendToAnyElement
173 dispatchSimulatedClick(0, sendToAnyElement
);
176 String
HTMLButtonElement::accessKey() const
178 return getAttribute(accesskeyAttr
);
181 void HTMLButtonElement::setAccessKey(const String
&value
)
183 setAttribute(accesskeyAttr
, value
);
186 String
HTMLButtonElement::value() const
188 return getAttribute(valueAttr
);
191 void HTMLButtonElement::setValue(const String
&value
)
193 setAttribute(valueAttr
, value
);