2 * Copyright (c) 2007 Henri Sivonen
3 * Copyright (c) 2007-2017 Mozilla Foundation
4 * Portions of comments Copyright 2004-2008 Apple Computer, Inc., Mozilla
5 * Foundation, and Opera Software ASA.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
27 * THIS IS A GENERATED FILE. PLEASE DO NOT EDIT.
28 * Please edit TreeBuilder.java instead and regenerate.
31 #ifndef nsHtml5TreeBuilder_h
32 #define nsHtml5TreeBuilder_h
34 #include "nsContentUtils.h"
36 #include "nsHtml5AtomTable.h"
37 #include "nsHtml5String.h"
38 #include "nsNameSpaceManager.h"
39 #include "nsIContent.h"
40 #include "nsTraceRefcnt.h"
42 #include "nsHtml5DocumentMode.h"
43 #include "nsHtml5ArrayCopy.h"
44 #include "nsHtml5Parser.h"
45 #include "nsGkAtoms.h"
46 #include "nsHtml5TreeOperation.h"
47 #include "nsHtml5StateSnapshot.h"
48 #include "nsHtml5StackNode.h"
49 #include "nsHtml5TreeOpExecutor.h"
50 #include "nsHtml5StreamParser.h"
51 #include "nsAHtml5TreeBuilderState.h"
52 #include "nsHtml5Highlighter.h"
53 #include "nsHtml5PlainTextUtils.h"
54 #include "nsHtml5ViewSourceUtils.h"
55 #include "mozilla/ImportScanner.h"
56 #include "mozilla/Likely.h"
57 #include "nsIContentHandle.h"
58 #include "nsHtml5OplessBuilder.h"
60 class nsHtml5StreamParser
;
62 class nsHtml5AttributeName
;
63 class nsHtml5ElementName
;
64 class nsHtml5Tokenizer
;
65 class nsHtml5MetaScanner
;
66 class nsHtml5UTF16Buffer
;
67 class nsHtml5StateSnapshot
;
68 class nsHtml5Portability
;
70 class nsHtml5TreeBuilder
: public nsAHtml5TreeBuilderState
{
72 static char16_t REPLACEMENT_CHARACTER
[];
75 static const int32_t OTHER
= 0;
77 static const int32_t A
= 1;
79 static const int32_t BASE
= 2;
81 static const int32_t BODY
= 3;
83 static const int32_t BR
= 4;
85 static const int32_t BUTTON
= 5;
87 static const int32_t CAPTION
= 6;
89 static const int32_t COL
= 7;
91 static const int32_t COLGROUP
= 8;
93 static const int32_t FORM
= 9;
95 static const int32_t FRAME
= 10;
97 static const int32_t FRAMESET
= 11;
99 static const int32_t IMAGE
= 12;
101 static const int32_t INPUT
= 13;
103 static const int32_t RT_OR_RP
= 14;
105 static const int32_t LI
= 15;
107 static const int32_t LINK_OR_BASEFONT_OR_BGSOUND
= 16;
109 static const int32_t MATH
= 17;
111 static const int32_t META
= 18;
113 static const int32_t SVG
= 19;
115 static const int32_t HEAD
= 20;
117 static const int32_t HR
= 22;
119 static const int32_t HTML
= 23;
121 static const int32_t NOBR
= 24;
123 static const int32_t NOFRAMES
= 25;
125 static const int32_t NOSCRIPT
= 26;
127 static const int32_t OPTGROUP
= 27;
129 static const int32_t OPTION
= 28;
131 static const int32_t P
= 29;
133 static const int32_t PLAINTEXT
= 30;
135 static const int32_t SCRIPT
= 31;
137 static const int32_t SELECT
= 32;
139 static const int32_t STYLE
= 33;
141 static const int32_t TABLE
= 34;
143 static const int32_t TEXTAREA
= 35;
145 static const int32_t TITLE
= 36;
147 static const int32_t TR
= 37;
149 static const int32_t XMP
= 38;
151 static const int32_t TBODY_OR_THEAD_OR_TFOOT
= 39;
153 static const int32_t TD_OR_TH
= 40;
155 static const int32_t DD_OR_DT
= 41;
157 static const int32_t H1_OR_H2_OR_H3_OR_H4_OR_H5_OR_H6
= 42;
159 static const int32_t MARQUEE_OR_APPLET
= 43;
161 static const int32_t PRE_OR_LISTING
= 44;
164 B_OR_BIG_OR_CODE_OR_EM_OR_I_OR_S_OR_SMALL_OR_STRIKE_OR_STRONG_OR_TT_OR_U
=
167 static const int32_t UL_OR_OL_OR_DL
= 46;
169 static const int32_t IFRAME
= 47;
171 static const int32_t EMBED
= 48;
173 static const int32_t AREA_OR_WBR
= 49;
175 static const int32_t DIV_OR_BLOCKQUOTE_OR_CENTER_OR_MENU
= 50;
178 ADDRESS_OR_ARTICLE_OR_ASIDE_OR_DETAILS_OR_DIALOG_OR_DIR_OR_FIGCAPTION_OR_FIGURE_OR_FOOTER_OR_HEADER_OR_HGROUP_OR_MAIN_OR_NAV_OR_SECTION_OR_SUMMARY
=
181 static const int32_t RUBY_OR_SPAN_OR_SUB_OR_SUP_OR_VAR
= 52;
183 static const int32_t RB_OR_RTC
= 53;
185 static const int32_t PARAM_OR_SOURCE_OR_TRACK
= 55;
187 static const int32_t MGLYPH_OR_MALIGNMARK
= 56;
189 static const int32_t MI_MO_MN_MS_MTEXT
= 57;
191 static const int32_t ANNOTATION_XML
= 58;
193 static const int32_t FOREIGNOBJECT_OR_DESC
= 59;
195 static const int32_t NOEMBED
= 60;
197 static const int32_t FIELDSET
= 61;
199 static const int32_t OUTPUT
= 62;
201 static const int32_t OBJECT
= 63;
203 static const int32_t FONT
= 64;
205 static const int32_t KEYGEN
= 65;
207 static const int32_t MENUITEM
= 66;
209 static const int32_t TEMPLATE
= 67;
211 static const int32_t IMG
= 68;
214 static const int32_t IN_ROW
= 0;
216 static const int32_t IN_TABLE_BODY
= 1;
218 static const int32_t IN_TABLE
= 2;
220 static const int32_t IN_CAPTION
= 3;
222 static const int32_t IN_CELL
= 4;
224 static const int32_t FRAMESET_OK
= 5;
226 static const int32_t IN_BODY
= 6;
228 static const int32_t IN_HEAD
= 7;
230 static const int32_t IN_HEAD_NOSCRIPT
= 8;
232 static const int32_t IN_COLUMN_GROUP
= 9;
234 static const int32_t IN_SELECT_IN_TABLE
= 10;
236 static const int32_t IN_SELECT
= 11;
238 static const int32_t AFTER_BODY
= 12;
240 static const int32_t IN_FRAMESET
= 13;
242 static const int32_t AFTER_FRAMESET
= 14;
244 static const int32_t INITIAL
= 15;
246 static const int32_t BEFORE_HTML
= 16;
248 static const int32_t BEFORE_HEAD
= 17;
250 static const int32_t AFTER_HEAD
= 18;
252 static const int32_t AFTER_AFTER_BODY
= 19;
254 static const int32_t AFTER_AFTER_FRAMESET
= 20;
256 static const int32_t TEXT
= 21;
258 static const int32_t IN_TEMPLATE
= 22;
260 static const int32_t CHARSET_INITIAL
= 0;
262 static const int32_t CHARSET_C
= 1;
264 static const int32_t CHARSET_H
= 2;
266 static const int32_t CHARSET_A
= 3;
268 static const int32_t CHARSET_R
= 4;
270 static const int32_t CHARSET_S
= 5;
272 static const int32_t CHARSET_E
= 6;
274 static const int32_t CHARSET_T
= 7;
276 static const int32_t CHARSET_EQUALS
= 8;
278 static const int32_t CHARSET_SINGLE_QUOTED
= 9;
280 static const int32_t CHARSET_DOUBLE_QUOTED
= 10;
282 static const int32_t CHARSET_UNQUOTED
= 11;
284 static staticJArray
<const char*, int32_t> QUIRKY_PUBLIC_IDS
;
285 static const int32_t NOT_FOUND_ON_STACK
= INT32_MAX
;
288 int32_t originalMode
;
292 nsHtml5Tokenizer
* tokenizer
;
295 bool scriptingEnabled
;
298 RefPtr
<nsAtom
> contextName
;
299 int32_t contextNamespace
;
300 nsIContentHandle
* contextNode
;
301 autoJArray
<int32_t, int32_t> templateModeStack
;
302 int32_t templateModePtr
;
303 autoJArray
<nsHtml5StackNode
*, int32_t> stackNodes
;
304 int32_t stackNodesIdx
;
305 int32_t numStackNodes
;
306 autoJArray
<nsHtml5StackNode
*, int32_t> stack
;
308 autoJArray
<nsHtml5StackNode
*, int32_t> listOfActiveFormattingElements
;
310 nsIContentHandle
* formPointer
;
311 nsIContentHandle
* headPointer
;
314 autoJArray
<char16_t
, int32_t> charBuffer
;
315 int32_t charBufferLen
;
319 bool isSrcdocDocument
;
320 inline nsHtml5ContentCreatorFunction
htmlCreator(
321 mozilla::dom::HTMLContentCreatorFunction htmlCreator
) {
322 nsHtml5ContentCreatorFunction creator
;
323 creator
.html
= htmlCreator
;
327 inline nsHtml5ContentCreatorFunction
svgCreator(
328 mozilla::dom::SVGContentCreatorFunction svgCreator
) {
329 nsHtml5ContentCreatorFunction creator
;
330 creator
.svg
= svgCreator
;
335 void startTokenization(nsHtml5Tokenizer
* self
);
336 void doctype(nsAtom
* name
, nsHtml5String publicIdentifier
,
337 nsHtml5String systemIdentifier
, bool forceQuirks
);
338 void comment(char16_t
* buf
, int32_t start
, int32_t length
);
339 void characters(const char16_t
* buf
, int32_t start
, int32_t length
);
340 void zeroOriginatingReplacementCharacter();
342 void endTokenization();
343 void startTag(nsHtml5ElementName
* elementName
,
344 nsHtml5HtmlAttributes
* attributes
, bool selfClosing
);
347 void startTagTitleInHead(nsHtml5ElementName
* elementName
,
348 nsHtml5HtmlAttributes
* attributes
);
349 void startTagGenericRawText(nsHtml5ElementName
* elementName
,
350 nsHtml5HtmlAttributes
* attributes
);
351 void startTagScriptInHead(nsHtml5ElementName
* elementName
,
352 nsHtml5HtmlAttributes
* attributes
);
353 void startTagTemplateInHead(nsHtml5ElementName
* elementName
,
354 nsHtml5HtmlAttributes
* attributes
);
355 bool isTemplateContents();
356 bool isTemplateModeStackEmpty();
357 bool isSpecialParentInForeign(nsHtml5StackNode
* stackNode
);
360 static nsHtml5String
extractCharsetFromContent(nsHtml5String attributeValue
,
361 nsHtml5TreeBuilder
* tb
);
364 void checkMetaCharset(nsHtml5HtmlAttributes
* attributes
);
367 void endTag(nsHtml5ElementName
* elementName
);
370 void endTagTemplateInHead();
371 int32_t findLastInTableScopeOrRootTemplateTbodyTheadTfoot();
372 int32_t findLast(nsAtom
* name
);
373 int32_t findLastInTableScope(nsAtom
* name
);
374 int32_t findLastInButtonScope(nsAtom
* name
);
375 int32_t findLastInScope(nsAtom
* name
);
376 int32_t findLastInListScope(nsAtom
* name
);
377 int32_t findLastInScopeHn();
378 void generateImpliedEndTagsExceptFor(nsAtom
* name
);
379 void generateImpliedEndTags();
380 bool isSecondOnStackBody();
381 void documentModeInternal(nsHtml5DocumentMode m
,
382 nsHtml5String publicIdentifier
,
383 nsHtml5String systemIdentifier
);
384 bool isAlmostStandards(nsHtml5String publicIdentifier
,
385 nsHtml5String systemIdentifier
);
386 bool isQuirky(nsAtom
* name
, nsHtml5String publicIdentifier
,
387 nsHtml5String systemIdentifier
, bool forceQuirks
);
388 void closeTheCell(int32_t eltPos
);
389 int32_t findLastInTableScopeTdTh();
390 void clearStackBackTo(int32_t eltPos
);
391 void resetTheInsertionMode();
392 void implicitlyCloseP();
393 bool debugOnlyClearLastStackSlot();
394 bool debugOnlyClearLastListSlot();
395 void pushTemplateMode(int32_t mode
);
396 void push(nsHtml5StackNode
* node
);
397 void silentPush(nsHtml5StackNode
* node
);
398 void append(nsHtml5StackNode
* node
);
399 inline void insertMarker() { append(nullptr); }
401 void clearTheListOfActiveFormattingElementsUpToTheLastMarker();
402 inline bool isCurrent(nsAtom
* name
) {
403 return stack
[currentPtr
]->ns
== kNameSpaceID_XHTML
&&
404 name
== stack
[currentPtr
]->name
;
407 void removeFromStack(int32_t pos
);
408 void removeFromStack(nsHtml5StackNode
* node
);
409 void removeFromListOfActiveFormattingElements(int32_t pos
);
410 bool adoptionAgencyEndTag(nsAtom
* name
);
411 void insertIntoStack(nsHtml5StackNode
* node
, int32_t position
);
412 void insertIntoListOfActiveFormattingElements(
413 nsHtml5StackNode
* formattingClone
, int32_t bookmark
);
414 int32_t findInListOfActiveFormattingElements(nsHtml5StackNode
* node
);
415 int32_t findInListOfActiveFormattingElementsContainsBetweenEndAndLastMarker(
417 void maybeForgetEarlierDuplicateFormattingElement(
418 nsAtom
* name
, nsHtml5HtmlAttributes
* attributes
);
419 int32_t findLastOrRoot(nsAtom
* name
);
420 int32_t findLastOrRoot(int32_t group
);
421 bool addAttributesToBody(nsHtml5HtmlAttributes
* attributes
);
422 void addAttributesToHtml(nsHtml5HtmlAttributes
* attributes
);
423 void pushHeadPointerOntoStack();
424 void reconstructTheActiveFormattingElements();
427 void notifyUnusedStackNode(int32_t idxInStackNodes
);
430 nsHtml5StackNode
* getUnusedStackNode();
431 nsHtml5StackNode
* createStackNode(
432 int32_t flags
, int32_t ns
, nsAtom
* name
, nsIContentHandle
* node
,
433 nsAtom
* popName
, nsHtml5HtmlAttributes
* attributes
,
434 mozilla::dom::HTMLContentCreatorFunction htmlCreator
);
435 nsHtml5StackNode
* createStackNode(nsHtml5ElementName
* elementName
,
436 nsIContentHandle
* node
);
437 nsHtml5StackNode
* createStackNode(nsHtml5ElementName
* elementName
,
438 nsIContentHandle
* node
,
439 nsHtml5HtmlAttributes
* attributes
);
440 nsHtml5StackNode
* createStackNode(nsHtml5ElementName
* elementName
,
441 nsIContentHandle
* node
, nsAtom
* popName
);
442 nsHtml5StackNode
* createStackNode(nsHtml5ElementName
* elementName
,
443 nsAtom
* popName
, nsIContentHandle
* node
);
444 nsHtml5StackNode
* createStackNode(nsHtml5ElementName
* elementName
,
445 nsIContentHandle
* node
, nsAtom
* popName
,
446 bool markAsIntegrationPoint
);
447 void insertIntoFosterParent(nsIContentHandle
* child
);
448 nsIContentHandle
* createAndInsertFosterParentedElement(
449 int32_t ns
, nsAtom
* name
, nsHtml5HtmlAttributes
* attributes
,
450 nsHtml5ContentCreatorFunction creator
);
451 nsIContentHandle
* createAndInsertFosterParentedElement(
452 int32_t ns
, nsAtom
* name
, nsHtml5HtmlAttributes
* attributes
,
453 nsIContentHandle
* form
, nsHtml5ContentCreatorFunction creator
);
454 bool isInStack(nsHtml5StackNode
* node
);
455 void popTemplateMode();
457 void popForeign(int32_t origPos
, int32_t eltPos
);
460 void appendHtmlElementToDocumentAndPush(nsHtml5HtmlAttributes
* attributes
);
461 void appendHtmlElementToDocumentAndPush();
462 void appendToCurrentNodeAndPushHeadElement(nsHtml5HtmlAttributes
* attributes
);
463 void appendToCurrentNodeAndPushBodyElement(nsHtml5HtmlAttributes
* attributes
);
464 void appendToCurrentNodeAndPushBodyElement();
465 void appendToCurrentNodeAndPushFormElementMayFoster(
466 nsHtml5HtmlAttributes
* attributes
);
467 void appendToCurrentNodeAndPushFormattingElementMayFoster(
468 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
469 void appendToCurrentNodeAndPushElement(nsHtml5ElementName
* elementName
,
470 nsHtml5HtmlAttributes
* attributes
);
471 void appendToCurrentNodeAndPushElementMayFoster(
472 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
473 void appendToCurrentNodeAndPushElementMayFosterMathML(
474 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
475 bool annotationXmlEncodingPermitsHtml(nsHtml5HtmlAttributes
* attributes
);
476 void appendToCurrentNodeAndPushElementMayFosterSVG(
477 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
478 void appendToCurrentNodeAndPushElementMayFoster(
479 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
,
480 nsIContentHandle
* form
);
481 void appendVoidElementToCurrentMayFoster(nsHtml5ElementName
* elementName
,
482 nsHtml5HtmlAttributes
* attributes
,
483 nsIContentHandle
* form
);
484 void appendVoidElementToCurrentMayFoster(nsHtml5ElementName
* elementName
,
485 nsHtml5HtmlAttributes
* attributes
);
486 void appendVoidElementToCurrentMayFosterSVG(
487 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
488 void appendVoidElementToCurrentMayFosterMathML(
489 nsHtml5ElementName
* elementName
, nsHtml5HtmlAttributes
* attributes
);
490 void appendVoidInputToCurrent(nsHtml5HtmlAttributes
* attributes
,
491 nsIContentHandle
* form
);
492 void appendVoidFormToCurrent(nsHtml5HtmlAttributes
* attributes
);
495 void accumulateCharacters(const char16_t
* buf
, int32_t start
, int32_t length
);
496 void requestSuspension();
497 nsIContentHandle
* createElement(int32_t ns
, nsAtom
* name
,
498 nsHtml5HtmlAttributes
* attributes
,
499 nsIContentHandle
* intendedParent
,
500 nsHtml5ContentCreatorFunction creator
);
501 nsIContentHandle
* createElement(int32_t ns
, nsAtom
* name
,
502 nsHtml5HtmlAttributes
* attributes
,
503 nsIContentHandle
* form
,
504 nsIContentHandle
* intendedParent
,
505 nsHtml5ContentCreatorFunction creator
);
506 nsIContentHandle
* createHtmlElementSetAsRoot(
507 nsHtml5HtmlAttributes
* attributes
);
508 void detachFromParent(nsIContentHandle
* element
);
509 bool hasChildren(nsIContentHandle
* element
);
510 void appendElement(nsIContentHandle
* child
, nsIContentHandle
* newParent
);
511 void appendChildrenToNewParent(nsIContentHandle
* oldParent
,
512 nsIContentHandle
* newParent
);
513 void insertFosterParentedChild(nsIContentHandle
* child
,
514 nsIContentHandle
* table
,
515 nsIContentHandle
* stackParent
);
516 nsIContentHandle
* createAndInsertFosterParentedElement(
517 int32_t ns
, nsAtom
* name
, nsHtml5HtmlAttributes
* attributes
,
518 nsIContentHandle
* form
, nsIContentHandle
* table
,
519 nsIContentHandle
* stackParent
, nsHtml5ContentCreatorFunction creator
);
521 void insertFosterParentedCharacters(char16_t
* buf
, int32_t start
,
522 int32_t length
, nsIContentHandle
* table
,
523 nsIContentHandle
* stackParent
);
524 void appendCharacters(nsIContentHandle
* parent
, char16_t
* buf
, int32_t start
,
526 void appendComment(nsIContentHandle
* parent
, char16_t
* buf
, int32_t start
,
528 void appendCommentToDocument(char16_t
* buf
, int32_t start
, int32_t length
);
529 void addAttributesToElement(nsIContentHandle
* element
,
530 nsHtml5HtmlAttributes
* attributes
);
531 void markMalformedIfScript(nsIContentHandle
* elt
);
532 void start(bool fragmentMode
);
534 void appendDoctypeToDocument(nsAtom
* name
, nsHtml5String publicIdentifier
,
535 nsHtml5String systemIdentifier
);
536 void elementPushed(int32_t ns
, nsAtom
* name
, nsIContentHandle
* node
);
537 void elementPopped(int32_t ns
, nsAtom
* name
, nsIContentHandle
* node
);
540 inline bool cdataSectionAllowed() { return isInForeign(); }
544 bool isInForeignButNotHtmlOrMathTextIntegrationPoint();
547 void setFragmentContext(nsAtom
* context
, int32_t ns
, nsIContentHandle
* node
,
551 nsIContentHandle
* currentNode();
554 bool isScriptingEnabled();
555 void setScriptingEnabled(bool scriptingEnabled
);
556 void setIsSrcdocDocument(bool isSrcdocDocument
);
557 void flushCharacters();
560 bool charBufferContainsNonWhitespace();
563 nsAHtml5TreeBuilderState
* newSnapshot();
564 bool snapshotMatches(nsAHtml5TreeBuilderState
* snapshot
);
565 void loadState(nsAHtml5TreeBuilderState
* snapshot
);
568 int32_t findInArray(nsHtml5StackNode
* node
,
569 jArray
<nsHtml5StackNode
*, int32_t> arr
);
570 nsIContentHandle
* nodeFromStackWithBlinkCompat(int32_t stackPos
);
573 nsIContentHandle
* getFormPointer() override
;
574 nsIContentHandle
* getHeadPointer() override
;
575 jArray
<nsHtml5StackNode
*, int32_t> getListOfActiveFormattingElements()
577 jArray
<nsHtml5StackNode
*, int32_t> getStack() override
;
578 jArray
<int32_t, int32_t> getTemplateModeStack() override
;
579 int32_t getMode() override
;
580 int32_t getOriginalMode() override
;
581 bool isFramesetOk() override
;
582 bool isNeedToDropLF() override
;
583 bool isQuirks() override
;
584 int32_t getListOfActiveFormattingElementsLength() override
;
585 int32_t getStackLength() override
;
586 int32_t getTemplateModeStackLength() override
;
587 static void initializeStatics();
588 static void releaseStatics();
590 #include "nsHtml5TreeBuilderHSupplement.h"