1 | /* |
2 | * Copyright (C) 2010 Google, Inc. All Rights Reserved. |
3 | * Copyright (C) 2011 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * |
14 | * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY |
15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR |
18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | */ |
26 | |
27 | #pragma once |
28 | |
29 | #include "HTMLStackItem.h" |
30 | #include <wtf/Forward.h> |
31 | #include <wtf/Noncopyable.h> |
32 | #include <wtf/Ref.h> |
33 | |
34 | namespace WebCore { |
35 | |
36 | class ContainerNode; |
37 | class Element; |
38 | class QualifiedName; |
39 | |
40 | // NOTE: The HTML5 spec uses a backwards (grows downward) stack. We're using |
41 | // more standard (grows upwards) stack terminology here. |
42 | class HTMLElementStack { |
43 | WTF_MAKE_NONCOPYABLE(HTMLElementStack); WTF_MAKE_FAST_ALLOCATED; |
44 | public: |
45 | HTMLElementStack() = default; |
46 | ~HTMLElementStack(); |
47 | |
48 | class ElementRecord { |
49 | WTF_MAKE_NONCOPYABLE(ElementRecord); WTF_MAKE_FAST_ALLOCATED; |
50 | public: |
51 | ElementRecord(Ref<HTMLStackItem>&&, std::unique_ptr<ElementRecord>); |
52 | ~ElementRecord(); |
53 | |
54 | Element& element() const { return m_item->element(); } |
55 | ContainerNode& node() const { return m_item->node(); } |
56 | const AtomicString& namespaceURI() const { return m_item->namespaceURI(); } |
57 | HTMLStackItem& stackItem() { return m_item.get(); } |
58 | const HTMLStackItem& stackItem() const { return m_item.get(); } |
59 | |
60 | void replaceElement(Ref<HTMLStackItem>&&); |
61 | |
62 | bool isAbove(ElementRecord&) const; |
63 | |
64 | ElementRecord* next() const { return m_next.get(); } |
65 | |
66 | private: |
67 | friend class HTMLElementStack; |
68 | |
69 | std::unique_ptr<ElementRecord> releaseNext() { return WTFMove(m_next); } |
70 | void setNext(std::unique_ptr<ElementRecord> next) { m_next = WTFMove(next); } |
71 | |
72 | Ref<HTMLStackItem> m_item; |
73 | std::unique_ptr<ElementRecord> m_next; |
74 | }; |
75 | |
76 | unsigned stackDepth() const { return m_stackDepth; } |
77 | |
78 | // Inlining this function is a (small) performance win on the parsing |
79 | // benchmark. |
80 | Element& top() const |
81 | { |
82 | return m_top->element(); |
83 | } |
84 | |
85 | ContainerNode& topNode() const |
86 | { |
87 | return m_top->node(); |
88 | } |
89 | |
90 | HTMLStackItem& topStackItem() const |
91 | { |
92 | return m_top->stackItem(); |
93 | } |
94 | |
95 | HTMLStackItem* oneBelowTop() const; |
96 | ElementRecord& topRecord() const; |
97 | ElementRecord* find(Element&) const; |
98 | ElementRecord* furthestBlockForFormattingElement(Element&) const; |
99 | ElementRecord* topmost(const AtomicString& tagName) const; |
100 | |
101 | void insertAbove(Ref<HTMLStackItem>&&, ElementRecord&); |
102 | |
103 | void push(Ref<HTMLStackItem>&&); |
104 | void pushRootNode(Ref<HTMLStackItem>&&); |
105 | void pushHTMLHtmlElement(Ref<HTMLStackItem>&&); |
106 | void pushHTMLHeadElement(Ref<HTMLStackItem>&&); |
107 | void pushHTMLBodyElement(Ref<HTMLStackItem>&&); |
108 | |
109 | void pop(); |
110 | void popUntil(const AtomicString& tagName); |
111 | void popUntil(Element&); |
112 | void popUntilPopped(const AtomicString& tagName); |
113 | void popUntilPopped(const QualifiedName& tagName) { popUntilPopped(tagName.localName()); } |
114 | |
115 | void popUntilPopped(Element&); |
116 | void (); |
117 | void popUntilTableScopeMarker(); // "clear the stack back to a table context" in the spec. |
118 | void popUntilTableBodyScopeMarker(); // "clear the stack back to a table body context" in the spec. |
119 | void popUntilTableRowScopeMarker(); // "clear the stack back to a table row context" in the spec. |
120 | void popUntilForeignContentScopeMarker(); |
121 | void popHTMLHeadElement(); |
122 | void popHTMLBodyElement(); |
123 | void popAll(); |
124 | |
125 | static bool isMathMLTextIntegrationPoint(HTMLStackItem&); |
126 | static bool isHTMLIntegrationPoint(HTMLStackItem&); |
127 | |
128 | void remove(Element&); |
129 | void removeHTMLHeadElement(Element&); |
130 | |
131 | bool contains(Element&) const; |
132 | bool contains(const AtomicString& tagName) const; |
133 | |
134 | bool inScope(Element&) const; |
135 | bool inScope(const AtomicString& tagName) const; |
136 | bool inScope(const QualifiedName&) const; |
137 | bool inListItemScope(const AtomicString& tagName) const; |
138 | bool inListItemScope(const QualifiedName&) const; |
139 | bool inTableScope(const AtomicString& tagName) const; |
140 | bool inTableScope(const QualifiedName&) const; |
141 | bool inButtonScope(const AtomicString& tagName) const; |
142 | bool inButtonScope(const QualifiedName&) const; |
143 | bool inSelectScope(const AtomicString& tagName) const; |
144 | bool inSelectScope(const QualifiedName&) const; |
145 | |
146 | bool () const; |
147 | |
148 | bool hasOnlyOneElement() const; |
149 | bool secondElementIsHTMLBodyElement() const; |
150 | bool hasTemplateInHTMLScope() const; |
151 | Element& htmlElement() const; |
152 | Element& headElement() const; |
153 | Element& bodyElement() const; |
154 | |
155 | ContainerNode& rootNode() const; |
156 | |
157 | #if ENABLE(TREE_DEBUGGING) |
158 | void show(); |
159 | #endif |
160 | |
161 | private: |
162 | void pushCommon(Ref<HTMLStackItem>&&); |
163 | void pushRootNodeCommon(Ref<HTMLStackItem>&&); |
164 | void popCommon(); |
165 | void removeNonTopCommon(Element&); |
166 | |
167 | std::unique_ptr<ElementRecord> m_top; |
168 | |
169 | // We remember the root node, <head> and <body> as they are pushed. Their |
170 | // ElementRecords keep them alive. The root node is never popped. |
171 | // FIXME: We don't currently require type-specific information about |
172 | // these elements so we haven't yet bothered to plumb the types all the |
173 | // way down through createElement, etc. |
174 | ContainerNode* m_rootNode { nullptr }; |
175 | Element* m_headElement { nullptr }; |
176 | Element* m_bodyElement { nullptr }; |
177 | unsigned m_stackDepth { 0 }; |
178 | }; |
179 | |
180 | } // namespace WebCore |
181 | |