/* Copyright (C) 1997 Martin Jones (mjones@kde.org) (C) 1997 Torben Weis (weis@kde.org) (C) 1998 Waldo Bastian (bastian@kde.org) (C) 1999 Lars Knoll (knoll@kde.org) Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef HTMLParser_h #define HTMLParser_h #include "QualifiedName.h" #include #include #include "HTMLParserErrorCodes.h" namespace WebCore { class Document; class DocumentFragment; class HTMLDocument; class HTMLFormElement; class HTMLHeadElement; class HTMLMapElement; class Node; class Token; struct HTMLStackElem; /** * The parser for HTML. It receives a stream of tokens from the HTMLTokenizer, and * builds up the Document structure from it. */ class HTMLParser : Noncopyable { public: HTMLParser(HTMLDocument*, bool reportErrors); HTMLParser(DocumentFragment*); virtual ~HTMLParser(); /** * parses one token delivered by the tokenizer */ PassRefPtr parseToken(Token*); /** * tokenizer says it's not going to be sending us any more tokens */ void finished(); /** * resets the parser */ void reset(); bool skipMode() const { return !m_skipModeTag.isNull(); } private: void setCurrent(Node*); void derefCurrent(); void setSkipMode(const QualifiedName& qName) { m_skipModeTag = qName.localName(); } PassRefPtr getNode(Token*); bool bodyCreateErrorCheck(Token*, RefPtr&); bool canvasCreateErrorCheck(Token*, RefPtr&); bool commentCreateErrorCheck(Token*, RefPtr&); bool ddCreateErrorCheck(Token*, RefPtr&); bool dtCreateErrorCheck(Token*, RefPtr&); bool formCreateErrorCheck(Token*, RefPtr&); bool framesetCreateErrorCheck(Token*, RefPtr&); bool headCreateErrorCheck(Token*, RefPtr&); bool iframeCreateErrorCheck(Token*, RefPtr&); bool isindexCreateErrorCheck(Token*, RefPtr&); bool mapCreateErrorCheck(Token*, RefPtr&); bool nestedCreateErrorCheck(Token*, RefPtr&); bool nestedStyleCreateErrorCheck(Token*, RefPtr&); bool noembedCreateErrorCheck(Token*, RefPtr&); bool noframesCreateErrorCheck(Token*, RefPtr&); bool nolayerCreateErrorCheck(Token*, RefPtr&); bool noscriptCreateErrorCheck(Token*, RefPtr&); bool selectCreateErrorCheck(Token*, RefPtr&); bool tableCellCreateErrorCheck(Token*, RefPtr&); bool tableSectionCreateErrorCheck(Token*, RefPtr&); bool textCreateErrorCheck(Token*, RefPtr&); void processCloseTag(Token*); bool insertNode(Node*, bool flat = false); bool handleError(Node*, bool flat, const AtomicString& localName, int tagPriority); void pushBlock(const AtomicString& tagName, int level); void popBlock(const AtomicString& tagName, bool reportErrors = false); void popBlock(const QualifiedName& qName, bool reportErrors = false) { return popBlock(qName.localName(), reportErrors); } // Convenience function for readability. void popOneBlock(); void moveOneBlockToStack(HTMLStackElem*& head); inline HTMLStackElem* popOneBlockCommon(); void popInlineBlocks(); void freeBlock(); void createHead(); static bool isResidualStyleTag(const AtomicString& tagName); static bool isAffectedByResidualStyle(const AtomicString& tagName); void handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem*); void reopenResidualStyleTags(HTMLStackElem*, Node* malformedTableParent); bool allowNestedRedundantTag(const AtomicString& tagName); static bool isHeaderTag(const AtomicString& tagName); void popNestedHeaderTag(); bool isInline(Node*) const; void startBody(); // inserts the isindex element PassRefPtr handleIsindex(Token*); void reportError(HTMLParserErrorCode errorCode, const AtomicString* tagName1 = 0, const AtomicString* tagName2 = 0, bool closeTags = false) { if (!m_reportErrors) return; reportErrorToConsole(errorCode, tagName1, tagName2, closeTags); } void reportErrorToConsole(HTMLParserErrorCode, const AtomicString* tagName1, const AtomicString* tagName2, bool closeTags); Document* document; // The currently active element (the one new elements will be added to). Can be a document fragment, a document or an element. Node* current; // We can't ref a document, but we don't want to constantly check if a node is a document just to decide whether to deref. bool didRefCurrent; HTMLStackElem* blockStack; RefPtr m_currentFormElement; // currently active form RefPtr m_currentMapElement; // current map HTMLHeadElement* head; // head element; needed for HTML which defines after RefPtr m_isindexElement; // a possible element in the head bool inBody; bool haveContent; bool haveFrameSet; AtomicString m_skipModeTag; // tells the parser to discard all tags until it reaches the one specified bool m_isParsingFragment; bool m_reportErrors; int inStrayTableContent; }; } #endif // HTMLParser_h