• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 HTMLParser_h
25 #define HTMLParser_h
26 
27 #include "QualifiedName.h"
28 #include <wtf/Forward.h>
29 #include <wtf/OwnPtr.h>
30 #include <wtf/RefPtr.h>
31 #include "HTMLParserErrorCodes.h"
32 
33 namespace WebCore {
34 
35 class DoctypeToken;
36 class Document;
37 class DocumentFragment;
38 class HTMLDocument;
39 class HTMLFormElement;
40 class HTMLHeadElement;
41 class HTMLMapElement;
42 class HTMLParserQuirks;
43 class Node;
44 
45 struct HTMLStackElem;
46 struct Token;
47 
48 /**
49  * The parser for HTML. It receives a stream of tokens from the HTMLTokenizer, and
50  * builds up the Document structure from it.
51  */
52 class HTMLParser : public Noncopyable {
53 public:
54     HTMLParser(HTMLDocument*, bool reportErrors);
55     HTMLParser(DocumentFragment*);
56     virtual ~HTMLParser();
57 
58     /**
59      * parses one token delivered by the tokenizer
60      */
61     PassRefPtr<Node> parseToken(Token*);
62 
63     // Parses a doctype token.
64     void parseDoctypeToken(DoctypeToken*);
65 
66     /**
67      * tokenizer says it's not going to be sending us any more tokens
68      */
69     void finished();
70 
71     /**
72      * resets the parser
73      */
74     void reset();
75 
skipMode()76     bool skipMode() const { return !m_skipModeTag.isNull(); }
isHandlingResidualStyleAcrossBlocks()77     bool isHandlingResidualStyleAcrossBlocks() const { return m_handlingResidualStyleAcrossBlocks; }
78 
79 private:
80     void setCurrent(Node*);
81     void derefCurrent();
setSkipMode(const QualifiedName & qName)82     void setSkipMode(const QualifiedName& qName) { m_skipModeTag = qName.localName(); }
83 
84     PassRefPtr<Node> getNode(Token*);
85     bool bodyCreateErrorCheck(Token*, RefPtr<Node>&);
86     bool canvasCreateErrorCheck(Token*, RefPtr<Node>&);
87     bool commentCreateErrorCheck(Token*, RefPtr<Node>&);
88     bool ddCreateErrorCheck(Token*, RefPtr<Node>&);
89     bool dtCreateErrorCheck(Token*, RefPtr<Node>&);
90     bool formCreateErrorCheck(Token*, RefPtr<Node>&);
91     bool framesetCreateErrorCheck(Token*, RefPtr<Node>&);
92     bool headCreateErrorCheck(Token*, RefPtr<Node>&);
93     bool iframeCreateErrorCheck(Token*, RefPtr<Node>&);
94     bool isindexCreateErrorCheck(Token*, RefPtr<Node>&);
95     bool mapCreateErrorCheck(Token*, RefPtr<Node>&);
96     bool nestedCreateErrorCheck(Token*, RefPtr<Node>&);
97     bool nestedPCloserCreateErrorCheck(Token*, RefPtr<Node>&);
98     bool nestedStyleCreateErrorCheck(Token*, RefPtr<Node>&);
99     bool noembedCreateErrorCheck(Token*, RefPtr<Node>&);
100     bool noframesCreateErrorCheck(Token*, RefPtr<Node>&);
101     bool nolayerCreateErrorCheck(Token*, RefPtr<Node>&);
102     bool noscriptCreateErrorCheck(Token*, RefPtr<Node>&);
103     bool pCloserCreateErrorCheck(Token*, RefPtr<Node>&);
104     bool pCloserStrictCreateErrorCheck(Token*, RefPtr<Node>&);
105     bool rpCreateErrorCheck(Token*, RefPtr<Node>&);
106     bool rtCreateErrorCheck(Token*, RefPtr<Node>&);
107     bool selectCreateErrorCheck(Token*, RefPtr<Node>&);
108     bool tableCellCreateErrorCheck(Token*, RefPtr<Node>&);
109     bool tableSectionCreateErrorCheck(Token*, RefPtr<Node>&);
110     bool textCreateErrorCheck(Token*, RefPtr<Node>&);
111 
112     void processCloseTag(Token*);
113 
114     bool insertNode(Node*, bool flat = false);
115     bool handleError(Node*, bool flat, const AtomicString& localName, int tagPriority);
116 
117     void pushBlock(const AtomicString& tagName, int level);
118     void popBlock(const AtomicString& tagName, bool reportErrors = false);
119     void popBlock(const QualifiedName& qName, bool reportErrors = false) { return popBlock(qName.localName(), reportErrors); } // Convenience function for readability.
120     void popOneBlock();
121     void moveOneBlockToStack(HTMLStackElem*& head);
122     inline HTMLStackElem* popOneBlockCommon();
123     void popInlineBlocks();
124 
125     void freeBlock();
126 
127     void createHead();
128 
129     static bool isResidualStyleTag(const AtomicString& tagName);
130     static bool isAffectedByResidualStyle(const AtomicString& tagName);
131     void handleResidualStyleCloseTagAcrossBlocks(HTMLStackElem*);
132     void reopenResidualStyleTags(HTMLStackElem*, Node* malformedTableParent);
133 
134     bool allowNestedRedundantTag(const AtomicString& tagName);
135 
136     static bool isHeaderTag(const AtomicString& tagName);
137     void popNestedHeaderTag();
138 
139     bool isInline(Node*) const;
140 
141     void startBody(); // inserts the isindex element
142     PassRefPtr<Node> handleIsindex(Token*);
143 
144     void checkIfHasPElementInScope();
hasPElementInScope()145     bool hasPElementInScope()
146     {
147         if (m_hasPElementInScope == Unknown)
148             checkIfHasPElementInScope();
149         return m_hasPElementInScope == InScope;
150     }
151 
152     void reportError(HTMLParserErrorCode errorCode, const AtomicString* tagName1 = 0, const AtomicString* tagName2 = 0, bool closeTags = false)
153     { if (!m_reportErrors) return; reportErrorToConsole(errorCode, tagName1, tagName2, closeTags); }
154 
155     void reportErrorToConsole(HTMLParserErrorCode, const AtomicString* tagName1, const AtomicString* tagName2, bool closeTags);
156 
157     Document* m_document;
158 
159     // The currently active element (the one new elements will be added to). Can be a document fragment, a document or an element.
160     Node* m_current;
161     // 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.
162     bool m_didRefCurrent;
163 
164     HTMLStackElem* m_blockStack;
165 
166     // The number of tags with priority minBlockLevelTagPriority or higher
167     // currently in m_blockStack. The parser enforces a cap on this value by
168     // adding such new elements as siblings instead of children once it is reached.
169     size_t m_blocksInStack;
170 
171     enum ElementInScopeState { NotInScope, InScope, Unknown };
172     ElementInScopeState m_hasPElementInScope;
173 
174     RefPtr<HTMLFormElement> m_currentFormElement; // currently active form
175     RefPtr<HTMLMapElement> m_currentMapElement; // current map
176     RefPtr<HTMLHeadElement> m_head; // head element; needed for HTML which defines <base> after </head>
177     RefPtr<Node> m_isindexElement; // a possible <isindex> element in the head
178 
179     bool m_inBody;
180     bool m_haveContent;
181     bool m_haveFrameSet;
182 
183     AtomicString m_skipModeTag; // tells the parser to discard all tags until it reaches the one specified
184 
185     bool m_isParsingFragment;
186     bool m_reportErrors;
187     bool m_handlingResidualStyleAcrossBlocks;
188     int m_inStrayTableContent;
189 
190     OwnPtr<HTMLParserQuirks> m_parserQuirks;
191 };
192 
193 #if defined(BUILDING_ON_LEOPARD) || defined(BUILDING_ON_TIGER)
194 bool shouldCreateImplicitHead(Document*);
195 #else
shouldCreateImplicitHead(Document *)196 inline bool shouldCreateImplicitHead(Document*) { return true; }
197 #endif
198 
199 }
200 
201 #endif // HTMLParser_h
202