#ifndef HTMLConstructionSite_h
#define HTMLConstructionSite_h
#include "core/dom/ParserContentPolicy.h"
#include "core/html/parser/HTMLElementStack.h"
#include "core/html/parser/HTMLFormattingElementList.h"
#include "wtf/Noncopyable.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include "wtf/text/StringBuilder.h"
namespace WebCore {
struct HTMLConstructionSiteTask {
enum Operation {
Insert,
InsertText,
InsertAlreadyParsedChild,
Reparent,
TakeAllChildren,
};
explicit HTMLConstructionSiteTask(Operation op)
: operation(op)
, selfClosing(false)
{
}
ContainerNode* oldParent()
{
return toContainerNode(child.get());
}
Operation operation;
RefPtr<ContainerNode> parent;
RefPtr<Node> nextChild;
RefPtr<Node> child;
bool selfClosing;
};
}
namespace WTF {
template<> struct VectorTraits<WebCore::HTMLConstructionSiteTask> : SimpleClassVectorTraits<WebCore::HTMLConstructionSiteTask> { };
}
namespace WebCore {
enum WhitespaceMode {
WhitespaceUnknown,
NotAllWhitespace,
AllWhitespace,
};
class AtomicHTMLToken;
class Document;
class Element;
class HTMLFormElement;
class HTMLConstructionSite {
WTF_MAKE_NONCOPYABLE(HTMLConstructionSite);
public:
HTMLConstructionSite(Document*, ParserContentPolicy);
HTMLConstructionSite(DocumentFragment*, ParserContentPolicy);
~HTMLConstructionSite();
void detach();
void executeQueuedTasks();
void flushPendingText();
void flush()
{
if (!hasPendingTasks())
return;
flushPendingText();
executeQueuedTasks();
ASSERT(!hasPendingTasks());
}
bool hasPendingTasks()
{
return !m_pendingText.isEmpty() || !m_taskQueue.isEmpty();
}
void setDefaultCompatibilityMode();
void processEndOfFile();
void finishedParsing();
void insertDoctype(AtomicHTMLToken*);
void insertComment(AtomicHTMLToken*);
void insertCommentOnDocument(AtomicHTMLToken*);
void insertCommentOnHTMLHtmlElement(AtomicHTMLToken*);
void insertHTMLElement(AtomicHTMLToken*);
void insertSelfClosingHTMLElement(AtomicHTMLToken*);
void insertFormattingElement(AtomicHTMLToken*);
void insertHTMLHeadElement(AtomicHTMLToken*);
void insertHTMLBodyElement(AtomicHTMLToken*);
void insertHTMLFormElement(AtomicHTMLToken*, bool isDemoted = false);
void insertScriptElement(AtomicHTMLToken*);
void insertTextNode(const String&, WhitespaceMode = WhitespaceUnknown);
void insertForeignElement(AtomicHTMLToken*, const AtomicString& namespaceURI);
void insertHTMLHtmlStartTagBeforeHTML(AtomicHTMLToken*);
void insertHTMLHtmlStartTagInBody(AtomicHTMLToken*);
void insertHTMLBodyStartTagInBody(AtomicHTMLToken*);
void reparent(HTMLElementStack::ElementRecord* newParent, HTMLElementStack::ElementRecord* child);
void reparent(HTMLElementStack::ElementRecord* newParent, HTMLStackItem* child);
void insertAlreadyParsedChild(HTMLStackItem* newParent, HTMLElementStack::ElementRecord* child);
void takeAllChildren(HTMLStackItem* newParent, HTMLElementStack::ElementRecord* oldParent);
PassRefPtr<HTMLStackItem> createElementFromSavedToken(HTMLStackItem*);
bool shouldFosterParent() const;
void fosterParent(PassRefPtr<Node>);
bool indexOfFirstUnopenFormattingElement(unsigned& firstUnopenElementIndex) const;
void reconstructTheActiveFormattingElements();
void generateImpliedEndTags();
void generateImpliedEndTagsWithExclusion(const AtomicString& tagName);
bool inQuirksMode();
bool isEmpty() const { return !m_openElements.stackDepth(); }
HTMLElementStack::ElementRecord* currentElementRecord() const { return m_openElements.topRecord(); }
Element* currentElement() const { return m_openElements.top(); }
ContainerNode* currentNode() const { return m_openElements.topNode(); }
HTMLStackItem* currentStackItem() const { return m_openElements.topStackItem(); }
HTMLStackItem* oneBelowTop() const { return m_openElements.oneBelowTop(); }
Document& ownerDocumentForCurrentNode();
HTMLElementStack* openElements() const { return &m_openElements; }
HTMLFormattingElementList* activeFormattingElements() const { return &m_activeFormattingElements; }
bool currentIsRootNode() { return m_openElements.topNode() == m_openElements.rootNode(); }
Element* head() const { return m_head->element(); }
HTMLStackItem* headStackItem() const { return m_head.get(); }
void setForm(HTMLFormElement*);
HTMLFormElement* form() const { return m_form.get(); }
PassRefPtr<HTMLFormElement> takeForm();
ParserContentPolicy parserContentPolicy() { return m_parserContentPolicy; }
class RedirectToFosterParentGuard {
WTF_MAKE_NONCOPYABLE(RedirectToFosterParentGuard);
public:
RedirectToFosterParentGuard(HTMLConstructionSite& tree)
: m_tree(tree)
, m_wasRedirectingBefore(tree.m_redirectAttachToFosterParent)
{
m_tree.m_redirectAttachToFosterParent = true;
}
~RedirectToFosterParentGuard()
{
m_tree.m_redirectAttachToFosterParent = m_wasRedirectingBefore;
}
private:
HTMLConstructionSite& m_tree;
bool m_wasRedirectingBefore;
};
private:
typedef Vector<HTMLConstructionSiteTask, 1> TaskQueue;
void setCompatibilityMode(Document::CompatibilityMode);
void setCompatibilityModeFromDoctype(const String& name, const String& publicId, const String& systemId);
void attachLater(ContainerNode* parent, PassRefPtr<Node> child, bool selfClosing = false);
void findFosterSite(HTMLConstructionSiteTask&);
PassRefPtr<Element> createHTMLElement(AtomicHTMLToken*);
PassRefPtr<Element> createElement(AtomicHTMLToken*, const AtomicString& namespaceURI);
void mergeAttributesFromTokenIntoElement(AtomicHTMLToken*, Element*);
void dispatchDocumentElementAvailableIfNeeded();
void executeTask(HTMLConstructionSiteTask&);
void queueTask(const HTMLConstructionSiteTask&);
Document* m_document;
ContainerNode* m_attachmentRoot;
RefPtr<HTMLStackItem> m_head;
RefPtr<HTMLFormElement> m_form;
mutable HTMLElementStack m_openElements;
mutable HTMLFormattingElementList m_activeFormattingElements;
TaskQueue m_taskQueue;
struct PendingText {
PendingText()
: whitespaceMode(WhitespaceUnknown)
{
}
void append(PassRefPtr<ContainerNode> newParent, PassRefPtr<Node> newNextChild, const String& newString, WhitespaceMode newWhitespaceMode)
{
ASSERT(!parent || parent == newParent);
parent = newParent;
ASSERT(!nextChild || nextChild == newNextChild);
nextChild = newNextChild;
stringBuilder.append(newString);
whitespaceMode = std::min(whitespaceMode, newWhitespaceMode);
}
void swap(PendingText& other)
{
std::swap(whitespaceMode, other.whitespaceMode);
parent.swap(other.parent);
nextChild.swap(other.nextChild);
stringBuilder.swap(other.stringBuilder);
}
void discard()
{
PendingText discardedText;
swap(discardedText);
}
bool isEmpty()
{
ASSERT(stringBuilder.isEmpty() == !parent);
ASSERT(!stringBuilder.isEmpty() || !nextChild);
ASSERT(!stringBuilder.isEmpty() || (whitespaceMode == WhitespaceUnknown));
return stringBuilder.isEmpty();
}
RefPtr<ContainerNode> parent;
RefPtr<Node> nextChild;
StringBuilder stringBuilder;
WhitespaceMode whitespaceMode;
};
PendingText m_pendingText;
ParserContentPolicy m_parserContentPolicy;
bool m_isParsingFragment;
bool m_redirectAttachToFosterParent;
bool m_inQuirksMode;
};
}
#endif