2 * Copyright (C) 2012 Company 100, 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 GOOGLE 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 GOOGLE 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.
26 #ifndef HTMLStackItem_h
27 #define HTMLStackItem_h
29 #include "core/HTMLNames.h"
30 #include "core/MathMLNames.h"
31 #include "core/SVGNames.h"
32 #include "core/dom/Element.h"
33 #include "core/html/parser/AtomicHTMLToken.h"
34 #include "platform/RuntimeEnabledFeatures.h"
35 #include "wtf/RefCounted.h"
36 #include "wtf/RefPtr.h"
37 #include "wtf/text/AtomicString.h"
43 class HTMLStackItem
: public RefCountedWillBeGarbageCollectedFinalized
<HTMLStackItem
> {
46 ItemForContextElement
,
47 ItemForDocumentFragmentNode
50 // Used by document fragment node and context element.
51 static PassRefPtrWillBeRawPtr
<HTMLStackItem
> create(PassRefPtrWillBeRawPtr
<ContainerNode
> node
, ItemType type
)
53 return adoptRefWillBeNoop(new HTMLStackItem(node
, type
));
56 // Used by HTMLElementStack and HTMLFormattingElementList.
57 static PassRefPtrWillBeRawPtr
<HTMLStackItem
> create(PassRefPtrWillBeRawPtr
<ContainerNode
> node
, AtomicHTMLToken
* token
, const AtomicString
& namespaceURI
= HTMLNames::xhtmlNamespaceURI
)
59 return adoptRefWillBeNoop(new HTMLStackItem(node
, token
, namespaceURI
));
62 Element
* element() const { return toElement(m_node
.get()); }
63 ContainerNode
* node() const { return m_node
.get(); }
65 bool isDocumentFragmentNode() const { return m_isDocumentFragmentNode
; }
66 bool isElementNode() const { return !m_isDocumentFragmentNode
; }
68 const AtomicString
& namespaceURI() const { return m_namespaceURI
; }
69 const AtomicString
& localName() const { return m_tokenLocalName
; }
71 const Vector
<Attribute
>& attributes() const { ASSERT(m_tokenLocalName
); return m_tokenAttributes
; }
72 Attribute
* getAttributeItem(const QualifiedName
& attributeName
)
74 ASSERT(m_tokenLocalName
);
75 return findAttributeInVector(m_tokenAttributes
, attributeName
);
78 bool hasLocalName(const AtomicString
& name
) const { return m_tokenLocalName
== name
; }
79 bool hasTagName(const QualifiedName
& name
) const { return m_tokenLocalName
== name
.localName() && m_namespaceURI
== name
.namespaceURI(); }
81 bool matchesHTMLTag(const AtomicString
& name
) const { return m_tokenLocalName
== name
&& m_namespaceURI
== HTMLNames::xhtmlNamespaceURI
; }
82 bool matchesHTMLTag(const QualifiedName
& name
) const { return m_tokenLocalName
== name
&& m_namespaceURI
== HTMLNames::xhtmlNamespaceURI
; }
84 bool causesFosterParenting()
86 return hasTagName(HTMLNames::tableTag
)
87 || hasTagName(HTMLNames::tbodyTag
)
88 || hasTagName(HTMLNames::tfootTag
)
89 || hasTagName(HTMLNames::theadTag
)
90 || hasTagName(HTMLNames::trTag
);
93 bool isInHTMLNamespace() const
95 // A DocumentFragment takes the place of the document element when parsing
96 // fragments and should be considered in the HTML namespace.
97 return namespaceURI() == HTMLNames::xhtmlNamespaceURI
98 || isDocumentFragmentNode(); // FIXME: Does this also apply to ShadowRoot?
101 bool isNumberedHeaderElement() const
103 return hasTagName(HTMLNames::h1Tag
)
104 || hasTagName(HTMLNames::h2Tag
)
105 || hasTagName(HTMLNames::h3Tag
)
106 || hasTagName(HTMLNames::h4Tag
)
107 || hasTagName(HTMLNames::h5Tag
)
108 || hasTagName(HTMLNames::h6Tag
);
111 bool isTableBodyContextElement() const
113 return hasTagName(HTMLNames::tbodyTag
)
114 || hasTagName(HTMLNames::tfootTag
)
115 || hasTagName(HTMLNames::theadTag
);
118 // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#special
119 bool isSpecialNode() const
121 if (hasTagName(MathMLNames::miTag
)
122 || hasTagName(MathMLNames::moTag
)
123 || hasTagName(MathMLNames::mnTag
)
124 || hasTagName(MathMLNames::msTag
)
125 || hasTagName(MathMLNames::mtextTag
)
126 || hasTagName(MathMLNames::annotation_xmlTag
)
127 || hasTagName(SVGNames::foreignObjectTag
)
128 || hasTagName(SVGNames::descTag
)
129 || hasTagName(SVGNames::titleTag
))
131 if (isDocumentFragmentNode())
133 if (!isInHTMLNamespace())
135 const AtomicString
& tagName
= localName();
136 return tagName
== HTMLNames::addressTag
137 || tagName
== HTMLNames::areaTag
138 || tagName
== HTMLNames::articleTag
139 || tagName
== HTMLNames::asideTag
140 || tagName
== HTMLNames::baseTag
141 || tagName
== HTMLNames::basefontTag
142 || tagName
== HTMLNames::bgsoundTag
143 || tagName
== HTMLNames::blockquoteTag
144 || tagName
== HTMLNames::bodyTag
145 || tagName
== HTMLNames::brTag
146 || tagName
== HTMLNames::buttonTag
147 || tagName
== HTMLNames::captionTag
148 || tagName
== HTMLNames::centerTag
149 || tagName
== HTMLNames::colTag
150 || tagName
== HTMLNames::colgroupTag
151 || tagName
== HTMLNames::commandTag
152 || tagName
== HTMLNames::ddTag
153 || tagName
== HTMLNames::detailsTag
154 || tagName
== HTMLNames::dirTag
155 || tagName
== HTMLNames::divTag
156 || tagName
== HTMLNames::dlTag
157 || tagName
== HTMLNames::dtTag
158 || tagName
== HTMLNames::embedTag
159 || tagName
== HTMLNames::fieldsetTag
160 || tagName
== HTMLNames::figcaptionTag
161 || tagName
== HTMLNames::figureTag
162 || tagName
== HTMLNames::footerTag
163 || tagName
== HTMLNames::formTag
164 || tagName
== HTMLNames::frameTag
165 || tagName
== HTMLNames::framesetTag
166 || isNumberedHeaderElement()
167 || tagName
== HTMLNames::headTag
168 || tagName
== HTMLNames::headerTag
169 || tagName
== HTMLNames::hgroupTag
170 || tagName
== HTMLNames::hrTag
171 || tagName
== HTMLNames::htmlTag
172 || tagName
== HTMLNames::iframeTag
173 || tagName
== HTMLNames::imgTag
174 || tagName
== HTMLNames::inputTag
175 || tagName
== HTMLNames::liTag
176 || tagName
== HTMLNames::linkTag
177 || tagName
== HTMLNames::listingTag
178 || tagName
== HTMLNames::mainTag
179 || tagName
== HTMLNames::marqueeTag
180 || tagName
== HTMLNames::menuTag
181 || (RuntimeEnabledFeatures::contextMenuEnabled() && tagName
== HTMLNames::menuitemTag
)
182 || tagName
== HTMLNames::metaTag
183 || tagName
== HTMLNames::navTag
184 || tagName
== HTMLNames::noembedTag
185 || tagName
== HTMLNames::noframesTag
186 || tagName
== HTMLNames::noscriptTag
187 || tagName
== HTMLNames::objectTag
188 || tagName
== HTMLNames::olTag
189 || tagName
== HTMLNames::pTag
190 || tagName
== HTMLNames::paramTag
191 || tagName
== HTMLNames::plaintextTag
192 || tagName
== HTMLNames::preTag
193 || tagName
== HTMLNames::scriptTag
194 || tagName
== HTMLNames::sectionTag
195 || tagName
== HTMLNames::selectTag
196 || tagName
== HTMLNames::styleTag
197 || tagName
== HTMLNames::summaryTag
198 || tagName
== HTMLNames::tableTag
199 || isTableBodyContextElement()
200 || tagName
== HTMLNames::tdTag
201 || tagName
== HTMLNames::templateTag
202 || tagName
== HTMLNames::textareaTag
203 || tagName
== HTMLNames::thTag
204 || tagName
== HTMLNames::titleTag
205 || tagName
== HTMLNames::trTag
206 || tagName
== HTMLNames::ulTag
207 || tagName
== HTMLNames::wbrTag
208 || tagName
== HTMLNames::xmpTag
;
211 DEFINE_INLINE_TRACE() { visitor
->trace(m_node
); }
214 HTMLStackItem(PassRefPtrWillBeRawPtr
<ContainerNode
> node
, ItemType type
)
218 case ItemForDocumentFragmentNode
:
219 m_isDocumentFragmentNode
= true;
221 case ItemForContextElement
:
222 m_tokenLocalName
= element()->localName();
223 m_namespaceURI
= element()->namespaceURI();
224 m_isDocumentFragmentNode
= false;
229 HTMLStackItem(PassRefPtrWillBeRawPtr
<ContainerNode
> node
, AtomicHTMLToken
* token
, const AtomicString
& namespaceURI
= HTMLNames::xhtmlNamespaceURI
)
231 , m_tokenLocalName(token
->name())
232 , m_tokenAttributes(token
->attributes())
233 , m_namespaceURI(namespaceURI
)
234 , m_isDocumentFragmentNode(false)
238 RefPtrWillBeMember
<ContainerNode
> m_node
;
240 AtomicString m_tokenLocalName
;
241 Vector
<Attribute
> m_tokenAttributes
;
242 AtomicString m_namespaceURI
;
243 bool m_isDocumentFragmentNode
;
248 #endif // HTMLStackItem_h