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, 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 "HTMLLegendElement.h"
27
28#include "HTMLNames.h"
29#include <wtf/StdLibExtras.h>
30
31namespace WebCore {
32
33using namespace HTMLNames;
34
35inline HTMLLegendElement::HTMLLegendElement(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
36    : HTMLFormControlElement(tagName, document, form)
37{
38    ASSERT(hasTagName(legendTag));
39}
40
41PassRefPtr<HTMLLegendElement> HTMLLegendElement::create(const QualifiedName& tagName, Document* document, HTMLFormElement* form)
42{
43    return adoptRef(new HTMLLegendElement(tagName, document, form));
44}
45
46bool HTMLLegendElement::supportsFocus() const
47{
48    return HTMLElement::supportsFocus();
49}
50
51const AtomicString& HTMLLegendElement::formControlType() const
52{
53    DEFINE_STATIC_LOCAL(const AtomicString, legend, ("legend"));
54    return legend;
55}
56
57HTMLFormControlElement* HTMLLegendElement::associatedControl()
58{
59    // Check if there's a fieldset belonging to this legend.
60    ContainerNode* fieldset = parentNode();
61    while (fieldset && !fieldset->hasTagName(fieldsetTag))
62        fieldset = fieldset->parentNode();
63    if (!fieldset)
64        return 0;
65
66    // Find first form element inside the fieldset that is not a legend element.
67    // FIXME: Should we consider tabindex?
68    Node* node = fieldset;
69    while ((node = node->traverseNextNode(fieldset))) {
70        if (node->isElementNode()) {
71            Element* element = static_cast<Element*>(node);
72            if (!element->hasLocalName(legendTag) && element->isFormControlElement())
73                return static_cast<HTMLFormControlElement*>(element);
74        }
75    }
76
77    return 0;
78}
79
80void HTMLLegendElement::focus(bool)
81{
82    if (isFocusable())
83        Element::focus();
84
85    // To match other browsers' behavior, never restore previous selection.
86    if (HTMLFormControlElement* control = associatedControl())
87        control->focus(false);
88}
89
90void HTMLLegendElement::accessKeyAction(bool sendToAnyElement)
91{
92    if (HTMLFormControlElement* control = associatedControl())
93        control->accessKeyAction(sendToAnyElement);
94}
95
96} // namespace
97