1/*
2 * Copyright (c) 2012 Motorola Mobility, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY MOTOROLA MOBILITY, INC. AND ITS CONTRIBUTORS
14 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MOTOROLA MOBILITY, INC. OR ITS
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "RadioNodeList.h"
28
29#include "Element.h"
30#include "HTMLFormElement.h"
31#include "HTMLInputElement.h"
32#include "HTMLNames.h"
33#include "HTMLObjectElement.h"
34#include "NodeRareData.h"
35
36namespace WebCore {
37
38using namespace HTMLNames;
39
40RadioNodeList::RadioNodeList(Node* rootNode, const AtomicString& name)
41    : LiveNodeList(rootNode, RadioNodeListType, InvalidateForFormControls, rootNode->hasTagName(formTag) ? NodeListIsRootedAtDocument : NodeListIsRootedAtNode)
42    , m_name(name)
43{
44}
45
46RadioNodeList::~RadioNodeList()
47{
48    ownerNode()->nodeLists()->removeCacheWithAtomicName(this, RadioNodeListType, m_name);
49}
50
51static inline HTMLInputElement* toRadioButtonInputElement(Node* node)
52{
53    ASSERT(node->isElementNode());
54    HTMLInputElement* inputElement = node->toInputElement();
55    if (!inputElement || !inputElement->isRadioButton() || inputElement->value().isEmpty())
56        return 0;
57    return inputElement;
58}
59
60String RadioNodeList::value() const
61{
62    for (unsigned i = 0; i < length(); ++i) {
63        Node* node = item(i);
64        const HTMLInputElement* inputElement = toRadioButtonInputElement(node);
65        if (!inputElement || !inputElement->checked())
66            continue;
67        return inputElement->value();
68    }
69    return String();
70}
71
72void RadioNodeList::setValue(const String& value)
73{
74    for (unsigned i = 0; i < length(); ++i) {
75        Node* node = item(i);
76        HTMLInputElement* inputElement = toRadioButtonInputElement(node);
77        if (!inputElement || inputElement->value() != value)
78            continue;
79        inputElement->setChecked(true);
80        return;
81    }
82}
83
84bool RadioNodeList::checkElementMatchesRadioNodeListFilter(Element* testElement) const
85{
86    ASSERT(testElement->hasTagName(objectTag) || testElement->isFormControlElement());
87    if (ownerNode()->hasTagName(formTag)) {
88        HTMLFormElement* formElement = 0;
89        if (testElement->hasTagName(objectTag))
90            formElement = static_cast<HTMLObjectElement*>(testElement)->form();
91        else
92            formElement = static_cast<HTMLFormControlElement*>(testElement)->form();
93        if (!formElement || formElement != ownerNode())
94            return false;
95    }
96
97    return testElement->getIdAttribute() == m_name || testElement->getNameAttribute() == m_name;
98}
99
100bool RadioNodeList::nodeMatches(Element* testElement) const
101{
102    if (!testElement->hasTagName(objectTag) && !testElement->isFormControlElement())
103        return false;
104
105    if (HTMLInputElement* inputElement = testElement->toInputElement()) {
106        if (inputElement->isImageButton())
107            return false;
108    }
109
110    return checkElementMatchesRadioNodeListFilter(testElement);
111}
112
113} // namspace
114
115