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/AtomString.h>
34
35namespace WebCore {
36
37class ContainerNode;
38class Document;
39class Element;
40class FloatPoint;
41class HTMLImageElement;
42class HTMLLabelElement;
43class HTMLMapElement;
44class LayoutPoint;
45class IdTargetObserverRegistry;
46class Node;
47class ShadowRoot;
48
49class TreeScope {
50 friend class Document;
51
52public:
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 AtomString&) const;
60 WEBCORE_EXPORT Element* getElementById(const String&) const;
61 Element* getElementById(StringView) const;
62 const Vector<Element*>* getAllElementsById(const AtomString&) const;
63 bool hasElementWithId(const AtomStringImpl&) const;
64 bool containsMultipleElementsWithId(const AtomString& id) const;
65 void addElementById(const AtomStringImpl& elementId, Element&, bool notifyObservers = true);
66 void removeElementById(const AtomStringImpl& elementId, Element&, bool notifyObservers = true);
67
68 WEBCORE_EXPORT Element* getElementByName(const AtomString&) const;
69 bool hasElementWithName(const AtomStringImpl&) const;
70 bool containsMultipleElementsWithName(const AtomString&) const;
71 void addElementByName(const AtomStringImpl&, Element&);
72 void removeElementByName(const AtomStringImpl&, 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 AtomString&) const;
86
87 void addImageElementByUsemap(const AtomStringImpl&, HTMLImageElement&);
88 void removeImageElementByUsemap(const AtomStringImpl&, HTMLImageElement&);
89 HTMLImageElement* imageElementByUsemap(const AtomStringImpl&) const;
90
91 // For accessibility.
92 bool shouldCacheLabelsByForAttribute() const { return !!m_labelsByForAttribute; }
93 void addLabel(const AtomStringImpl& forAttributeValue, HTMLLabelElement&);
94 void removeLabel(const AtomStringImpl& forAttributeValue, HTMLLabelElement&);
95 HTMLLabelElement* labelElementForId(const AtomString& 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
112protected:
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
125private:
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
140inline bool TreeScope::hasElementWithId(const AtomStringImpl& id) const
141{
142 return m_elementsById && m_elementsById->contains(id);
143}
144
145inline bool TreeScope::containsMultipleElementsWithId(const AtomString& id) const
146{
147 return m_elementsById && id.impl() && m_elementsById->containsMultiple(*id.impl());
148}
149
150inline bool TreeScope::hasElementWithName(const AtomStringImpl& id) const
151{
152 return m_elementsByName && m_elementsByName->contains(id);
153}
154
155inline bool TreeScope::containsMultipleElementsWithName(const AtomString& name) const
156{
157 return m_elementsByName && name.impl() && m_elementsByName->containsMultiple(*name.impl());
158}
159
160TreeScope* commonTreeScope(Node*, Node*);
161
162} // namespace WebCore
163