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 *
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 "HTMLLabelElement.h"
27
28#include "Document.h"
29#include "Event.h"
30#include "EventNames.h"
31#include "FormAssociatedElement.h"
32#include "HTMLNames.h"
33#include "NodeTraversal.h"
34
35namespace WebCore {
36
37using namespace HTMLNames;
38
39static LabelableElement* nodeAsLabelableElement(Node* node)
40{
41    if (!node || !node->isHTMLElement())
42        return 0;
43
44    HTMLElement* element = static_cast<HTMLElement*>(node);
45    if (!element->isLabelable())
46        return 0;
47
48    LabelableElement* labelableElement = static_cast<LabelableElement*>(element);
49    if (!labelableElement->supportLabels())
50        return 0;
51
52    return labelableElement;
53}
54
55inline HTMLLabelElement::HTMLLabelElement(const QualifiedName& tagName, Document* document)
56    : HTMLElement(tagName, document)
57{
58    ASSERT(hasTagName(labelTag));
59}
60
61PassRefPtr<HTMLLabelElement> HTMLLabelElement::create(const QualifiedName& tagName, Document* document)
62{
63    return adoptRef(new HTMLLabelElement(tagName, document));
64}
65
66bool HTMLLabelElement::isFocusable() const
67{
68    return false;
69}
70
71LabelableElement* HTMLLabelElement::control()
72{
73    const AtomicString& controlId = getAttribute(forAttr);
74    if (controlId.isNull()) {
75        // Search the children and descendants of the label element for a form element.
76        // per http://dev.w3.org/html5/spec/Overview.html#the-label-element
77        // the form element must be "labelable form-associated element".
78        Element* element = this;
79        while ((element = ElementTraversal::next(element, this))) {
80            if (LabelableElement* labelableElement = nodeAsLabelableElement(element))
81                return labelableElement;
82        }
83        return 0;
84    }
85
86    // Find the first element whose id is controlId. If it is found and it is a labelable form control,
87    // return it, otherwise return 0.
88    return nodeAsLabelableElement(treeScope()->getElementById(controlId));
89}
90
91HTMLFormElement* HTMLLabelElement::form() const
92{
93    return FormAssociatedElement::findAssociatedForm(this, 0);
94}
95
96void HTMLLabelElement::setActive(bool down, bool pause)
97{
98    if (down == active())
99        return;
100
101    // Update our status first.
102    HTMLElement::setActive(down, pause);
103
104    // Also update our corresponding control.
105    if (HTMLElement* element = control())
106        element->setActive(down, pause);
107}
108
109void HTMLLabelElement::setHovered(bool over)
110{
111    if (over == hovered())
112        return;
113
114    // Update our status first.
115    HTMLElement::setHovered(over);
116
117    // Also update our corresponding control.
118    if (HTMLElement* element = control())
119        element->setHovered(over);
120}
121
122void HTMLLabelElement::defaultEventHandler(Event* evt)
123{
124    static bool processingClick = false;
125
126    if (evt->type() == eventNames().clickEvent && !processingClick) {
127        RefPtr<HTMLElement> element = control();
128
129        // If we can't find a control or if the control received the click
130        // event, then there's no need for us to do anything.
131        if (!element || (evt->target() && element->containsIncludingShadowDOM(evt->target()->toNode())))
132            return;
133
134        processingClick = true;
135
136        // Click the corresponding control.
137        element->dispatchSimulatedClick(evt);
138
139        if (element->isMouseFocusable())
140            element->focus();
141
142        processingClick = false;
143
144        evt->setDefaultHandled();
145    }
146
147    HTMLElement::defaultEventHandler(evt);
148}
149
150bool HTMLLabelElement::willRespondToMouseClickEvents()
151{
152    if (control() && control()->willRespondToMouseClickEvents())
153        return true;
154
155    return HTMLElement::willRespondToMouseClickEvents();
156}
157
158void HTMLLabelElement::focus(bool, FocusDirection direction)
159{
160    // to match other browsers, always restore previous selection
161    if (HTMLElement* element = control())
162        element->focus(true, direction);
163}
164
165void HTMLLabelElement::accessKeyAction(bool sendMouseEvents)
166{
167    if (HTMLElement* element = control())
168        element->accessKeyAction(sendMouseEvents);
169    else
170        HTMLElement::accessKeyAction(sendMouseEvents);
171}
172
173} // namespace
174