| 1 | /* |
| 2 | * Copyright (C) 2015-2019 Apple 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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * 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 APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * 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 | #pragma once |
| 27 | |
| 28 | #include "CollectionTraversal.h" |
| 29 | #include "HTMLCollection.h" |
| 30 | #include "HTMLElement.h" |
| 31 | #include <wtf/IsoMalloc.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 36 | class CachedHTMLCollection : public HTMLCollection { |
| 37 | WTF_MAKE_ISO_NONALLOCATABLE(CachedHTMLCollection); |
| 38 | public: |
| 39 | CachedHTMLCollection(ContainerNode& base, CollectionType); |
| 40 | |
| 41 | virtual ~CachedHTMLCollection(); |
| 42 | |
| 43 | unsigned length() const final { return m_indexCache.nodeCount(collection()); } |
| 44 | Element* item(unsigned offset) const override { return m_indexCache.nodeAt(collection(), offset); } |
| 45 | Element* namedItem(const AtomicString& name) const override; |
| 46 | size_t memoryCost() const final |
| 47 | { |
| 48 | // memoryCost() may be invoked concurrently from a GC thread, and we need to be careful about what data we access here and how. |
| 49 | // Accessing m_indexCache.memoryCost() is safe because because it doesn't involve any pointer chasing. |
| 50 | // HTMLCollection::memoryCost() ensures its own thread safety. |
| 51 | return m_indexCache.memoryCost() + HTMLCollection::memoryCost(); |
| 52 | } |
| 53 | |
| 54 | // For CollectionIndexCache; do not use elsewhere. |
| 55 | using CollectionTraversalIterator = typename CollectionTraversal<traversalType>::Iterator; |
| 56 | CollectionTraversalIterator collectionBegin() const { return CollectionTraversal<traversalType>::begin(collection(), rootNode()); } |
| 57 | CollectionTraversalIterator collectionLast() const { return CollectionTraversal<traversalType>::last(collection(), rootNode()); } |
| 58 | CollectionTraversalIterator collectionEnd() const { return CollectionTraversal<traversalType>::end(rootNode()); } |
| 59 | void collectionTraverseForward(CollectionTraversalIterator& current, unsigned count, unsigned& traversedCount) const { CollectionTraversal<traversalType>::traverseForward(collection(), current, count, traversedCount); } |
| 60 | void collectionTraverseBackward(CollectionTraversalIterator& current, unsigned count) const { CollectionTraversal<traversalType>::traverseBackward(collection(), current, count); } |
| 61 | bool collectionCanTraverseBackward() const { return traversalType != CollectionTraversalType::CustomForwardOnly; } |
| 62 | void willValidateIndexCache() const { document().registerCollection(const_cast<CachedHTMLCollection<HTMLCollectionClass, traversalType>&>(*this)); } |
| 63 | |
| 64 | void invalidateCacheForDocument(Document&) override; |
| 65 | |
| 66 | bool elementMatches(Element&) const; |
| 67 | |
| 68 | private: |
| 69 | HTMLCollectionClass& collection() { return static_cast<HTMLCollectionClass&>(*this); } |
| 70 | const HTMLCollectionClass& collection() const { return static_cast<const HTMLCollectionClass&>(*this); } |
| 71 | |
| 72 | mutable CollectionIndexCache<HTMLCollectionClass, CollectionTraversalIterator> m_indexCache; |
| 73 | }; |
| 74 | |
| 75 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 76 | CachedHTMLCollection<HTMLCollectionClass, traversalType>::CachedHTMLCollection(ContainerNode& base, CollectionType collectionType) |
| 77 | : HTMLCollection(base, collectionType) |
| 78 | , m_indexCache(collection()) |
| 79 | { } |
| 80 | |
| 81 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 82 | CachedHTMLCollection<HTMLCollectionClass, traversalType>::~CachedHTMLCollection() |
| 83 | { |
| 84 | if (m_indexCache.hasValidCache(collection())) |
| 85 | document().unregisterCollection(*this); |
| 86 | } |
| 87 | |
| 88 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 89 | void CachedHTMLCollection<HTMLCollectionClass, traversalType>::invalidateCacheForDocument(Document& document) |
| 90 | { |
| 91 | HTMLCollection::invalidateCacheForDocument(document); |
| 92 | if (m_indexCache.hasValidCache(collection())) { |
| 93 | document.unregisterCollection(*this); |
| 94 | m_indexCache.invalidate(collection()); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 99 | bool CachedHTMLCollection<HTMLCollectionClass, traversalType>::elementMatches(Element&) const |
| 100 | { |
| 101 | // We call the elementMatches() method directly on the subclass instead for performance. |
| 102 | ASSERT_NOT_REACHED(); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | static inline bool nameShouldBeVisibleInDocumentAll(HTMLElement& element) |
| 107 | { |
| 108 | // https://html.spec.whatwg.org/multipage/infrastructure.html#all-named-elements |
| 109 | return element.hasTagName(HTMLNames::aTag) |
| 110 | || element.hasTagName(HTMLNames::appletTag) |
| 111 | || element.hasTagName(HTMLNames::buttonTag) |
| 112 | || element.hasTagName(HTMLNames::embedTag) |
| 113 | || element.hasTagName(HTMLNames::formTag) |
| 114 | || element.hasTagName(HTMLNames::frameTag) |
| 115 | || element.hasTagName(HTMLNames::framesetTag) |
| 116 | || element.hasTagName(HTMLNames::iframeTag) |
| 117 | || element.hasTagName(HTMLNames::imgTag) |
| 118 | || element.hasTagName(HTMLNames::inputTag) |
| 119 | || element.hasTagName(HTMLNames::mapTag) |
| 120 | || element.hasTagName(HTMLNames::metaTag) |
| 121 | || element.hasTagName(HTMLNames::objectTag) |
| 122 | || element.hasTagName(HTMLNames::selectTag) |
| 123 | || element.hasTagName(HTMLNames::textareaTag); |
| 124 | } |
| 125 | |
| 126 | static inline bool nameShouldBeVisibleInDocumentAll(Element& element) |
| 127 | { |
| 128 | return is<HTMLElement>(element) && nameShouldBeVisibleInDocumentAll(downcast<HTMLElement>(element)); |
| 129 | } |
| 130 | |
| 131 | template <typename HTMLCollectionClass, CollectionTraversalType traversalType> |
| 132 | Element* CachedHTMLCollection<HTMLCollectionClass, traversalType>::namedItem(const AtomicString& name) const |
| 133 | { |
| 134 | // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp |
| 135 | // This method first searches for an object with a matching id |
| 136 | // attribute. If a match is not found, the method then searches for an |
| 137 | // object with a matching name attribute, but only on those elements |
| 138 | // that are allowed a name attribute. |
| 139 | |
| 140 | if (name.isEmpty()) |
| 141 | return nullptr; |
| 142 | |
| 143 | ContainerNode& root = rootNode(); |
| 144 | if (traversalType != CollectionTraversalType::CustomForwardOnly && root.isInTreeScope()) { |
| 145 | Element* candidate = nullptr; |
| 146 | |
| 147 | TreeScope& treeScope = root.treeScope(); |
| 148 | if (treeScope.hasElementWithId(*name.impl())) { |
| 149 | if (!treeScope.containsMultipleElementsWithId(name)) |
| 150 | candidate = treeScope.getElementById(name); |
| 151 | } else if (treeScope.hasElementWithName(*name.impl())) { |
| 152 | if (!treeScope.containsMultipleElementsWithName(name)) { |
| 153 | if ((candidate = treeScope.getElementByName(name))) { |
| 154 | if (!is<HTMLElement>(*candidate)) |
| 155 | candidate = nullptr; |
| 156 | else if (type() == DocAll && !nameShouldBeVisibleInDocumentAll(*candidate)) |
| 157 | candidate = nullptr; |
| 158 | } |
| 159 | } |
| 160 | } else |
| 161 | return nullptr; |
| 162 | |
| 163 | if (candidate && collection().elementMatches(*candidate)) { |
| 164 | if (traversalType == CollectionTraversalType::ChildrenOnly ? candidate->parentNode() == &root : candidate->isDescendantOf(root)) |
| 165 | return candidate; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return namedItemSlow(name); |
| 170 | } |
| 171 | |
| 172 | } // namespace WebCore |
| 173 | |