Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / TreeScope.h
1 /*
2  * Copyright (C) 2011 Google Inc. All Rights Reserved.
3  * Copyright (C) 2012 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 #ifndef TreeScope_h
28 #define TreeScope_h
29
30 #include "core/dom/DocumentOrderedMap.h"
31 #include "platform/heap/Handle.h"
32 #include "wtf/Forward.h"
33 #include "wtf/text/AtomicString.h"
34
35 namespace WebCore {
36
37 class ContainerNode;
38 class DOMSelection;
39 class Document;
40 class Element;
41 class HTMLLabelElement;
42 class HTMLMapElement;
43 class HitTestResult;
44 class LayoutPoint;
45 class IdTargetObserverRegistry;
46 class Node;
47 class RenderObject;
48
49 // A class which inherits both Node and TreeScope must call clearRareData() in its destructor
50 // so that the Node destructor no longer does problematic NodeList cache manipulation in
51 // the destructor.
52 class TreeScope : public WillBeGarbageCollectedMixin {
53 public:
54     TreeScope* parentTreeScope() const { return m_parentTreeScope; }
55
56     TreeScope* olderShadowRootOrParentTreeScope() const;
57     bool isInclusiveOlderSiblingShadowRootOrAncestorTreeScopeOf(const TreeScope&) const;
58
59     Element* adjustedFocusedElement() const;
60     Element* getElementById(const AtomicString&) const;
61     const Vector<Element*>& getAllElementsById(const AtomicString&) const;
62     bool hasElementWithId(StringImpl* id) const;
63     bool containsMultipleElementsWithId(const AtomicString& id) const;
64     void addElementById(const AtomicString& elementId, Element*);
65     void removeElementById(const AtomicString& elementId, Element*);
66
67     Document& document() const
68     {
69         ASSERT(m_document);
70         return *m_document;
71     }
72
73     Node* ancestorInThisScope(Node*) const;
74
75     void addImageMap(HTMLMapElement*);
76     void removeImageMap(HTMLMapElement*);
77     HTMLMapElement* getImageMap(const String& url) const;
78
79     Element* elementFromPoint(int x, int y) const;
80
81     // For accessibility.
82     bool shouldCacheLabelsByForAttribute() const { return m_labelsByForAttribute; }
83     void addLabel(const AtomicString& forAttributeValue, HTMLLabelElement*);
84     void removeLabel(const AtomicString& forAttributeValue, HTMLLabelElement*);
85     HTMLLabelElement* labelElementForId(const AtomicString& forAttributeValue);
86
87     DOMSelection* getSelection() const;
88
89     // Find first anchor with the given name.
90     // First searches for an element with the given ID, but if that fails, then looks
91     // for an anchor with the given name. ID matching is always case sensitive, but
92     // Anchor name matching is case sensitive in strict mode and not case sensitive in
93     // quirks mode for historical compatibility reasons.
94     Element* findAnchor(const String& name);
95
96     bool applyAuthorStyles() const;
97
98     // Used by the basic DOM mutation methods (e.g., appendChild()).
99     void adoptIfNeeded(Node&);
100
101     Node& rootNode() const { return *m_rootNode; }
102
103     IdTargetObserverRegistry& idTargetObserverRegistry() const { return *m_idTargetObserverRegistry.get(); }
104
105
106 #if !ENABLE(OILPAN)
107     // Nodes belonging to this scope hold guard references -
108     // these are enough to keep the scope from being destroyed, but
109     // not enough to keep it from removing its children. This allows a
110     // node that outlives its scope to still have a valid document
111     // pointer without introducing reference cycles.
112     void guardRef()
113     {
114         ASSERT(!deletionHasBegun());
115         ++m_guardRefCount;
116     }
117
118     void guardDeref()
119     {
120         ASSERT(m_guardRefCount > 0);
121         ASSERT(!deletionHasBegun());
122         --m_guardRefCount;
123         if (!m_guardRefCount && !refCount() && !rootNodeHasTreeSharedParent()) {
124             beginDeletion();
125             delete this;
126         }
127     }
128 #endif
129
130     void removedLastRefToScope();
131
132     bool isInclusiveAncestorOf(const TreeScope&) const;
133     unsigned short comparePosition(const TreeScope&) const;
134
135     const TreeScope* commonAncestorTreeScope(const TreeScope& other) const;
136     TreeScope* commonAncestorTreeScope(TreeScope& other);
137
138     Element* getElementByAccessKey(const String& key) const;
139
140     virtual void trace(Visitor*);
141
142 protected:
143     TreeScope(ContainerNode&, Document&);
144     TreeScope(Document&);
145     virtual ~TreeScope();
146
147 #if !ENABLE(OILPAN)
148     void destroyTreeScopeData();
149 #endif
150
151     void setDocument(Document& document) { m_document = &document; }
152     void setParentTreeScope(TreeScope&);
153
154 #if !ENABLE(OILPAN)
155     bool hasGuardRefCount() const { return m_guardRefCount; }
156 #endif
157
158     void setNeedsStyleRecalcForViewportUnits();
159
160 private:
161     virtual void dispose() { }
162
163 #if !ENABLE(OILPAN)
164     int refCount() const;
165
166 #if SECURITY_ASSERT_ENABLED
167     bool deletionHasBegun();
168     void beginDeletion();
169 #else
170     bool deletionHasBegun() { return false; }
171     void beginDeletion() { }
172 #endif
173 #endif
174
175     bool rootNodeHasTreeSharedParent() const;
176
177     RawPtrWillBeMember<Node> m_rootNode;
178     RawPtrWillBeMember<Document> m_document;
179     RawPtrWillBeMember<TreeScope> m_parentTreeScope;
180
181 #if !ENABLE(OILPAN)
182     int m_guardRefCount;
183 #endif
184
185     OwnPtr<DocumentOrderedMap> m_elementsById;
186     OwnPtr<DocumentOrderedMap> m_imageMapsByName;
187     OwnPtr<DocumentOrderedMap> m_labelsByForAttribute;
188
189     OwnPtrWillBeMember<IdTargetObserverRegistry> m_idTargetObserverRegistry;
190
191     mutable RefPtrWillBeMember<DOMSelection> m_selection;
192 };
193
194 inline bool TreeScope::hasElementWithId(StringImpl* id) const
195 {
196     ASSERT(id);
197     return m_elementsById && m_elementsById->contains(id);
198 }
199
200 inline bool TreeScope::containsMultipleElementsWithId(const AtomicString& id) const
201 {
202     return m_elementsById && m_elementsById->containsMultiple(id.impl());
203 }
204
205 inline bool operator==(const TreeScope& a, const TreeScope& b) { return &a == &b; }
206 inline bool operator==(const TreeScope& a, const TreeScope* b) { return &a == b; }
207 inline bool operator==(const TreeScope* a, const TreeScope& b) { return a == &b; }
208 inline bool operator!=(const TreeScope& a, const TreeScope& b) { return !(a == b); }
209 inline bool operator!=(const TreeScope& a, const TreeScope* b) { return !(a == b); }
210 inline bool operator!=(const TreeScope* a, const TreeScope& b) { return !(a == b); }
211
212 HitTestResult hitTestInDocument(const Document*, int x, int y);
213 TreeScope* commonTreeScope(Node*, Node*);
214
215 } // namespace WebCore
216
217 #endif // TreeScope_h