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"
30 #include "HTMLAnchorElement.h"
31 #include "HTMLMapElement.h"
32 #include "HTMLNames.h"
33 #include "NodeRareData.h"
37 using namespace HTMLNames;
39 TreeScope::TreeScope(Document* document)
40 : ContainerNode(document)
41 , m_parentTreeScope(0)
42 , m_numNodeListCaches(0)
46 TreeScope::~TreeScope()
52 void TreeScope::destroyTreeScopeData()
54 m_elementsById.clear();
55 m_imageMapsByName.clear();
58 void TreeScope::setParentTreeScope(TreeScope* newParentScope)
60 // A document node cannot be re-parented.
61 ASSERT(!isDocumentNode());
62 // Every scope other than document needs a parent scope.
63 ASSERT(newParentScope);
65 m_parentTreeScope = newParentScope;
68 Element* TreeScope::getElementById(const AtomicString& elementId) const
70 if (elementId.isEmpty())
72 return m_elementsById.getElementById(elementId.impl(), this);
75 void TreeScope::addElementById(const AtomicString& elementId, Element* element)
77 m_elementsById.add(elementId.impl(), element);
80 void TreeScope::removeElementById(const AtomicString& elementId, Element* element)
82 m_elementsById.remove(elementId.impl(), element);
85 void TreeScope::addImageMap(HTMLMapElement* imageMap)
87 AtomicStringImpl* name = imageMap->getName().impl();
90 m_imageMapsByName.add(name, imageMap);
93 void TreeScope::removeImageMap(HTMLMapElement* imageMap)
95 AtomicStringImpl* name = imageMap->getName().impl();
98 m_imageMapsByName.remove(name, imageMap);
101 HTMLMapElement* TreeScope::getImageMap(const String& url) const
105 size_t hashPos = url.find('#');
106 String name = (hashPos == notFound ? url : url.substring(hashPos + 1)).impl();
107 if (document()->isHTMLDocument())
108 return static_cast<HTMLMapElement*>(m_imageMapsByName.getElementByLowercasedMapName(AtomicString(name.lower()).impl(), this));
109 return static_cast<HTMLMapElement*>(m_imageMapsByName.getElementByMapName(AtomicString(name).impl(), this));
112 Element* TreeScope::findAnchor(const String& name)
116 if (Element* element = getElementById(name))
118 for (Node* node = this; node; node = node->traverseNextNode()) {
119 if (node->hasTagName(aTag)) {
120 HTMLAnchorElement* anchor = static_cast<HTMLAnchorElement*>(node);
121 if (document()->inQuirksMode()) {
122 // Quirks mode, case insensitive comparison of names.
123 if (equalIgnoringCase(anchor->name(), name))
126 // Strict mode, names need to match exactly.
127 if (anchor->name() == name)
135 bool TreeScope::applyAuthorSheets() const
140 } // namespace WebCore