2 * Copyright (C) 2010 Google, Inc. All Rights Reserved.
3 * Copyright (C) 2011 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #ifndef HTMLConstructionSite_h
28 #define HTMLConstructionSite_h
30 #include "core/dom/ParserContentPolicy.h"
31 #include "core/html/parser/HTMLElementStack.h"
32 #include "core/html/parser/HTMLFormattingElementList.h"
33 #include "wtf/Noncopyable.h"
34 #include "wtf/PassRefPtr.h"
35 #include "wtf/RefPtr.h"
36 #include "wtf/Vector.h"
37 #include "wtf/text/StringBuilder.h"
41 struct HTMLConstructionSiteTask {
44 InsertText, // Handles possible merging of text nodes.
45 InsertAlreadyParsedChild, // Insert w/o calling begin/end parsing.
50 explicit HTMLConstructionSiteTask(Operation op)
56 ContainerNode* oldParent()
58 // It's sort of ugly, but we store the |oldParent| in the |child| field
59 // of the task so that we don't bloat the HTMLConstructionSiteTask
60 // object in the common case of the Insert operation.
61 return toContainerNode(child.get());
65 RefPtr<ContainerNode> parent;
66 RefPtr<Node> nextChild;
71 } // namespace WebCore
74 template<> struct VectorTraits<WebCore::HTMLConstructionSiteTask> : SimpleClassVectorTraits { };
79 // Note: These are intentionally ordered so that when we concatonate
80 // strings and whitespaces the resulting whitespace is ws = min(ws1, ws2).
87 class AtomicHTMLToken;
90 class HTMLFormElement;
92 class HTMLConstructionSite {
93 WTF_MAKE_NONCOPYABLE(HTMLConstructionSite);
95 HTMLConstructionSite(Document*, ParserContentPolicy);
96 HTMLConstructionSite(DocumentFragment*, ParserContentPolicy);
97 ~HTMLConstructionSite();
101 // executeQueuedTasks empties the queue but does not flush pending text.
102 // NOTE: Possible reentrancy via JavaScript execution.
103 void executeQueuedTasks();
105 // flushPendingText turns pending text into queued Text insertions, but does not execute them.
106 void flushPendingText();
108 // Called before every token in HTMLTreeBuilder::processToken, thus inlined:
111 if (!hasPendingTasks())
114 executeQueuedTasks(); // NOTE: Possible reentrancy via JavaScript execution.
115 ASSERT(!hasPendingTasks());
118 bool hasPendingTasks()
120 return !m_pendingText.isEmpty() || !m_taskQueue.isEmpty();
123 void setDefaultCompatibilityMode();
124 void processEndOfFile();
125 void finishedParsing();
127 void insertDoctype(AtomicHTMLToken*);
128 void insertComment(AtomicHTMLToken*);
129 void insertCommentOnDocument(AtomicHTMLToken*);
130 void insertCommentOnHTMLHtmlElement(AtomicHTMLToken*);
131 void insertHTMLElement(AtomicHTMLToken*);
132 void insertSelfClosingHTMLElement(AtomicHTMLToken*);
133 void insertFormattingElement(AtomicHTMLToken*);
134 void insertHTMLHeadElement(AtomicHTMLToken*);
135 void insertHTMLBodyElement(AtomicHTMLToken*);
136 void insertHTMLFormElement(AtomicHTMLToken*, bool isDemoted = false);
137 void insertScriptElement(AtomicHTMLToken*);
138 void insertTextNode(const String&, WhitespaceMode = WhitespaceUnknown);
139 void insertForeignElement(AtomicHTMLToken*, const AtomicString& namespaceURI);
141 void insertHTMLHtmlStartTagBeforeHTML(AtomicHTMLToken*);
142 void insertHTMLHtmlStartTagInBody(AtomicHTMLToken*);
143 void insertHTMLBodyStartTagInBody(AtomicHTMLToken*);
145 void reparent(HTMLElementStack::ElementRecord* newParent, HTMLElementStack::ElementRecord* child);
146 void reparent(HTMLElementStack::ElementRecord* newParent, HTMLStackItem* child);
147 // insertAlreadyParsedChild assumes that |child| has already been parsed (i.e., we're just
148 // moving it around in the tree rather than parsing it for the first time). That means
149 // this function doesn't call beginParsingChildren / finishParsingChildren.
150 void insertAlreadyParsedChild(HTMLStackItem* newParent, HTMLElementStack::ElementRecord* child);
151 void takeAllChildren(HTMLStackItem* newParent, HTMLElementStack::ElementRecord* oldParent);
153 PassRefPtr<HTMLStackItem> createElementFromSavedToken(HTMLStackItem*);
155 bool shouldFosterParent() const;
156 void fosterParent(PassRefPtr<Node>);
158 bool indexOfFirstUnopenFormattingElement(unsigned& firstUnopenElementIndex) const;
159 void reconstructTheActiveFormattingElements();
161 void generateImpliedEndTags();
162 void generateImpliedEndTagsWithExclusion(const AtomicString& tagName);
166 bool isEmpty() const { return !m_openElements.stackDepth(); }
167 HTMLElementStack::ElementRecord* currentElementRecord() const { return m_openElements.topRecord(); }
168 Element* currentElement() const { return m_openElements.top(); }
169 ContainerNode* currentNode() const { return m_openElements.topNode(); }
170 HTMLStackItem* currentStackItem() const { return m_openElements.topStackItem(); }
171 HTMLStackItem* oneBelowTop() const { return m_openElements.oneBelowTop(); }
172 Document& ownerDocumentForCurrentNode();
173 HTMLElementStack* openElements() const { return &m_openElements; }
174 HTMLFormattingElementList* activeFormattingElements() const { return &m_activeFormattingElements; }
175 bool currentIsRootNode() { return m_openElements.topNode() == m_openElements.rootNode(); }
177 Element* head() const { return m_head->element(); }
178 HTMLStackItem* headStackItem() const { return m_head.get(); }
180 void setForm(HTMLFormElement*);
181 HTMLFormElement* form() const { return m_form.get(); }
182 PassRefPtr<HTMLFormElement> takeForm();
184 ParserContentPolicy parserContentPolicy() { return m_parserContentPolicy; }
186 class RedirectToFosterParentGuard {
187 WTF_MAKE_NONCOPYABLE(RedirectToFosterParentGuard);
189 RedirectToFosterParentGuard(HTMLConstructionSite& tree)
191 , m_wasRedirectingBefore(tree.m_redirectAttachToFosterParent)
193 m_tree.m_redirectAttachToFosterParent = true;
196 ~RedirectToFosterParentGuard()
198 m_tree.m_redirectAttachToFosterParent = m_wasRedirectingBefore;
202 HTMLConstructionSite& m_tree;
203 bool m_wasRedirectingBefore;
207 // In the common case, this queue will have only one task because most
208 // tokens produce only one DOM mutation.
209 typedef Vector<HTMLConstructionSiteTask, 1> TaskQueue;
211 void setCompatibilityMode(Document::CompatibilityMode);
212 void setCompatibilityModeFromDoctype(const String& name, const String& publicId, const String& systemId);
214 void attachLater(ContainerNode* parent, PassRefPtr<Node> child, bool selfClosing = false);
216 void findFosterSite(HTMLConstructionSiteTask&);
218 PassRefPtr<Element> createHTMLElement(AtomicHTMLToken*);
219 PassRefPtr<Element> createElement(AtomicHTMLToken*, const AtomicString& namespaceURI);
221 void mergeAttributesFromTokenIntoElement(AtomicHTMLToken*, Element*);
222 void dispatchDocumentElementAvailableIfNeeded();
224 void executeTask(HTMLConstructionSiteTask&);
225 void queueTask(const HTMLConstructionSiteTask&);
227 Document* m_document;
229 // This is the root ContainerNode to which the parser attaches all newly
230 // constructed nodes. It points to a DocumentFragment when parsing fragments
231 // and a Document in all other cases.
232 ContainerNode* m_attachmentRoot;
234 RefPtr<HTMLStackItem> m_head;
235 RefPtr<HTMLFormElement> m_form;
236 mutable HTMLElementStack m_openElements;
237 mutable HTMLFormattingElementList m_activeFormattingElements;
239 TaskQueue m_taskQueue;
243 : whitespaceMode(WhitespaceUnknown)
247 void append(PassRefPtr<ContainerNode> newParent, PassRefPtr<Node> newNextChild, const String& newString, WhitespaceMode newWhitespaceMode)
249 ASSERT(!parent || parent == newParent);
251 ASSERT(!nextChild || nextChild == newNextChild);
252 nextChild = newNextChild;
253 stringBuilder.append(newString);
254 whitespaceMode = std::min(whitespaceMode, newWhitespaceMode);
257 void swap(PendingText& other)
259 std::swap(whitespaceMode, other.whitespaceMode);
260 parent.swap(other.parent);
261 nextChild.swap(other.nextChild);
262 stringBuilder.swap(other.stringBuilder);
267 PendingText discardedText;
273 // When the stringbuilder is empty, the parent and whitespace should also be "empty".
274 ASSERT(stringBuilder.isEmpty() == !parent);
275 ASSERT(!stringBuilder.isEmpty() || !nextChild);
276 ASSERT(!stringBuilder.isEmpty() || (whitespaceMode == WhitespaceUnknown));
277 return stringBuilder.isEmpty();
280 RefPtr<ContainerNode> parent;
281 RefPtr<Node> nextChild;
282 StringBuilder stringBuilder;
283 WhitespaceMode whitespaceMode;
286 PendingText m_pendingText;
288 ParserContentPolicy m_parserContentPolicy;
289 bool m_isParsingFragment;
291 // http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html#parsing-main-intable
292 // In the "in table" insertion mode, we sometimes get into a state where
293 // "whenever a node would be inserted into the current node, it must instead
294 // be foster parented." This flag tracks whether we're in that state.
295 bool m_redirectAttachToFosterParent;
300 } // namespace WebCore