Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /*
2 * Copyright (c) 2007 Henri Sivonen
3 * Copyright (c) 2007-2011 Mozilla Foundation
4 * Portions of comments Copyright 2004-2008 Apple Computer, Inc., Mozilla
5 * Foundation, and Opera Software ASA.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 */
26 /*
27 * THIS IS A GENERATED FILE. PLEASE DO NOT EDIT.
28 * Please edit TreeBuilder.java instead and regenerate.
29 */
31 #ifndef nsHtml5TreeBuilder_h
32 #define nsHtml5TreeBuilder_h
34 #include "nsContentUtils.h"
35 #include "nsIAtom.h"
36 #include "nsHtml5AtomTable.h"
37 #include "nsITimer.h"
38 #include "nsString.h"
39 #include "nsNameSpaceManager.h"
40 #include "nsIContent.h"
41 #include "nsTraceRefcnt.h"
42 #include "jArray.h"
43 #include "nsHtml5DocumentMode.h"
44 #include "nsHtml5ArrayCopy.h"
45 #include "nsHtml5Parser.h"
46 #include "nsHtml5Atoms.h"
47 #include "nsHtml5TreeOperation.h"
48 #include "nsHtml5PendingNotification.h"
49 #include "nsHtml5StateSnapshot.h"
50 #include "nsHtml5StackNode.h"
51 #include "nsHtml5TreeOpExecutor.h"
52 #include "nsHtml5StreamParser.h"
53 #include "nsAHtml5TreeBuilderState.h"
54 #include "nsHtml5Highlighter.h"
55 #include "nsHtml5PlainTextUtils.h"
56 #include "nsHtml5ViewSourceUtils.h"
57 #include "mozilla/Likely.h"
58 #include "nsIContentHandle.h"
59 #include "nsHtml5OplessBuilder.h"
61 class nsHtml5StreamParser;
63 class nsHtml5Tokenizer;
64 class nsHtml5MetaScanner;
65 class nsHtml5AttributeName;
66 class nsHtml5ElementName;
67 class nsHtml5HtmlAttributes;
68 class nsHtml5UTF16Buffer;
69 class nsHtml5StateSnapshot;
70 class nsHtml5Portability;
73 class nsHtml5TreeBuilder : public nsAHtml5TreeBuilderState
74 {
75 private:
76 static char16_t REPLACEMENT_CHARACTER[];
77 static staticJArray<const char*,int32_t> QUIRKY_PUBLIC_IDS;
78 int32_t mode;
79 int32_t originalMode;
80 bool framesetOk;
81 protected:
82 nsHtml5Tokenizer* tokenizer;
83 private:
84 bool scriptingEnabled;
85 bool needToDropLF;
86 bool fragment;
87 nsIAtom* contextName;
88 int32_t contextNamespace;
89 nsIContentHandle* contextNode;
90 autoJArray<int32_t,int32_t> templateModeStack;
91 int32_t templateModePtr;
92 autoJArray<nsHtml5StackNode*,int32_t> stack;
93 int32_t currentPtr;
94 autoJArray<nsHtml5StackNode*,int32_t> listOfActiveFormattingElements;
95 int32_t listPtr;
96 nsIContentHandle* formPointer;
97 nsIContentHandle* headPointer;
98 nsIContentHandle* deepTreeSurrogateParent;
99 protected:
100 autoJArray<char16_t,int32_t> charBuffer;
101 int32_t charBufferLen;
102 private:
103 bool quirks;
104 bool isSrcdocDocument;
105 public:
106 void startTokenization(nsHtml5Tokenizer* self);
107 void doctype(nsIAtom* name, nsString* publicIdentifier, nsString* systemIdentifier, bool forceQuirks);
108 void comment(char16_t* buf, int32_t start, int32_t length);
109 void characters(const char16_t* buf, int32_t start, int32_t length);
110 void zeroOriginatingReplacementCharacter();
111 void eof();
112 void endTokenization();
113 void startTag(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes, bool selfClosing);
114 private:
115 void startTagTitleInHead(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
116 void startTagGenericRawText(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
117 void startTagScriptInHead(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
118 void startTagTemplateInHead(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
119 bool isTemplateContents();
120 bool isTemplateModeStackEmpty();
121 bool isSpecialParentInForeign(nsHtml5StackNode* stackNode);
122 public:
123 static nsString* extractCharsetFromContent(nsString* attributeValue);
124 private:
125 void checkMetaCharset(nsHtml5HtmlAttributes* attributes);
126 public:
127 void endTag(nsHtml5ElementName* elementName);
128 private:
129 void endTagTemplateInHead();
130 int32_t findLastInTableScopeOrRootTemplateTbodyTheadTfoot();
131 int32_t findLast(nsIAtom* name);
132 int32_t findLastInTableScope(nsIAtom* name);
133 int32_t findLastInButtonScope(nsIAtom* name);
134 int32_t findLastInScope(nsIAtom* name);
135 int32_t findLastInListScope(nsIAtom* name);
136 int32_t findLastInScopeHn();
137 void generateImpliedEndTagsExceptFor(nsIAtom* name);
138 void generateImpliedEndTags();
139 bool isSecondOnStackBody();
140 void documentModeInternal(nsHtml5DocumentMode m, nsString* publicIdentifier, nsString* systemIdentifier, bool html4SpecificAdditionalErrorChecks);
141 bool isAlmostStandards(nsString* publicIdentifier, nsString* systemIdentifier);
142 bool isQuirky(nsIAtom* name, nsString* publicIdentifier, nsString* systemIdentifier, bool forceQuirks);
143 void closeTheCell(int32_t eltPos);
144 int32_t findLastInTableScopeTdTh();
145 void clearStackBackTo(int32_t eltPos);
146 void resetTheInsertionMode();
147 void implicitlyCloseP();
148 bool debugOnlyClearLastStackSlot();
149 bool debugOnlyClearLastListSlot();
150 void pushTemplateMode(int32_t mode);
151 void push(nsHtml5StackNode* node);
152 void silentPush(nsHtml5StackNode* node);
153 void append(nsHtml5StackNode* node);
154 inline void insertMarker()
155 {
156 append(nullptr);
157 }
159 void clearTheListOfActiveFormattingElementsUpToTheLastMarker();
160 inline bool isCurrent(nsIAtom* name)
161 {
162 return stack[currentPtr]->ns == kNameSpaceID_XHTML && name == stack[currentPtr]->name;
163 }
165 void removeFromStack(int32_t pos);
166 void removeFromStack(nsHtml5StackNode* node);
167 void removeFromListOfActiveFormattingElements(int32_t pos);
168 bool adoptionAgencyEndTag(nsIAtom* name);
169 void insertIntoStack(nsHtml5StackNode* node, int32_t position);
170 void insertIntoListOfActiveFormattingElements(nsHtml5StackNode* formattingClone, int32_t bookmark);
171 int32_t findInListOfActiveFormattingElements(nsHtml5StackNode* node);
172 int32_t findInListOfActiveFormattingElementsContainsBetweenEndAndLastMarker(nsIAtom* name);
173 void maybeForgetEarlierDuplicateFormattingElement(nsIAtom* name, nsHtml5HtmlAttributes* attributes);
174 int32_t findLastOrRoot(nsIAtom* name);
175 int32_t findLastOrRoot(int32_t group);
176 bool addAttributesToBody(nsHtml5HtmlAttributes* attributes);
177 void addAttributesToHtml(nsHtml5HtmlAttributes* attributes);
178 void pushHeadPointerOntoStack();
179 void reconstructTheActiveFormattingElements();
180 void insertIntoFosterParent(nsIContentHandle* child);
181 bool isInStack(nsHtml5StackNode* node);
182 void popTemplateMode();
183 void pop();
184 void silentPop();
185 void popOnEof();
186 void appendHtmlElementToDocumentAndPush(nsHtml5HtmlAttributes* attributes);
187 void appendHtmlElementToDocumentAndPush();
188 void appendToCurrentNodeAndPushHeadElement(nsHtml5HtmlAttributes* attributes);
189 void appendToCurrentNodeAndPushBodyElement(nsHtml5HtmlAttributes* attributes);
190 void appendToCurrentNodeAndPushBodyElement();
191 void appendToCurrentNodeAndPushFormElementMayFoster(nsHtml5HtmlAttributes* attributes);
192 void appendToCurrentNodeAndPushFormattingElementMayFoster(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
193 void appendToCurrentNodeAndPushElement(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
194 void appendToCurrentNodeAndPushElementMayFoster(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
195 void appendToCurrentNodeAndPushElementMayFosterMathML(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
196 bool annotationXmlEncodingPermitsHtml(nsHtml5HtmlAttributes* attributes);
197 void appendToCurrentNodeAndPushElementMayFosterSVG(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
198 void appendToCurrentNodeAndPushElementMayFoster(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes, nsIContentHandle* form);
199 void appendVoidElementToCurrentMayFoster(nsIAtom* name, nsHtml5HtmlAttributes* attributes, nsIContentHandle* form);
200 void appendVoidElementToCurrentMayFoster(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
201 void appendVoidElementToCurrentMayFosterSVG(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
202 void appendVoidElementToCurrentMayFosterMathML(nsHtml5ElementName* elementName, nsHtml5HtmlAttributes* attributes);
203 void appendVoidElementToCurrent(nsIAtom* name, nsHtml5HtmlAttributes* attributes, nsIContentHandle* form);
204 void appendVoidFormToCurrent(nsHtml5HtmlAttributes* attributes);
205 protected:
206 void accumulateCharacters(const char16_t* buf, int32_t start, int32_t length);
207 void requestSuspension();
208 nsIContentHandle* createElement(int32_t ns, nsIAtom* name, nsHtml5HtmlAttributes* attributes);
209 nsIContentHandle* createElement(int32_t ns, nsIAtom* name, nsHtml5HtmlAttributes* attributes, nsIContentHandle* form);
210 nsIContentHandle* createHtmlElementSetAsRoot(nsHtml5HtmlAttributes* attributes);
211 void detachFromParent(nsIContentHandle* element);
212 bool hasChildren(nsIContentHandle* element);
213 void appendElement(nsIContentHandle* child, nsIContentHandle* newParent);
214 void appendChildrenToNewParent(nsIContentHandle* oldParent, nsIContentHandle* newParent);
215 void insertFosterParentedChild(nsIContentHandle* child, nsIContentHandle* table, nsIContentHandle* stackParent);
216 void insertFosterParentedCharacters(char16_t* buf, int32_t start, int32_t length, nsIContentHandle* table, nsIContentHandle* stackParent);
217 void appendCharacters(nsIContentHandle* parent, char16_t* buf, int32_t start, int32_t length);
218 void appendIsindexPrompt(nsIContentHandle* parent);
219 void appendComment(nsIContentHandle* parent, char16_t* buf, int32_t start, int32_t length);
220 void appendCommentToDocument(char16_t* buf, int32_t start, int32_t length);
221 void addAttributesToElement(nsIContentHandle* element, nsHtml5HtmlAttributes* attributes);
222 void markMalformedIfScript(nsIContentHandle* elt);
223 void start(bool fragmentMode);
224 void end();
225 void appendDoctypeToDocument(nsIAtom* name, nsString* publicIdentifier, nsString* systemIdentifier);
226 void elementPushed(int32_t ns, nsIAtom* name, nsIContentHandle* node);
227 void elementPopped(int32_t ns, nsIAtom* name, nsIContentHandle* node);
228 public:
229 inline bool cdataSectionAllowed()
230 {
231 return isInForeign();
232 }
234 private:
235 bool isInForeign();
236 bool isInForeignButNotHtmlOrMathTextIntegrationPoint();
237 public:
238 void setFragmentContext(nsIAtom* context, int32_t ns, nsIContentHandle* node, bool quirks);
239 protected:
240 nsIContentHandle* currentNode();
241 public:
242 bool isScriptingEnabled();
243 void setScriptingEnabled(bool scriptingEnabled);
244 void setIsSrcdocDocument(bool isSrcdocDocument);
245 void flushCharacters();
246 private:
247 bool charBufferContainsNonWhitespace();
248 public:
249 nsAHtml5TreeBuilderState* newSnapshot();
250 bool snapshotMatches(nsAHtml5TreeBuilderState* snapshot);
251 void loadState(nsAHtml5TreeBuilderState* snapshot, nsHtml5AtomTable* interner);
252 private:
253 int32_t findInArray(nsHtml5StackNode* node, jArray<nsHtml5StackNode*,int32_t> arr);
254 public:
255 nsIContentHandle* getFormPointer();
256 nsIContentHandle* getHeadPointer();
257 nsIContentHandle* getDeepTreeSurrogateParent();
258 jArray<nsHtml5StackNode*,int32_t> getListOfActiveFormattingElements();
259 jArray<nsHtml5StackNode*,int32_t> getStack();
260 jArray<int32_t,int32_t> getTemplateModeStack();
261 int32_t getMode();
262 int32_t getOriginalMode();
263 bool isFramesetOk();
264 bool isNeedToDropLF();
265 bool isQuirks();
266 int32_t getListOfActiveFormattingElementsLength();
267 int32_t getStackLength();
268 int32_t getTemplateModeStackLength();
269 static void initializeStatics();
270 static void releaseStatics();
272 #include "nsHtml5TreeBuilderHSupplement.h"
273 };
275 #define NS_HTML5TREE_BUILDER_OTHER 0
276 #define NS_HTML5TREE_BUILDER_A 1
277 #define NS_HTML5TREE_BUILDER_BASE 2
278 #define NS_HTML5TREE_BUILDER_BODY 3
279 #define NS_HTML5TREE_BUILDER_BR 4
280 #define NS_HTML5TREE_BUILDER_BUTTON 5
281 #define NS_HTML5TREE_BUILDER_CAPTION 6
282 #define NS_HTML5TREE_BUILDER_COL 7
283 #define NS_HTML5TREE_BUILDER_COLGROUP 8
284 #define NS_HTML5TREE_BUILDER_FORM 9
285 #define NS_HTML5TREE_BUILDER_FRAME 10
286 #define NS_HTML5TREE_BUILDER_FRAMESET 11
287 #define NS_HTML5TREE_BUILDER_IMAGE 12
288 #define NS_HTML5TREE_BUILDER_INPUT 13
289 #define NS_HTML5TREE_BUILDER_ISINDEX 14
290 #define NS_HTML5TREE_BUILDER_LI 15
291 #define NS_HTML5TREE_BUILDER_LINK_OR_BASEFONT_OR_BGSOUND 16
292 #define NS_HTML5TREE_BUILDER_MATH 17
293 #define NS_HTML5TREE_BUILDER_META 18
294 #define NS_HTML5TREE_BUILDER_SVG 19
295 #define NS_HTML5TREE_BUILDER_HEAD 20
296 #define NS_HTML5TREE_BUILDER_HR 22
297 #define NS_HTML5TREE_BUILDER_HTML 23
298 #define NS_HTML5TREE_BUILDER_NOBR 24
299 #define NS_HTML5TREE_BUILDER_NOFRAMES 25
300 #define NS_HTML5TREE_BUILDER_NOSCRIPT 26
301 #define NS_HTML5TREE_BUILDER_OPTGROUP 27
302 #define NS_HTML5TREE_BUILDER_OPTION 28
303 #define NS_HTML5TREE_BUILDER_P 29
304 #define NS_HTML5TREE_BUILDER_PLAINTEXT 30
305 #define NS_HTML5TREE_BUILDER_SCRIPT 31
306 #define NS_HTML5TREE_BUILDER_SELECT 32
307 #define NS_HTML5TREE_BUILDER_STYLE 33
308 #define NS_HTML5TREE_BUILDER_TABLE 34
309 #define NS_HTML5TREE_BUILDER_TEXTAREA 35
310 #define NS_HTML5TREE_BUILDER_TITLE 36
311 #define NS_HTML5TREE_BUILDER_TR 37
312 #define NS_HTML5TREE_BUILDER_XMP 38
313 #define NS_HTML5TREE_BUILDER_TBODY_OR_THEAD_OR_TFOOT 39
314 #define NS_HTML5TREE_BUILDER_TD_OR_TH 40
315 #define NS_HTML5TREE_BUILDER_DD_OR_DT 41
316 #define NS_HTML5TREE_BUILDER_H1_OR_H2_OR_H3_OR_H4_OR_H5_OR_H6 42
317 #define NS_HTML5TREE_BUILDER_MARQUEE_OR_APPLET 43
318 #define NS_HTML5TREE_BUILDER_PRE_OR_LISTING 44
319 #define NS_HTML5TREE_BUILDER_B_OR_BIG_OR_CODE_OR_EM_OR_I_OR_S_OR_SMALL_OR_STRIKE_OR_STRONG_OR_TT_OR_U 45
320 #define NS_HTML5TREE_BUILDER_UL_OR_OL_OR_DL 46
321 #define NS_HTML5TREE_BUILDER_IFRAME 47
322 #define NS_HTML5TREE_BUILDER_EMBED 48
323 #define NS_HTML5TREE_BUILDER_AREA_OR_WBR 49
324 #define NS_HTML5TREE_BUILDER_DIV_OR_BLOCKQUOTE_OR_CENTER_OR_MENU 50
325 #define NS_HTML5TREE_BUILDER_ADDRESS_OR_ARTICLE_OR_ASIDE_OR_DETAILS_OR_DIR_OR_FIGCAPTION_OR_FIGURE_OR_FOOTER_OR_HEADER_OR_HGROUP_OR_MAIN_OR_NAV_OR_SECTION_OR_SUMMARY 51
326 #define NS_HTML5TREE_BUILDER_RUBY_OR_SPAN_OR_SUB_OR_SUP_OR_VAR 52
327 #define NS_HTML5TREE_BUILDER_RT_OR_RP 53
328 #define NS_HTML5TREE_BUILDER_PARAM_OR_SOURCE_OR_TRACK 55
329 #define NS_HTML5TREE_BUILDER_MGLYPH_OR_MALIGNMARK 56
330 #define NS_HTML5TREE_BUILDER_MI_MO_MN_MS_MTEXT 57
331 #define NS_HTML5TREE_BUILDER_ANNOTATION_XML 58
332 #define NS_HTML5TREE_BUILDER_FOREIGNOBJECT_OR_DESC 59
333 #define NS_HTML5TREE_BUILDER_NOEMBED 60
334 #define NS_HTML5TREE_BUILDER_FIELDSET 61
335 #define NS_HTML5TREE_BUILDER_OUTPUT_OR_LABEL 62
336 #define NS_HTML5TREE_BUILDER_OBJECT 63
337 #define NS_HTML5TREE_BUILDER_FONT 64
338 #define NS_HTML5TREE_BUILDER_KEYGEN 65
339 #define NS_HTML5TREE_BUILDER_MENUITEM 66
340 #define NS_HTML5TREE_BUILDER_TEMPLATE 67
341 #define NS_HTML5TREE_BUILDER_IMG 68
342 #define NS_HTML5TREE_BUILDER_IN_ROW 0
343 #define NS_HTML5TREE_BUILDER_IN_TABLE_BODY 1
344 #define NS_HTML5TREE_BUILDER_IN_TABLE 2
345 #define NS_HTML5TREE_BUILDER_IN_CAPTION 3
346 #define NS_HTML5TREE_BUILDER_IN_CELL 4
347 #define NS_HTML5TREE_BUILDER_FRAMESET_OK 5
348 #define NS_HTML5TREE_BUILDER_IN_BODY 6
349 #define NS_HTML5TREE_BUILDER_IN_HEAD 7
350 #define NS_HTML5TREE_BUILDER_IN_HEAD_NOSCRIPT 8
351 #define NS_HTML5TREE_BUILDER_IN_COLUMN_GROUP 9
352 #define NS_HTML5TREE_BUILDER_IN_SELECT_IN_TABLE 10
353 #define NS_HTML5TREE_BUILDER_IN_SELECT 11
354 #define NS_HTML5TREE_BUILDER_AFTER_BODY 12
355 #define NS_HTML5TREE_BUILDER_IN_FRAMESET 13
356 #define NS_HTML5TREE_BUILDER_AFTER_FRAMESET 14
357 #define NS_HTML5TREE_BUILDER_INITIAL 15
358 #define NS_HTML5TREE_BUILDER_BEFORE_HTML 16
359 #define NS_HTML5TREE_BUILDER_BEFORE_HEAD 17
360 #define NS_HTML5TREE_BUILDER_AFTER_HEAD 18
361 #define NS_HTML5TREE_BUILDER_AFTER_AFTER_BODY 19
362 #define NS_HTML5TREE_BUILDER_AFTER_AFTER_FRAMESET 20
363 #define NS_HTML5TREE_BUILDER_TEXT 21
364 #define NS_HTML5TREE_BUILDER_IN_TEMPLATE 22
365 #define NS_HTML5TREE_BUILDER_CHARSET_INITIAL 0
366 #define NS_HTML5TREE_BUILDER_CHARSET_C 1
367 #define NS_HTML5TREE_BUILDER_CHARSET_H 2
368 #define NS_HTML5TREE_BUILDER_CHARSET_A 3
369 #define NS_HTML5TREE_BUILDER_CHARSET_R 4
370 #define NS_HTML5TREE_BUILDER_CHARSET_S 5
371 #define NS_HTML5TREE_BUILDER_CHARSET_E 6
372 #define NS_HTML5TREE_BUILDER_CHARSET_T 7
373 #define NS_HTML5TREE_BUILDER_CHARSET_EQUALS 8
374 #define NS_HTML5TREE_BUILDER_CHARSET_SINGLE_QUOTED 9
375 #define NS_HTML5TREE_BUILDER_CHARSET_DOUBLE_QUOTED 10
376 #define NS_HTML5TREE_BUILDER_CHARSET_UNQUOTED 11
377 #define NS_HTML5TREE_BUILDER_NOT_FOUND_ON_STACK INT32_MAX
380 #endif