1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2001 Dirk Mueller (mueller (at) kde.org) 5 * Copyright (C) 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved. 6 * (C) 2006 Alexey Proskuryakov (ap (at) nypop.com) 7 * Copyright (C) 2007 Samuel Weinig (sam (at) 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 "core/html/HTMLButtonElement.h" 28 29 #include "HTMLNames.h" 30 #include "core/dom/Attribute.h" 31 #include "core/dom/EventNames.h" 32 #include "core/dom/KeyboardEvent.h" 33 #include "core/html/FormDataList.h" 34 #include "core/html/HTMLFormElement.h" 35 #include "core/rendering/RenderButton.h" 36 #include "wtf/StdLibExtras.h" 37 38 namespace WebCore { 39 40 using namespace HTMLNames; 41 42 inline HTMLButtonElement::HTMLButtonElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form) 43 : HTMLFormControlElement(tagName, document, form) 44 , m_type(SUBMIT) 45 , m_isActivatedSubmit(false) 46 { 47 ASSERT(hasTagName(buttonTag)); 48 ScriptWrappable::init(this); 49 } 50 51 PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form) 52 { 53 return adoptRef(new HTMLButtonElement(tagName, document, form)); 54 } 55 56 void HTMLButtonElement::setType(const AtomicString& type) 57 { 58 setAttribute(typeAttr, type); 59 } 60 61 RenderObject* HTMLButtonElement::createRenderer(RenderStyle*) 62 { 63 return new RenderButton(this); 64 } 65 66 const 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 87 bool 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 98 void 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 112 void 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 && toKeyboardEvent(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 (toKeyboardEvent(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 && toKeyboardEvent(event)->keyIdentifier() == "U+0020") { 146 if (active()) 147 dispatchSimulatedClick(event); 148 event->setDefaultHandled(); 149 return; 150 } 151 } 152 153 HTMLFormControlElement::defaultEventHandler(event); 154 } 155 156 bool HTMLButtonElement::willRespondToMouseClickEvents() 157 { 158 if (!isDisabledFormControl() && form() && (m_type == SUBMIT || m_type == RESET)) 159 return true; 160 return HTMLFormControlElement::willRespondToMouseClickEvents(); 161 } 162 163 bool 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 170 bool HTMLButtonElement::isActivatedSubmit() const 171 { 172 return m_isActivatedSubmit; 173 } 174 175 void HTMLButtonElement::setActivatedSubmit(bool flag) 176 { 177 m_isActivatedSubmit = flag; 178 } 179 180 bool 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 188 void HTMLButtonElement::accessKeyAction(bool sendMouseEvents) 189 { 190 focus(); 191 192 dispatchSimulatedClick(0, sendMouseEvents ? SendMouseUpDownEvents : SendNoEvents); 193 } 194 195 bool HTMLButtonElement::isURLAttribute(const Attribute& attribute) const 196 { 197 return attribute.name() == formactionAttr || HTMLFormControlElement::isURLAttribute(attribute); 198 } 199 200 String HTMLButtonElement::value() const 201 { 202 return getAttribute(valueAttr); 203 } 204 205 bool HTMLButtonElement::recalcWillValidate() const 206 { 207 return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate(); 208 } 209 210 } // namespace 211