WebCore/html/LegacyHTMLTreeBuilder.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2     Copyright (C) 1997 Martin Jones (mjones@kde.org)
       
     3               (C) 1997 Torben Weis (weis@kde.org)
       
     4               (C) 1998 Waldo Bastian (bastian@kde.org)
       
     5               (C) 1999 Lars Knoll (knoll@kde.org)
       
     6     Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
       
     7 
       
     8     This library is free software; you can redistribute it and/or
       
     9     modify it under the terms of the GNU Library General Public
       
    10     License as published by the Free Software Foundation; either
       
    11     version 2 of the License, or (at your option) any later version.
       
    12 
       
    13     This library is distributed in the hope that it will be useful,
       
    14     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    16     Library General Public License for more details.
       
    17 
       
    18     You should have received a copy of the GNU Library General Public License
       
    19     along with this library; see the file COPYING.LIB.  If not, write to
       
    20     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
       
    21     Boston, MA 02110-1301, USA.
       
    22 */
       
    23 
       
    24 #ifndef LegacyHTMLTreeBuilder_h
       
    25 #define LegacyHTMLTreeBuilder_h
       
    26 
       
    27 #include "FragmentScriptingPermission.h"
       
    28 #include "HTMLParserErrorCodes.h"
       
    29 #include "QualifiedName.h"
       
    30 #include <wtf/Forward.h>
       
    31 #include <wtf/OwnPtr.h>
       
    32 #include <wtf/RefPtr.h>
       
    33 
       
    34 namespace WebCore {
       
    35 
       
    36 class DoctypeToken;
       
    37 class Document;
       
    38 class DocumentFragment;
       
    39 class HTMLDocument;
       
    40 class HTMLFormElement;
       
    41 class HTMLHeadElement;
       
    42 class HTMLMapElement;
       
    43 class HTMLParserQuirks;
       
    44 class Node;
       
    45 
       
    46 struct HTMLStackElem;
       
    47 struct Token;
       
    48 
       
    49 /**
       
    50  * The parser for HTML. It receives a stream of tokens from the LegacyHTMLDocumentParser, and
       
    51  * builds up the Document structure from it.
       
    52  */
       
    53 class LegacyHTMLTreeBuilder : public Noncopyable {
       
    54 public:
       
    55     LegacyHTMLTreeBuilder(HTMLDocument*, bool reportErrors);
       
    56     LegacyHTMLTreeBuilder(DocumentFragment*, FragmentScriptingPermission = FragmentScriptingAllowed);
       
    57     virtual ~LegacyHTMLTreeBuilder();
       
    58 
       
    59     /**
       
    60      * parses one token delivered by the tokenizer
       
    61      */
       
    62     PassRefPtr<Node> parseToken(Token*);
       
    63     
       
    64     // Parses a doctype token.
       
    65     void parseDoctypeToken(DoctypeToken*);
       
    66 
       
    67     /**
       
    68      * tokenizer says it's not going to be sending us any more tokens
       
    69      */
       
    70     void finished();
       
    71 
       
    72     /**
       
    73      * resets the parser
       
    74      */
       
    75     void reset();
       
    76 
       
    77     bool skipMode() const { return !m_skipModeTag.isNull(); }
       
    78     bool isHandlingResidualStyleAcrossBlocks() const { return m_handlingResidualStyleAcrossBlocks; }
       
    79 
       
    80 private:
       
    81     void setCurrent(Node*);
       
    82     void derefCurrent();
       
    83     void setSkipMode(const QualifiedName& qName) { m_skipModeTag = qName.localName(); }
       
    84 
       
    85     PassRefPtr<Node> getNode(Token*);
       
    86     bool bodyCreateErrorCheck(Token*, RefPtr<Node>&);
       
    87     bool canvasCreateErrorCheck(Token*, RefPtr<Node>&);
       
    88     bool colCreateErrorCheck(Token*, RefPtr<Node>&);
       
    89     bool commentCreateErrorCheck(Token*, RefPtr<Node>&);
       
    90     bool ddCreateErrorCheck(Token*, RefPtr<Node>&);
       
    91     bool dtCreateErrorCheck(Token*, RefPtr<Node>&);
       
    92     bool formCreateErrorCheck(Token*, RefPtr<Node>&);
       
    93     bool framesetCreateErrorCheck(Token*, RefPtr<Node>&);
       
    94     bool headCreateErrorCheck(Token*, RefPtr<Node>&);
       
    95     bool iframeCreateErrorCheck(Token*, RefPtr<Node>&);
       
    96     bool isindexCreateErrorCheck(Token*, RefPtr<Node>&);
       
    97     bool mapCreateErrorCheck(Token*, RefPtr<Node>&);
       
    98     bool nestedCreateErrorCheck(Token*, RefPtr<Node>&);
       
    99     bool nestedPCloserCreateErrorCheck(Token*, RefPtr<Node>&);
       
   100     bool nestedStyleCreateErrorCheck(Token*, RefPtr<Node>&);
       
   101     bool noembedCreateErrorCheck(Token*, RefPtr<Node>&);
       
   102     bool noframesCreateErrorCheck(Token*, RefPtr<Node>&);
       
   103     bool nolayerCreateErrorCheck(Token*, RefPtr<Node>&);
       
   104     bool noscriptCreateErrorCheck(Token*, RefPtr<Node>&);
       
   105     bool pCloserCreateErrorCheck(Token*, RefPtr<Node>&);
       
   106     bool pCloserStrictCreateErrorCheck(Token*, RefPtr<Node>&);
       
   107     bool rpCreateErrorCheck(Token*, RefPtr<Node>&);
       
   108     bool rtCreateErrorCheck(Token*, RefPtr<Node>&);
       
   109     bool selectCreateErrorCheck(Token*, RefPtr<Node>&);
       
   110     bool tableCellCreateErrorCheck(Token*, RefPtr<Node>&);
       
   111     bool tableSectionCreateErrorCheck(Token*, RefPtr<Node>&);
       
   112     bool textCreateErrorCheck(Token*, RefPtr<Node>&);
       
   113 
       
   114     void processCloseTag(Token*);
       
   115 
       
   116     void limitDepth(int tagPriority);
       
   117     bool insertNodeAfterLimitDepth(Node*, bool flat = false);
       
   118     bool insertNode(Node*, bool flat = false);
       
   119     bool handleError(Node*, bool flat, const AtomicString& localName, int tagPriority);
       
   120     
       
   121     void pushBlock(const AtomicString& tagName, int level);
       
   122     void popBlock(const AtomicString& tagName, bool reportErrors = false);
       
   123     void popBlock(const QualifiedName& qName, bool reportErrors = false) { return popBlock(qName.localName(), reportErrors); } // Convenience function for readability.
       
   124     void popOneBlock();
       
   125     void moveOneBlockToStack(HTMLStackElem*& head);
       
   126     inline HTMLStackElem* popOneBlockCommon();
       
   127     void popInlineBlocks();
       
   128 
       
   129     void freeBlock();
       
   130 
       
   131     void createHead();
       
   132 
       
   133     static bool isResidualStyleTag(const AtomicString& tagName);
       
   134     static bool isAffectedByResidualStyle(const AtomicString& tagName);
       
   135     void handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem*);
       
   136     void reopenResidualStyleTags(HTMLStackElem*, Node* malformedTableParent);
       
   137 
       
   138     bool allowNestedRedundantTag(const AtomicString& tagName);
       
   139     
       
   140     static bool isHeadingTag(const AtomicString& tagName);
       
   141 
       
   142     bool isInline(Node*) const;
       
   143     
       
   144     void startBody(); // inserts the isindex element
       
   145     PassRefPtr<Node> handleIsindex(Token*);
       
   146 
       
   147     void checkIfHasPElementInScope();
       
   148     bool hasPElementInScope()
       
   149     {
       
   150         if (m_hasPElementInScope == Unknown)
       
   151             checkIfHasPElementInScope();
       
   152         return m_hasPElementInScope == InScope;
       
   153     }
       
   154 
       
   155     void reportError(HTMLParserErrorCode errorCode, const AtomicString* tagName1 = 0, const AtomicString* tagName2 = 0, bool closeTags = false)
       
   156     { if (!m_reportErrors) return; reportErrorToConsole(errorCode, tagName1, tagName2, closeTags); }
       
   157 
       
   158     void reportErrorToConsole(HTMLParserErrorCode, const AtomicString* tagName1, const AtomicString* tagName2, bool closeTags);
       
   159     
       
   160     Document* m_document;
       
   161 
       
   162     // The currently active element (the one new elements will be added to). Can be a document fragment, a document or an element.
       
   163     Node* m_current;
       
   164     // 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.
       
   165     bool m_didRefCurrent;
       
   166 
       
   167     HTMLStackElem* m_blockStack;
       
   168 
       
   169     // The number of tags with priority minBlockLevelTagPriority or higher
       
   170     // currently in m_blockStack. The parser enforces a cap on this value by
       
   171     // adding such new elements as siblings instead of children once it is reached.
       
   172     size_t m_blocksInStack;
       
   173     // Depth of the tree.
       
   174     unsigned m_treeDepth;
       
   175 
       
   176     enum ElementInScopeState { NotInScope, InScope, Unknown }; 
       
   177     ElementInScopeState m_hasPElementInScope;
       
   178 
       
   179     RefPtr<HTMLFormElement> m_currentFormElement; // currently active form
       
   180     RefPtr<HTMLMapElement> m_currentMapElement; // current map
       
   181     RefPtr<HTMLHeadElement> m_head; // head element; needed for HTML which defines <base> after </head>
       
   182     RefPtr<Node> m_isindexElement; // a possible <isindex> element in the head
       
   183 
       
   184     bool m_inBody;
       
   185     bool m_haveContent;
       
   186     bool m_haveFrameSet;
       
   187 
       
   188     AtomicString m_skipModeTag; // tells the parser to discard all tags until it reaches the one specified
       
   189 
       
   190     bool m_isParsingFragment;
       
   191     bool m_reportErrors;
       
   192     bool m_handlingResidualStyleAcrossBlocks;
       
   193     int m_inStrayTableContent;
       
   194     FragmentScriptingPermission m_scriptingPermission;
       
   195 
       
   196     OwnPtr<HTMLParserQuirks> m_parserQuirks;
       
   197 };
       
   198 
       
   199 #if defined(BUILDING_ON_LEOPARD) || defined(BUILDING_ON_TIGER)
       
   200 bool shouldCreateImplicitHead(Document*);
       
   201 #else
       
   202 inline bool shouldCreateImplicitHead(Document*) { return true; }
       
   203 #endif
       
   204 
       
   205 // Converts the specified string to a floating number.
       
   206 // If the conversion fails, the return value is false. Take care that leading or trailing unnecessary characters make failures.  This returns false for an empty string input.
       
   207 // The double* parameter may be 0.
       
   208 bool parseToDoubleForNumberType(const String&, double*);
       
   209 // Converts the specified number to a string. This is an implementation of
       
   210 // HTML5's "algorithm to convert a number to a string" for NUMBER/RANGE types.
       
   211 String serializeForNumberType(double);
       
   212 
       
   213 }
       
   214     
       
   215 #endif // LegacyHTMLTreeBuilder_h