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