2 * Copyright (c) 2012 Motorola Mobility, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/html/RadioNodeList.h"
29 #include "HTMLNames.h"
30 #include "core/dom/Element.h"
31 #include "core/dom/NodeRareData.h"
32 #include "core/html/HTMLFormElement.h"
33 #include "core/html/HTMLInputElement.h"
34 #include "core/html/HTMLObjectElement.h"
38 using namespace HTMLNames;
40 RadioNodeList::RadioNodeList(ContainerNode* rootNode, const AtomicString& name, CollectionType type)
41 : LiveNodeList(rootNode, type, InvalidateForFormControls, rootNode->hasTagName(formTag) ? NodeListIsRootedAtDocument : NodeListIsRootedAtNode)
43 , m_onlyMatchImgElements(type == RadioImgNodeListType)
45 ScriptWrappable::init(this);
48 RadioNodeList::~RadioNodeList()
50 ownerNode()->nodeLists()->removeCache(this, m_onlyMatchImgElements ? RadioImgNodeListType : RadioNodeListType, m_name);
53 static inline HTMLInputElement* toRadioButtonInputElement(Node* node)
55 ASSERT(node->isElementNode());
56 if (!node->hasTagName(inputTag))
58 HTMLInputElement* inputElement = toHTMLInputElement(node);
59 if (!inputElement->isRadioButton() || inputElement->value().isEmpty())
64 String RadioNodeList::value() const
66 if (m_onlyMatchImgElements)
68 for (unsigned i = 0; i < length(); ++i) {
70 const HTMLInputElement* inputElement = toRadioButtonInputElement(node);
71 if (!inputElement || !inputElement->checked())
73 return inputElement->value();
78 void RadioNodeList::setValue(const String& value)
80 if (m_onlyMatchImgElements)
82 for (unsigned i = 0; i < length(); ++i) {
84 HTMLInputElement* inputElement = toRadioButtonInputElement(node);
85 if (!inputElement || inputElement->value() != value)
87 inputElement->setChecked(true);
92 bool RadioNodeList::checkElementMatchesRadioNodeListFilter(const Element& testElement) const
94 ASSERT(!m_onlyMatchImgElements);
95 ASSERT(testElement.hasTagName(objectTag) || testElement.isFormControlElement());
96 if (ownerNode()->hasTagName(formTag)) {
97 HTMLFormElement* formElement = toHTMLElement(testElement).formOwner();
98 if (!formElement || formElement != ownerNode())
102 return testElement.getIdAttribute() == m_name || testElement.getNameAttribute() == m_name;
105 bool RadioNodeList::nodeMatches(const Element& testElement) const
107 if (m_onlyMatchImgElements)
108 return testElement.hasTagName(imgTag);
110 if (!testElement.hasTagName(objectTag) && !testElement.isFormControlElement())
113 if (testElement.hasTagName(inputTag) && toHTMLInputElement(testElement).isImageButton())
116 return checkElementMatchesRadioNodeListFilter(testElement);