2 * Copyright (C) 2011 Google Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "TreeScope.h"
29 #include "ComposedShadowTreeWalker.h"
30 #include "ContainerNode.h"
31 #include "ContextFeatures.h"
32 #include "DOMSelection.h"
33 #include "DOMWindow.h"
36 #include "FocusController.h"
38 #include "HTMLAnchorElement.h"
39 #include "HTMLFrameOwnerElement.h"
40 #include "HTMLMapElement.h"
41 #include "HTMLNames.h"
42 #include "IdTargetObserverRegistry.h"
43 #include "InsertionPoint.h"
45 #include "ShadowRoot.h"
46 #include "TreeScopeAdopter.h"
47 #include <wtf/Vector.h>
48 #include <wtf/text/AtomicString.h>
49 #include <wtf/text/CString.h>
53 using namespace HTMLNames;
55 TreeScope::TreeScope(ContainerNode* rootNode)
56 : m_rootNode(rootNode)
57 , m_parentTreeScope(0)
58 , m_idTargetObserverRegistry(IdTargetObserverRegistry::create())
63 TreeScope::~TreeScope()
66 m_selection->clearTreeScope();
71 void TreeScope::destroyTreeScopeData()
73 m_elementsById.clear();
74 m_imageMapsByName.clear();
77 void TreeScope::setParentTreeScope(TreeScope* newParentScope)
79 // A document node cannot be re-parented.
80 ASSERT(!rootNode()->isDocumentNode());
81 // Every scope other than document needs a parent scope.
82 ASSERT(newParentScope);
84 m_parentTreeScope = newParentScope;
87 Element* TreeScope::getElementById(const AtomicString& elementId) const
89 if (elementId.isEmpty())
91 return m_elementsById.getElementById(elementId.impl(), this);
94 void TreeScope::addElementById(const AtomicString& elementId, Element* element)
96 m_elementsById.add(elementId.impl(), element);
97 m_idTargetObserverRegistry->notifyObservers(elementId);
100 void TreeScope::removeElementById(const AtomicString& elementId, Element* element)
102 m_elementsById.remove(elementId.impl(), element);
103 m_idTargetObserverRegistry->notifyObservers(elementId);
106 Node* TreeScope::ancestorInThisScope(Node* node) const
109 if (node->treeScope() == this)
111 if (!node->isInShadowTree())
113 node = node->shadowAncestorNode();
119 void TreeScope::addImageMap(HTMLMapElement* imageMap)
121 AtomicStringImpl* name = imageMap->getName().impl();
124 m_imageMapsByName.add(name, imageMap);
127 void TreeScope::removeImageMap(HTMLMapElement* imageMap)
129 AtomicStringImpl* name = imageMap->getName().impl();
132 m_imageMapsByName.remove(name, imageMap);
135 HTMLMapElement* TreeScope::getImageMap(const String& url) const
139 size_t hashPos = url.find('#');
140 String name = (hashPos == notFound ? url : url.substring(hashPos + 1)).impl();
141 if (rootNode()->document()->isHTMLDocument())
142 return static_cast<HTMLMapElement*>(m_imageMapsByName.getElementByLowercasedMapName(AtomicString(name.lower()).impl(), this));
143 return static_cast<HTMLMapElement*>(m_imageMapsByName.getElementByMapName(AtomicString(name).impl(), this));
146 DOMSelection* TreeScope::getSelection() const
148 if (!rootNode()->document()->frame())
152 return m_selection.get();
154 // FIXME: The correct selection in Shadow DOM requires that Position can have a ShadowRoot
155 // as a container. It is now enabled only if runtime Shadow DOM feature is enabled.
156 // See https://bugs.webkit.org/show_bug.cgi?id=82697
157 #if ENABLE(SHADOW_DOM)
158 if (ContextFeatures::shadowDOMEnabled(rootNode()->document())) {
159 m_selection = DOMSelection::create(this);
160 return m_selection.get();
164 if (this != rootNode()->document())
165 return rootNode()->document()->getSelection();
167 m_selection = DOMSelection::create(rootNode()->document());
168 return m_selection.get();
171 Element* TreeScope::findAnchor(const String& name)
175 if (Element* element = getElementById(name))
177 for (Node* node = rootNode(); node; node = node->traverseNextNode()) {
178 if (node->hasTagName(aTag)) {
179 HTMLAnchorElement* anchor = static_cast<HTMLAnchorElement*>(node);
180 if (rootNode()->document()->inQuirksMode()) {
181 // Quirks mode, case insensitive comparison of names.
182 if (equalIgnoringCase(anchor->name(), name))
185 // Strict mode, names need to match exactly.
186 if (anchor->name() == name)
194 bool TreeScope::applyAuthorStyles() const
199 bool TreeScope::resetStyleInheritance() const
204 void TreeScope::adoptIfNeeded(Node* node)
208 ASSERT(!node->isDocumentNode());
209 ASSERT(!node->m_deletionHasBegun);
210 TreeScopeAdopter adopter(node, this);
211 if (adopter.needsScopeChange())
215 static Node* focusedFrameOwnerElement(Frame* focusedFrame, Frame* currentFrame)
217 for (; focusedFrame; focusedFrame = focusedFrame->tree()->parent()) {
218 if (focusedFrame->tree()->parent() == currentFrame)
219 return focusedFrame->ownerElement();
224 Node* TreeScope::focusedNode()
226 Document* document = rootNode()->document();
227 Node* node = document->focusedNode();
228 if (!node && document->page())
229 node = focusedFrameOwnerElement(document->page()->focusController()->focusedFrame(), document->frame());
232 Vector<Node*> targetStack;
234 for (ComposedShadowTreeParentWalker walker(node); walker.get(); walker.parentIncludingInsertionPointAndShadowRoot()) {
235 Node* node = walker.get();
236 if (targetStack.isEmpty())
237 targetStack.append(node);
238 else if (isInsertionPoint(node) && toInsertionPoint(node)->contains(last))
239 targetStack.append(targetStack.last());
240 if (node == rootNode())
241 return targetStack.last();
243 if (node->isShadowRoot()) {
244 ASSERT(!targetStack.isEmpty());
245 targetStack.removeLast();
251 static void listTreeScopes(Node* node, Vector<TreeScope*, 5>& treeScopes)
254 treeScopes.append(node->treeScope());
255 Element* ancestor = node->shadowHost();
262 TreeScope* commonTreeScope(Node* nodeA, Node* nodeB)
264 if (!nodeA || !nodeB)
267 if (nodeA->treeScope() == nodeB->treeScope())
268 return nodeA->treeScope();
270 Vector<TreeScope*, 5> treeScopesA;
271 listTreeScopes(nodeA, treeScopesA);
273 Vector<TreeScope*, 5> treeScopesB;
274 listTreeScopes(nodeB, treeScopesB);
276 size_t indexA = treeScopesA.size();
277 size_t indexB = treeScopesB.size();
279 for (; indexA > 0 && indexB > 0 && treeScopesA[indexA - 1] == treeScopesB[indexB - 1]; --indexA, --indexB) { }
281 return treeScopesA[indexA] == treeScopesB[indexB] ? treeScopesA[indexA] : 0;
284 } // namespace WebCore