| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. All Rights Reserved. |
| 3 | * Copyright (C) 2012, 2013 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 APPLE INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 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 |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include "TreeScopeOrderedMap.h" |
| 30 | #include <memory> |
| 31 | #include <wtf/Forward.h> |
| 32 | #include <wtf/Vector.h> |
| 33 | #include <wtf/text/AtomicString.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | class ContainerNode; |
| 38 | class Document; |
| 39 | class Element; |
| 40 | class FloatPoint; |
| 41 | class HTMLImageElement; |
| 42 | class HTMLLabelElement; |
| 43 | class HTMLMapElement; |
| 44 | class LayoutPoint; |
| 45 | class IdTargetObserverRegistry; |
| 46 | class Node; |
| 47 | class ShadowRoot; |
| 48 | |
| 49 | class TreeScope { |
| 50 | friend class Document; |
| 51 | |
| 52 | public: |
| 53 | TreeScope* parentTreeScope() const { return m_parentTreeScope; } |
| 54 | void setParentTreeScope(TreeScope&); |
| 55 | |
| 56 | Element* focusedElementInScope(); |
| 57 | Element* pointerLockElement() const; |
| 58 | |
| 59 | WEBCORE_EXPORT Element* getElementById(const AtomicString&) const; |
| 60 | WEBCORE_EXPORT Element* getElementById(const String&) const; |
| 61 | Element* getElementById(StringView) const; |
| 62 | const Vector<Element*>* getAllElementsById(const AtomicString&) const; |
| 63 | bool hasElementWithId(const AtomicStringImpl&) const; |
| 64 | bool containsMultipleElementsWithId(const AtomicString& id) const; |
| 65 | void addElementById(const AtomicStringImpl& elementId, Element&, bool notifyObservers = true); |
| 66 | void removeElementById(const AtomicStringImpl& elementId, Element&, bool notifyObservers = true); |
| 67 | |
| 68 | WEBCORE_EXPORT Element* getElementByName(const AtomicString&) const; |
| 69 | bool hasElementWithName(const AtomicStringImpl&) const; |
| 70 | bool containsMultipleElementsWithName(const AtomicString&) const; |
| 71 | void addElementByName(const AtomicStringImpl&, Element&); |
| 72 | void removeElementByName(const AtomicStringImpl&, Element&); |
| 73 | |
| 74 | Document& documentScope() const { return m_documentScope.get(); } |
| 75 | static ptrdiff_t documentScopeMemoryOffset() { return OBJECT_OFFSETOF(TreeScope, m_documentScope); } |
| 76 | |
| 77 | // https://dom.spec.whatwg.org/#retarget |
| 78 | Node& retargetToScope(Node&) const; |
| 79 | |
| 80 | WEBCORE_EXPORT Node* ancestorNodeInThisScope(Node*) const; |
| 81 | WEBCORE_EXPORT Element* ancestorElementInThisScope(Element*) const; |
| 82 | |
| 83 | void addImageMap(HTMLMapElement&); |
| 84 | void removeImageMap(HTMLMapElement&); |
| 85 | HTMLMapElement* getImageMap(const AtomicString&) const; |
| 86 | |
| 87 | void addImageElementByUsemap(const AtomicStringImpl&, HTMLImageElement&); |
| 88 | void removeImageElementByUsemap(const AtomicStringImpl&, HTMLImageElement&); |
| 89 | HTMLImageElement* imageElementByUsemap(const AtomicStringImpl&) const; |
| 90 | |
| 91 | // For accessibility. |
| 92 | bool shouldCacheLabelsByForAttribute() const { return !!m_labelsByForAttribute; } |
| 93 | void addLabel(const AtomicStringImpl& forAttributeValue, HTMLLabelElement&); |
| 94 | void removeLabel(const AtomicStringImpl& forAttributeValue, HTMLLabelElement&); |
| 95 | HTMLLabelElement* labelElementForId(const AtomicString& forAttributeValue); |
| 96 | |
| 97 | WEBCORE_EXPORT RefPtr<Element> elementFromPoint(double clientX, double clientY); |
| 98 | WEBCORE_EXPORT Vector<RefPtr<Element>> elementsFromPoint(double clientX, double clientY); |
| 99 | WEBCORE_EXPORT Vector<RefPtr<Element>> elementsFromPoint(const FloatPoint&); |
| 100 | |
| 101 | // Find first anchor with the given name. |
| 102 | // First searches for an element with the given ID, but if that fails, then looks |
| 103 | // for an anchor with the given name. ID matching is always case sensitive, but |
| 104 | // Anchor name matching is case sensitive in strict mode and not case sensitive in |
| 105 | // quirks mode for historical compatibility reasons. |
| 106 | Element* findAnchor(const String& name); |
| 107 | |
| 108 | ContainerNode& rootNode() const { return m_rootNode; } |
| 109 | |
| 110 | IdTargetObserverRegistry& idTargetObserverRegistry() const { return *m_idTargetObserverRegistry.get(); } |
| 111 | |
| 112 | protected: |
| 113 | TreeScope(ShadowRoot&, Document&); |
| 114 | explicit TreeScope(Document&); |
| 115 | ~TreeScope(); |
| 116 | |
| 117 | void destroyTreeScopeData(); |
| 118 | void setDocumentScope(Document& document) |
| 119 | { |
| 120 | m_documentScope = document; |
| 121 | } |
| 122 | |
| 123 | Node* nodeFromPoint(const LayoutPoint& clientPoint, LayoutPoint* localPoint); |
| 124 | |
| 125 | private: |
| 126 | |
| 127 | ContainerNode& m_rootNode; |
| 128 | std::reference_wrapper<Document> m_documentScope; |
| 129 | TreeScope* m_parentTreeScope; |
| 130 | |
| 131 | std::unique_ptr<TreeScopeOrderedMap> m_elementsById; |
| 132 | std::unique_ptr<TreeScopeOrderedMap> m_elementsByName; |
| 133 | std::unique_ptr<TreeScopeOrderedMap> m_imageMapsByName; |
| 134 | std::unique_ptr<TreeScopeOrderedMap> m_imagesByUsemap; |
| 135 | std::unique_ptr<TreeScopeOrderedMap> m_labelsByForAttribute; |
| 136 | |
| 137 | std::unique_ptr<IdTargetObserverRegistry> m_idTargetObserverRegistry; |
| 138 | }; |
| 139 | |
| 140 | inline bool TreeScope::hasElementWithId(const AtomicStringImpl& id) const |
| 141 | { |
| 142 | return m_elementsById && m_elementsById->contains(id); |
| 143 | } |
| 144 | |
| 145 | inline bool TreeScope::containsMultipleElementsWithId(const AtomicString& id) const |
| 146 | { |
| 147 | return m_elementsById && id.impl() && m_elementsById->containsMultiple(*id.impl()); |
| 148 | } |
| 149 | |
| 150 | inline bool TreeScope::hasElementWithName(const AtomicStringImpl& id) const |
| 151 | { |
| 152 | return m_elementsByName && m_elementsByName->contains(id); |
| 153 | } |
| 154 | |
| 155 | inline bool TreeScope::containsMultipleElementsWithName(const AtomicString& name) const |
| 156 | { |
| 157 | return m_elementsByName && name.impl() && m_elementsByName->containsMultiple(*name.impl()); |
| 158 | } |
| 159 | |
| 160 | TreeScope* commonTreeScope(Node*, Node*); |
| 161 | |
| 162 | } // namespace WebCore |
| 163 | |