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, 2010 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 "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>
38
39namespace WebCore {
40
41using namespace HTMLNames;
42
43inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
44    : HTMLFormControlElement(tagName, document, form)
45    , m_type(SUBMIT)
46    , m_isActivatedSubmit(false)
47{
48    ASSERT(hasTagName(buttonTag));
49}
50
51PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
52{
53    return adoptRef(new HTMLButtonElement(tagName, document, form));
54}
55
56void HTMLButtonElement::setType(const AtomicString& type)
57{
58    setAttribute(typeAttr, type);
59}
60
61RenderObject* HTMLButtonElement::createRenderer(RenderArena* arena, RenderStyle*)
62{
63    return new (arena) RenderButton(this);
64}
65
66const AtomicString& HTMLButtonElement::formControlType() const
67{
68    switch (m_type) {
69        case SUBMIT: {
70            DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit", AtomicString::ConstructFromLiteral));
71            return submit;
72        }
73        case BUTTON: {
74            DEFINE_STATIC_LOCAL(const AtomicString, button, ("button", AtomicString::ConstructFromLiteral));
75            return button;
76        }
77        case RESET: {
78            DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset", AtomicString::ConstructFromLiteral));
79            return reset;
80        }
81    }
82
83    ASSERT_NOT_REACHED();
84    return emptyAtom;
85}
86
87bool HTMLButtonElement::isPresentationAttribute(const QualifiedName& name) const
88{
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
92        return false;
93    }
94
95    return HTMLFormControlElement::isPresentationAttribute(name);
96}
97
98void HTMLButtonElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
99{
100    if (name == typeAttr) {
101        if (equalIgnoringCase(value, "reset"))
102            m_type = RESET;
103        else if (equalIgnoringCase(value, "button"))
104            m_type = BUTTON;
105        else
106            m_type = SUBMIT;
107        setNeedsWillValidateCheck();
108    } else
109        HTMLFormControlElement::parseAttribute(name, value);
110}
111
112void HTMLButtonElement::defaultEventHandler(Event* event)
113{
114    if (event->type() == eventNames().DOMActivateEvent && !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.
120        }
121        if (form() && m_type == RESET) {
122            form()->reset();
123            event->setDefaultHandled();
124        }
125    }
126
127    if (event->isKeyboardEvent()) {
128        if (event->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
129            setActive(true, true);
130            // No setDefaultHandled() - IE dispatches a keypress in this case.
131            return;
132        }
133        if (event->type() == eventNames().keypressEvent) {
134            switch (static_cast<KeyboardEvent*>(event)->charCode()) {
135                case '\r':
136                    dispatchSimulatedClick(event);
137                    event->setDefaultHandled();
138                    return;
139                case ' ':
140                    // Prevent scrolling down the page.
141                    event->setDefaultHandled();
142                    return;
143            }
144        }
145        if (event->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") {
146            if (active())
147                dispatchSimulatedClick(event);
148            event->setDefaultHandled();
149            return;
150        }
151    }
152
153    HTMLFormControlElement::defaultEventHandler(event);
154}
155
156bool HTMLButtonElement::willRespondToMouseClickEvents()
157{
158    if (!isDisabledFormControl() && form() && (m_type == SUBMIT || m_type == RESET))
159        return true;
160    return HTMLFormControlElement::willRespondToMouseClickEvents();
161}
162
163bool HTMLButtonElement::isSuccessfulSubmitButton() const
164{
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();
168}
169
170bool HTMLButtonElement::isActivatedSubmit() const
171{
172    return m_isActivatedSubmit;
173}
174
175void HTMLButtonElement::setActivatedSubmit(bool flag)
176{
177    m_isActivatedSubmit = flag;
178}
179
180bool HTMLButtonElement::appendFormData(FormDataList& formData, bool)
181{
182    if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit)
183        return false;
184    formData.appendData(name(), value());
185    return true;
186}
187
188void HTMLButtonElement::accessKeyAction(bool sendMouseEvents)
189{
190    focus();
191
192    dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents);
193}
194
195bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const
196{
197    return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute);
198}
199
200String HTMLButtonElement::value() const
201{
202    return getAttribute(valueAttr);
203}
204
205bool HTMLButtonElement::recalcWillValidate() const
206{
207    return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate();
208}
209
210} // namespace
211