blob: c543eca43c1febdc573645eccf6f979bd989387c [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25#include "config.h"
26#include "core/html/HTMLLabelElement.h"
27
28#include "HTMLNames.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010029#include "core/dom/Event.h"
30#include "core/dom/EventNames.h"
31#include "core/dom/NodeTraversal.h"
32#include "core/html/FormAssociatedElement.h"
33
34namespace WebCore {
35
36using namespace HTMLNames;
37
38static LabelableElement* nodeAsLabelableElement(Node* node)
39{
40 if (!node || !node->isHTMLElement())
41 return 0;
42
43 HTMLElement* element = static_cast<HTMLElement*>(node);
44 if (!element->isLabelable())
45 return 0;
46
47 LabelableElement* labelableElement = static_cast<LabelableElement*>(element);
48 if (!labelableElement->supportLabels())
49 return 0;
50
51 return labelableElement;
52}
53
54inline HTMLLabelElement::HTMLLabelElement(const QualifiedName& tagName, Document* document)
55 : HTMLElement(tagName, document)
56{
57 ASSERT(hasTagName(labelTag));
58 ScriptWrappable::init(this);
59}
60
61PassRefPtr<HTMLLabelElement> HTMLLabelElement::create(const QualifiedName& tagName, Document* document)
62{
63 return adoptRef(new HTMLLabelElement(tagName, document));
64}
65
Torne (Richard Coles)521d96e2013-06-19 11:58:24 +010066bool HTMLLabelElement::rendererIsFocusable() const
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010067{
Torne (Richard Coles)81a51572013-05-13 16:52:28 +010068 HTMLLabelElement* that = const_cast<HTMLLabelElement*>(this);
69 return that->isContentEditable();
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010070}
71
72LabelableElement* HTMLLabelElement::control()
73{
74 const AtomicString& controlId = getAttribute(forAttr);
75 if (controlId.isNull()) {
76 // Search the children and descendants of the label element for a form element.
77 // per http://dev.w3.org/html5/spec/Overview.html#the-label-element
78 // the form element must be "labelable form-associated element".
79 Element* element = this;
80 while ((element = ElementTraversal::next(element, this))) {
81 if (LabelableElement* labelableElement = nodeAsLabelableElement(element))
82 return labelableElement;
83 }
84 return 0;
85 }
86
87 // Find the first element whose id is controlId. If it is found and it is a labelable form control,
88 // return it, otherwise return 0.
89 return nodeAsLabelableElement(treeScope()->getElementById(controlId));
90}
91
92HTMLFormElement* HTMLLabelElement::form() const
93{
94 return FormAssociatedElement::findAssociatedForm(this, 0);
95}
96
97void HTMLLabelElement::setActive(bool down, bool pause)
98{
99 if (down == active())
100 return;
101
102 // Update our status first.
103 HTMLElement::setActive(down, pause);
104
105 // Also update our corresponding control.
106 if (HTMLElement* element = control())
107 element->setActive(down, pause);
108}
109
110void HTMLLabelElement::setHovered(bool over)
111{
112 if (over == hovered())
113 return;
114
115 // Update our status first.
116 HTMLElement::setHovered(over);
117
118 // Also update our corresponding control.
119 if (HTMLElement* element = control())
120 element->setHovered(over);
121}
122
123void HTMLLabelElement::defaultEventHandler(Event* evt)
124{
125 static bool processingClick = false;
126
127 if (evt->type() == eventNames().clickEvent && !processingClick) {
128 RefPtr<HTMLElement> element = control();
129
130 // If we can't find a control or if the control received the click
131 // event, then there's no need for us to do anything.
132 if (!element || (evt->target() && element->containsIncludingShadowDOM(evt->target()->toNode())))
133 return;
134
135 processingClick = true;
136
137 // Click the corresponding control.
138 element->dispatchSimulatedClick(evt);
139
140 if (element->isMouseFocusable())
141 element->focus();
142
143 processingClick = false;
144
145 evt->setDefaultHandled();
146 }
147
148 HTMLElement::defaultEventHandler(evt);
149}
150
151bool HTMLLabelElement::willRespondToMouseClickEvents()
152{
153 if (control() && control()->willRespondToMouseClickEvents())
154 return true;
155
156 return HTMLElement::willRespondToMouseClickEvents();
157}
158
159void HTMLLabelElement::focus(bool, FocusDirection direction)
160{
161 // to match other browsers, always restore previous selection
162 if (HTMLElement* element = control())
163 element->focus(true, direction);
Torne (Richard Coles)5267f702013-06-11 10:57:24 +0100164 if (isFocusable())
165 HTMLElement::focus(true, direction);
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +0100166}
167
168void HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
169{
170 if (HTMLElement* element = control())
171 element->accessKeyAction(sendMouseEvents);
172 else
173 HTMLElement::accessKeyAction(sendMouseEvents);
174}
175
176} // namespace