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 "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 39 namespace WebCore { 40 41 using namespace HTMLNames; 42 43 inline 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 51 PassRefPtr<HTMLButtonElement> HTMLButtonElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form) 52 { 53 return adoptRef(new HTMLButtonElement(tagName, document, form)); 54 } 55 56 RenderObject* HTMLButtonElement::createRenderer(RenderArena* arena, RenderStyle*) 57 { 58 return new (arena) RenderButton(this); 59 } 60 61 const AtomicString& HTMLButtonElement::formControlType() const 62 { 63 switch (m_type) { 64 case SUBMIT: { 65 DEFINE_STATIC_LOCAL(const AtomicString, submit, ("submit")); 66 return submit; 67 } 68 case BUTTON: { 69 DEFINE_STATIC_LOCAL(const AtomicString, button, ("button")); 70 return button; 71 } 72 case RESET: { 73 DEFINE_STATIC_LOCAL(const AtomicString, reset, ("reset")); 74 return reset; 75 } 76 } 77 78 ASSERT_NOT_REACHED(); 79 return emptyAtom; 80 } 81 82 void HTMLButtonElement::parseMappedAttribute(Attribute* attr) 83 { 84 if (attr->name() == typeAttr) { 85 if (equalIgnoringCase(attr->value(), "reset")) 86 m_type = RESET; 87 else if (equalIgnoringCase(attr->value(), "button")) 88 m_type = BUTTON; 89 else 90 m_type = SUBMIT; 91 setNeedsWillValidateCheck(); 92 } else if (attr->name() == alignAttr) { 93 // Don't map 'align' attribute. This matches what Firefox and IE do, but not Opera. 94 // See http://bugs.webkit.org/show_bug.cgi?id=12071 95 } else 96 HTMLFormControlElement::parseMappedAttribute(attr); 97 } 98 99 void HTMLButtonElement::defaultEventHandler(Event* event) 100 { 101 if (event->type() == eventNames().DOMActivateEvent && !disabled()) { 102 if (form() && m_type == SUBMIT) { 103 m_isActivatedSubmit = true; 104 form()->prepareForSubmission(event); 105 m_isActivatedSubmit = false; // Do this in case submission was canceled. 106 } 107 if (form() && m_type == RESET) 108 form()->reset(); 109 } 110 111 if (event->isKeyboardEvent()) { 112 if (event->type() == eventNames().keydownEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") { 113 setActive(true, true); 114 // No setDefaultHandled() - IE dispatches a keypress in this case. 115 return; 116 } 117 if (event->type() == eventNames().keypressEvent) { 118 switch (static_cast<KeyboardEvent*>(event)->charCode()) { 119 case '\r': 120 dispatchSimulatedClick(event); 121 event->setDefaultHandled(); 122 return; 123 case ' ': 124 // Prevent scrolling down the page. 125 event->setDefaultHandled(); 126 return; 127 } 128 } 129 if (event->type() == eventNames().keyupEvent && static_cast<KeyboardEvent*>(event)->keyIdentifier() == "U+0020") { 130 if (active()) 131 dispatchSimulatedClick(event); 132 event->setDefaultHandled(); 133 return; 134 } 135 } 136 137 HTMLFormControlElement::defaultEventHandler(event); 138 } 139 140 bool HTMLButtonElement::isSuccessfulSubmitButton() const 141 { 142 // HTML spec says that buttons must have names to be considered successful. 143 // However, other browsers do not impose this constraint. 144 return m_type == SUBMIT && !disabled(); 145 } 146 147 bool HTMLButtonElement::isActivatedSubmit() const 148 { 149 return m_isActivatedSubmit; 150 } 151 152 void HTMLButtonElement::setActivatedSubmit(bool flag) 153 { 154 m_isActivatedSubmit = flag; 155 } 156 157 bool HTMLButtonElement::appendFormData(FormDataList& formData, bool) 158 { 159 if (m_type != SUBMIT || name().isEmpty() || !m_isActivatedSubmit) 160 return false; 161 formData.appendData(name(), value()); 162 return true; 163 } 164 165 void HTMLButtonElement::accessKeyAction(bool sendToAnyElement) 166 { 167 focus(); 168 // send the mouse button events iff the caller specified sendToAnyElement 169 dispatchSimulatedClick(0, sendToAnyElement); 170 } 171 172 bool HTMLButtonElement::isURLAttribute(Attribute* attr) const 173 { 174 return attr->name() == formactionAttr; 175 } 176 177 String HTMLButtonElement::value() const 178 { 179 return getAttribute(valueAttr); 180 } 181 182 bool HTMLButtonElement::recalcWillValidate() const 183 { 184 return m_type == SUBMIT && HTMLFormControlElement::recalcWillValidate(); 185 } 186 187 } // namespace 188