Rubber-stamped by Brady Eidson.
[webbrowser.git] / WebCore / html / HTMLButtonElement.cpp
blob3987859d02cca2dbb6a3770f2409e8b8ed16a1ec
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 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.
26 #include "config.h"
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>
39 namespace WebCore {
41 using namespace HTMLNames;
43 HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* doc, HTMLFormElement* form)
44 : HTMLFormControlElement(tagName, doc, form)
45 , m_type(SUBMIT)
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
62 switch (m_type) {
63 case SUBMIT: {
64 DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit"));
65 return submit;
67 case BUTTON: {
68 DEFINE_STATIC_LOCAL(const AtomicString, button, ("button"));
69 return button;
71 case RESET: {
72 DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset"));
73 return reset;
77 ASSERT_NOT_REACHED();
78 return emptyAtom;
81 void HTMLButtonElement::parseMappedAttribute(MappedAttribute* attr)
83 if (attr->name() == typeAttr) {
84 if (equalIgnoringCase(attr->value(), "reset"))
85 m_type = RESET;
86 else if (equalIgnoringCase(attr->value(), "button"))
87 m_type = BUTTON;
88 else
89 m_type = SUBMIT;
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));
97 } else
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)
110 form()->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.
117 return;
119 if (evt->type() == eventNames().keypressEvent) {
120 switch (static_cast<KeyboardEvent*>(evt)->charCode()) {
121 case '\r':
122 dispatchSimulatedClick(evt);
123 evt->setDefaultHandled();
124 return;
125 case ' ':
126 // Prevent scrolling down the page.
127 evt->setDefaultHandled();
128 return;
129 default:
130 break;
133 if (evt->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(evt)->keyIdentifier() == "U+0020") {
134 if (active())
135 dispatchSimulatedClick(evt);
136 evt->setDefaultHandled();
137 return;
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)
164 return false;
165 formData.appendData(name(), value());
166 return true;
169 void HTMLButtonElement::accessKeyAction(bool sendToAnyElement)
171 focus();
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);
196 } // namespace