#ifndef HTMLTreeBuilder_h
#define HTMLTreeBuilder_h
#include "core/html/parser/HTMLConstructionSite.h"
#include "core/html/parser/HTMLElementStack.h"
#include "core/html/parser/HTMLParserOptions.h"
#include "wtf/Noncopyable.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include "wtf/text/StringBuilder.h"
#include "wtf/text/TextPosition.h"
namespace WebCore {
class AtomicHTMLToken;
class Document;
class DocumentFragment;
class Element;
class LocalFrame;
class HTMLToken;
class HTMLDocument;
class Node;
class HTMLDocumentParser;
class HTMLTreeBuilder {
WTF_MAKE_NONCOPYABLE(HTMLTreeBuilder); WTF_MAKE_FAST_ALLOCATED;
public:
static PassOwnPtr<HTMLTreeBuilder> create(HTMLDocumentParser* parser, HTMLDocument* document, ParserContentPolicy parserContentPolicy, bool reportErrors, const HTMLParserOptions& options)
{
return adoptPtr(new HTMLTreeBuilder(parser, document, parserContentPolicy, reportErrors, options));
}
static PassOwnPtr<HTMLTreeBuilder> create(HTMLDocumentParser* parser, DocumentFragment* fragment, Element* contextElement, ParserContentPolicy parserContentPolicy, const HTMLParserOptions& options)
{
return adoptPtr(new HTMLTreeBuilder(parser, fragment, contextElement, parserContentPolicy, options));
}
~HTMLTreeBuilder();
const HTMLElementStack* openElements() const { return m_tree.openElements(); }
bool isParsingFragment() const { return !!m_fragmentContext.fragment(); }
bool isParsingTemplateContents() const { return m_tree.openElements()->hasTemplateInHTMLScope(); }
bool isParsingFragmentOrTemplateContents() const { return isParsingFragment() || isParsingTemplateContents(); }
void detach();
void constructTree(AtomicHTMLToken*);
bool hasParserBlockingScript() const { return !!m_scriptToProcess; }
PassRefPtr<Element> takeScriptToProcess(TextPosition& scriptStartPosition);
void finished();
void flush() { m_tree.flush(); }
void setShouldSkipLeadingNewline(bool shouldSkip) { m_shouldSkipLeadingNewline = shouldSkip; }
private:
class CharacterTokenBuffer;
enum InsertionMode {
InitialMode,
BeforeHTMLMode,
BeforeHeadMode,
InHeadMode,
InHeadNoscriptMode,
AfterHeadMode,
TemplateContentsMode,
InBodyMode,
TextMode,
InTableMode,
InTableTextMode,
InCaptionMode,
InColumnGroupMode,
InTableBodyMode,
InRowMode,
InCellMode,
InSelectMode,
InSelectInTableMode,
AfterBodyMode,
InFramesetMode,
AfterFramesetMode,
AfterAfterBodyMode,
AfterAfterFramesetMode,
};
HTMLTreeBuilder(HTMLDocumentParser*, HTMLDocument*, ParserContentPolicy, bool reportErrors, const HTMLParserOptions&);
HTMLTreeBuilder(HTMLDocumentParser*, DocumentFragment*, Element* contextElement, ParserContentPolicy, const HTMLParserOptions&);
void processToken(AtomicHTMLToken*);
void processDoctypeToken(AtomicHTMLToken*);
void processStartTag(AtomicHTMLToken*);
void processEndTag(AtomicHTMLToken*);
void processComment(AtomicHTMLToken*);
void processCharacter(AtomicHTMLToken*);
void processEndOfFile(AtomicHTMLToken*);
bool processStartTagForInHead(AtomicHTMLToken*);
void processStartTagForInBody(AtomicHTMLToken*);
void processStartTagForInTable(AtomicHTMLToken*);
void processEndTagForInBody(AtomicHTMLToken*);
void processEndTagForInTable(AtomicHTMLToken*);
void processEndTagForInTableBody(AtomicHTMLToken*);
void processEndTagForInRow(AtomicHTMLToken*);
void processEndTagForInCell(AtomicHTMLToken*);
void processIsindexStartTagForInBody(AtomicHTMLToken*);
void processHtmlStartTagForInBody(AtomicHTMLToken*);
bool processBodyEndTagForInBody(AtomicHTMLToken*);
bool processTableEndTagForInTable();
bool processCaptionEndTagForInCaption();
bool processColgroupEndTagForInColumnGroup();
bool processTrEndTagForInRow();
void processAnyOtherEndTagForInBody(AtomicHTMLToken*);
void processCharacterBuffer(CharacterTokenBuffer&);
inline void processCharacterBufferForInBody(CharacterTokenBuffer&);
void processFakeStartTag(const QualifiedName&, const Vector<Attribute>& attributes = Vector<Attribute>());
void processFakeEndTag(const QualifiedName&);
void processFakeEndTag(const AtomicString&);
void processFakePEndTagIfPInButtonScope();
void processGenericRCDATAStartTag(AtomicHTMLToken*);
void processGenericRawTextStartTag(AtomicHTMLToken*);
void processScriptStartTag(AtomicHTMLToken*);
void defaultForInitial();
void defaultForBeforeHTML();
void defaultForBeforeHead();
void defaultForInHead();
void defaultForInHeadNoscript();
void defaultForAfterHead();
void defaultForInTableText();
inline HTMLStackItem* adjustedCurrentStackItem() const;
inline bool shouldProcessTokenInForeignContent(AtomicHTMLToken*);
void processTokenInForeignContent(AtomicHTMLToken*);
Vector<Attribute> attributesForIsindexInput(AtomicHTMLToken*);
void callTheAdoptionAgency(AtomicHTMLToken*);
void closeTheCell();
template <bool shouldClose(const HTMLStackItem*)>
void processCloseWhenNestedTag(AtomicHTMLToken*);
void parseError(AtomicHTMLToken*);
InsertionMode insertionMode() const { return m_insertionMode; }
void setInsertionMode(InsertionMode mode) { m_insertionMode = mode; }
void resetInsertionModeAppropriately();
void processTemplateStartTag(AtomicHTMLToken*);
bool processTemplateEndTag(AtomicHTMLToken*);
bool processEndOfFileForInTemplateContents(AtomicHTMLToken*);
class FragmentParsingContext {
WTF_MAKE_NONCOPYABLE(FragmentParsingContext);
public:
FragmentParsingContext();
FragmentParsingContext(DocumentFragment*, Element* contextElement);
~FragmentParsingContext();
DocumentFragment* fragment() const { return m_fragment; }
Element* contextElement() const { ASSERT(m_fragment); return m_contextElementStackItem->element(); }
HTMLStackItem* contextElementStackItem() const { ASSERT(m_fragment); return m_contextElementStackItem.get(); }
private:
DocumentFragment* m_fragment;
RefPtr<HTMLStackItem> m_contextElementStackItem;
};
bool m_framesetOk;
#ifndef NDEBUG
bool m_isAttached;
#endif
FragmentParsingContext m_fragmentContext;
HTMLConstructionSite m_tree;
InsertionMode m_insertionMode;
InsertionMode m_originalInsertionMode;
Vector<InsertionMode> m_templateInsertionModes;
StringBuilder m_pendingTableCharacters;
bool m_shouldSkipLeadingNewline;
HTMLDocumentParser* m_parser;
RefPtr<Element> m_scriptToProcess;
TextPosition m_scriptToProcessStartPosition;
HTMLParserOptions m_options;
};
}
#endif