initial import
[vuplus_webkit] / Source / WebCore / dom / TreeScope.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #include "config.h"
27 #include "TreeScope.h"
28
29 #include "Element.h"
30 #include "HTMLAnchorElement.h"
31 #include "HTMLMapElement.h"
32 #include "HTMLNames.h"
33 #include "NodeRareData.h"
34
35 namespace WebCore {
36
37 using namespace HTMLNames;
38
39 TreeScope::TreeScope(Document* document)
40     : ContainerNode(document)
41     , m_parentTreeScope(0)
42     , m_numNodeListCaches(0)
43 {
44 }
45
46 TreeScope::~TreeScope()
47 {
48     if (hasRareData())
49         clearRareData();
50 }
51
52 void TreeScope::destroyTreeScopeData()
53 {
54     m_elementsById.clear();
55     m_imageMapsByName.clear();
56 }
57
58 void TreeScope::setParentTreeScope(TreeScope* newParentScope)
59 {
60     // A document node cannot be re-parented.
61     ASSERT(!isDocumentNode());
62     // Every scope other than document needs a parent scope.
63     ASSERT(newParentScope);
64
65     m_parentTreeScope = newParentScope;
66 }
67
68 Element* TreeScope::getElementById(const AtomicString& elementId) const
69 {
70     if (elementId.isEmpty())
71         return 0;
72     return m_elementsById.getElementById(elementId.impl(), this);
73 }
74
75 void TreeScope::addElementById(const AtomicString& elementId, Element* element)
76 {
77     m_elementsById.add(elementId.impl(), element);
78 }
79
80 void TreeScope::removeElementById(const AtomicString& elementId, Element* element)
81 {
82     m_elementsById.remove(elementId.impl(), element);
83 }
84
85 void TreeScope::addImageMap(HTMLMapElement* imageMap)
86 {
87     AtomicStringImpl* name = imageMap->getName().impl();
88     if (!name)
89         return;
90     m_imageMapsByName.add(name, imageMap);
91 }
92
93 void TreeScope::removeImageMap(HTMLMapElement* imageMap)
94 {
95     AtomicStringImpl* name = imageMap->getName().impl();
96     if (!name)
97         return;
98     m_imageMapsByName.remove(name, imageMap);
99 }
100
101 HTMLMapElement* TreeScope::getImageMap(const String& url) const
102 {
103     if (url.isNull())
104         return 0;
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));
110 }
111
112 Element* TreeScope::findAnchor(const String& name)
113 {
114     if (name.isEmpty())
115         return 0;
116     if (Element* element = getElementById(name))
117         return element;
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))
124                     return anchor;
125             } else {
126                 // Strict mode, names need to match exactly.
127                 if (anchor->name() == name)
128                     return anchor;
129             }
130         }
131     }
132     return 0;
133 }
134
135 bool TreeScope::applyAuthorSheets() const
136 {
137     return true;
138 }
139
140 } // namespace WebCore
141