1 /*
2 * Copyright (C) 2005, 2006, 2009 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21
22 #ifdef SKIP_STATIC_CONSTRUCTORS_ON_GCC
23 #define WEBCORE_QUALIFIEDNAME_HIDE_GLOBALS 1
24 #else
25 #define QNAME_DEFAULT_CONSTRUCTOR
26 #endif
27
28 #include "core/dom/QualifiedName.h"
29
30 #include "core/HTMLNames.h"
31 #include "core/SVGNames.h"
32 #include "core/XLinkNames.h"
33 #include "core/XMLNSNames.h"
34 #include "core/XMLNames.h"
35 #include "wtf/Assertions.h"
36 #include "wtf/HashSet.h"
37 #include "wtf/MainThread.h"
38 #include "wtf/StaticConstructors.h"
39
40 namespace WebCore {
41
42 static const int staticQualifiedNamesCount = HTMLNames::HTMLTagsCount + HTMLNames::HTMLAttrsCount
43 + SVGNames::SVGTagsCount + SVGNames::SVGAttrsCount
44 + XLinkNames::XLinkAttrsCount
45 + XMLNSNames::XMLNSAttrsCount
46 + XMLNames::XMLAttrsCount;
47
48 struct QualifiedNameHashTraits : public HashTraits<QualifiedName::QualifiedNameImpl*> {
49 static const unsigned minimumTableSize = WTF::HashTableCapacityForSize<staticQualifiedNamesCount>::value;
50 };
51
52 typedef HashSet<QualifiedName::QualifiedNameImpl*, QualifiedNameHash, QualifiedNameHashTraits> QualifiedNameCache;
53
qualifiedNameCache()54 static QualifiedNameCache& qualifiedNameCache()
55 {
56 // This code is lockless and thus assumes it all runs on one thread!
57 ASSERT(isMainThread());
58 static QualifiedNameCache* gNameCache = new QualifiedNameCache;
59 return *gNameCache;
60 }
61
62 struct QNameComponentsTranslator {
hashWebCore::QNameComponentsTranslator63 static unsigned hash(const QualifiedNameData& data)
64 {
65 return hashComponents(data.m_components);
66 }
equalWebCore::QNameComponentsTranslator67 static bool equal(QualifiedName::QualifiedNameImpl* name, const QualifiedNameData& data)
68 {
69 return data.m_components.m_prefix == name->m_prefix.impl()
70 && data.m_components.m_localName == name->m_localName.impl()
71 && data.m_components.m_namespace == name->m_namespace.impl();
72 }
translateWebCore::QNameComponentsTranslator73 static void translate(QualifiedName::QualifiedNameImpl*& location, const QualifiedNameData& data, unsigned)
74 {
75 const QualifiedNameComponents& components = data.m_components;
76 location = QualifiedName::QualifiedNameImpl::create(AtomicString(components.m_prefix), AtomicString(components.m_localName), AtomicString(components.m_namespace), data.m_isStatic).leakRef();
77 }
78 };
79
QualifiedName(const AtomicString & p,const AtomicString & l,const AtomicString & n)80 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n)
81 {
82 QualifiedNameData data = { { p.impl(), l.impl(), n.isEmpty() ? nullAtom.impl() : n.impl() }, false };
83 QualifiedNameCache::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(data);
84 m_impl = addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.storedValue;
85 }
86
QualifiedName(const AtomicString & p,const AtomicString & l,const AtomicString & n,bool isStatic)87 QualifiedName::QualifiedName(const AtomicString& p, const AtomicString& l, const AtomicString& n, bool isStatic)
88 {
89 QualifiedNameData data = { { p.impl(), l.impl(), n.impl() }, isStatic };
90 QualifiedNameCache::AddResult addResult = qualifiedNameCache().add<QNameComponentsTranslator>(data);
91 m_impl = addResult.isNewEntry ? adoptRef(*addResult.storedValue) : *addResult.storedValue;
92 }
93
~QualifiedName()94 QualifiedName::~QualifiedName()
95 {
96 }
97
~QualifiedNameImpl()98 QualifiedName::QualifiedNameImpl::~QualifiedNameImpl()
99 {
100 qualifiedNameCache().remove(this);
101 }
102
toString() const103 String QualifiedName::toString() const
104 {
105 String local = localName();
106 if (hasPrefix())
107 return prefix().string() + ":" + local;
108 return local;
109 }
110
111 // Global init routines
DEFINE_GLOBAL(QualifiedName,anyName,nullAtom,starAtom,starAtom)112 DEFINE_GLOBAL(QualifiedName, anyName, nullAtom, starAtom, starAtom)
113
114 void QualifiedName::init()
115 {
116 ASSERT(starAtom.impl());
117 new ((void*)&anyName) QualifiedName(nullAtom, starAtom, starAtom, true );
118 }
119
null()120 const QualifiedName& QualifiedName::null()
121 {
122 DEFINE_STATIC_LOCAL(QualifiedName, nullName, (nullAtom, nullAtom, nullAtom, true));
123 return nullName;
124 }
125
localNameUpper() const126 const AtomicString& QualifiedName::localNameUpper() const
127 {
128 if (!m_impl->m_localNameUpper)
129 m_impl->m_localNameUpper = m_impl->m_localName.upper();
130 return m_impl->m_localNameUpper;
131 }
132
computeHash() const133 unsigned QualifiedName::QualifiedNameImpl::computeHash() const
134 {
135 QualifiedNameComponents components = { m_prefix.impl(), m_localName.impl(), m_namespace.impl() };
136 return hashComponents(components);
137 }
138
createStatic(void * targetAddress,StringImpl * name,const AtomicString & nameNamespace)139 void QualifiedName::createStatic(void* targetAddress, StringImpl* name, const AtomicString& nameNamespace)
140 {
141 new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nameNamespace, true);
142 }
143
createStatic(void * targetAddress,StringImpl * name)144 void QualifiedName::createStatic(void* targetAddress, StringImpl* name)
145 {
146 new (targetAddress) QualifiedName(nullAtom, AtomicString(name), nullAtom, true);
147 }
148
149 }
150