index.d.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Tokenizer, TokenizerMode, type TokenHandler } from '../tokenizer/index.js';
  2. import { OpenElementStack, type StackHandler } from './open-element-stack.js';
  3. import { FormattingElementList } from './formatting-element-list.js';
  4. import { ERR, type ParserErrorHandler } from '../common/error-codes.js';
  5. import { TAG_ID as $, NS } from '../common/html.js';
  6. import type { TreeAdapter, TreeAdapterTypeMap } from '../tree-adapters/interface.js';
  7. import { type Token, type CommentToken, type CharacterToken, type TagToken, type DoctypeToken, type EOFToken, type LocationWithAttributes } from '../common/token.js';
  8. declare enum InsertionMode {
  9. INITIAL = 0,
  10. BEFORE_HTML = 1,
  11. BEFORE_HEAD = 2,
  12. IN_HEAD = 3,
  13. IN_HEAD_NO_SCRIPT = 4,
  14. AFTER_HEAD = 5,
  15. IN_BODY = 6,
  16. TEXT = 7,
  17. IN_TABLE = 8,
  18. IN_TABLE_TEXT = 9,
  19. IN_CAPTION = 10,
  20. IN_COLUMN_GROUP = 11,
  21. IN_TABLE_BODY = 12,
  22. IN_ROW = 13,
  23. IN_CELL = 14,
  24. IN_SELECT = 15,
  25. IN_SELECT_IN_TABLE = 16,
  26. IN_TEMPLATE = 17,
  27. AFTER_BODY = 18,
  28. IN_FRAMESET = 19,
  29. AFTER_FRAMESET = 20,
  30. AFTER_AFTER_BODY = 21,
  31. AFTER_AFTER_FRAMESET = 22
  32. }
  33. export interface ParserOptions<T extends TreeAdapterTypeMap> {
  34. /**
  35. * The [scripting flag](https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag). If set
  36. * to `true`, `noscript` element content will be parsed as text.
  37. *
  38. * @default `true`
  39. */
  40. scriptingEnabled?: boolean;
  41. /**
  42. * Enables source code location information. When enabled, each node (except the root node)
  43. * will have a `sourceCodeLocation` property. If the node is not an empty element, `sourceCodeLocation` will
  44. * be a {@link ElementLocation} object, otherwise it will be {@link Location}.
  45. * If the element was implicitly created by the parser (as part of
  46. * [tree correction](https://html.spec.whatwg.org/multipage/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser)),
  47. * its `sourceCodeLocation` property will be `undefined`.
  48. *
  49. * @default `false`
  50. */
  51. sourceCodeLocationInfo?: boolean;
  52. /**
  53. * Specifies the resulting tree format.
  54. *
  55. * @default `treeAdapters.default`
  56. */
  57. treeAdapter?: TreeAdapter<T>;
  58. /**
  59. * Callback for parse errors.
  60. *
  61. * @default `null`
  62. */
  63. onParseError?: ParserErrorHandler | null;
  64. }
  65. export declare class Parser<T extends TreeAdapterTypeMap> implements TokenHandler, StackHandler<T> {
  66. fragmentContext: T['element'] | null;
  67. scriptHandler: null | ((pendingScript: T['element']) => void);
  68. treeAdapter: TreeAdapter<T>;
  69. onParseError: ParserErrorHandler | null;
  70. private currentToken;
  71. options: Required<ParserOptions<T>>;
  72. document: T['document'];
  73. constructor(options?: ParserOptions<T>, document?: T['document'], fragmentContext?: T['element'] | null, scriptHandler?: null | ((pendingScript: T['element']) => void));
  74. static parse<T extends TreeAdapterTypeMap>(html: string, options?: ParserOptions<T>): T['document'];
  75. static getFragmentParser<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): Parser<T>;
  76. getFragment(): T['documentFragment'];
  77. tokenizer: Tokenizer;
  78. stopped: boolean;
  79. insertionMode: InsertionMode;
  80. originalInsertionMode: InsertionMode;
  81. fragmentContextID: $;
  82. headElement: null | T['element'];
  83. formElement: null | T['element'];
  84. openElements: OpenElementStack<T>;
  85. activeFormattingElements: FormattingElementList<T>;
  86. /** Indicates that the current node is not an element in the HTML namespace */
  87. private currentNotInHTML;
  88. /**
  89. * The template insertion mode stack is maintained from the left.
  90. * Ie. the topmost element will always have index 0.
  91. */
  92. tmplInsertionModeStack: InsertionMode[];
  93. pendingCharacterTokens: CharacterToken[];
  94. hasNonWhitespacePendingCharacterToken: boolean;
  95. framesetOk: boolean;
  96. skipNextNewLine: boolean;
  97. fosterParentingEnabled: boolean;
  98. _err(token: Token, code: ERR, beforeToken?: boolean): void;
  99. onItemPush(node: T['parentNode'], tid: number, isTop: boolean): void;
  100. onItemPop(node: T['parentNode'], isTop: boolean): void;
  101. private _setContextModes;
  102. _switchToTextParsing(currentToken: TagToken, nextTokenizerState: typeof TokenizerMode[keyof typeof TokenizerMode]): void;
  103. switchToPlaintextParsing(): void;
  104. _getAdjustedCurrentElement(): T['element'];
  105. _findFormInFragmentContext(): void;
  106. private _initTokenizerForFragmentParsing;
  107. _setDocumentType(token: DoctypeToken): void;
  108. _attachElementToTree(element: T['element'], location: LocationWithAttributes | null): void;
  109. _appendElement(token: TagToken, namespaceURI: NS): void;
  110. _insertElement(token: TagToken, namespaceURI: NS): void;
  111. _insertFakeElement(tagName: string, tagID: $): void;
  112. _insertTemplate(token: TagToken): void;
  113. _insertFakeRootElement(): void;
  114. _appendCommentNode(token: CommentToken, parent: T['parentNode']): void;
  115. _insertCharacters(token: CharacterToken): void;
  116. _adoptNodes(donor: T['parentNode'], recipient: T['parentNode']): void;
  117. _setEndLocation(element: T['element'], closingToken: Token): void;
  118. private shouldProcessStartTagTokenInForeignContent;
  119. _processToken(token: Token): void;
  120. _isIntegrationPoint(tid: $, element: T['element'], foreignNS?: NS): boolean;
  121. _reconstructActiveFormattingElements(): void;
  122. _closeTableCell(): void;
  123. _closePElement(): void;
  124. _resetInsertionMode(): void;
  125. _resetInsertionModeForSelect(selectIdx: number): void;
  126. _isElementCausesFosterParenting(tn: $): boolean;
  127. _shouldFosterParentOnInsertion(): boolean;
  128. _findFosterParentingLocation(): {
  129. parent: T['parentNode'];
  130. beforeElement: T['element'] | null;
  131. };
  132. _fosterParentElement(element: T['element']): void;
  133. _isSpecialElement(element: T['element'], id: $): boolean;
  134. onCharacter(token: CharacterToken): void;
  135. onNullCharacter(token: CharacterToken): void;
  136. onComment(token: CommentToken): void;
  137. onDoctype(token: DoctypeToken): void;
  138. onStartTag(token: TagToken): void;
  139. /**
  140. * Processes a given start tag.
  141. *
  142. * `onStartTag` checks if a self-closing tag was recognized. When a token
  143. * is moved inbetween multiple insertion modes, this check for self-closing
  144. * could lead to false positives. To avoid this, `_processStartTag` is used
  145. * for nested calls.
  146. *
  147. * @param token The token to process.
  148. */
  149. _processStartTag(token: TagToken): void;
  150. _startTagOutsideForeignContent(token: TagToken): void;
  151. onEndTag(token: TagToken): void;
  152. _endTagOutsideForeignContent(token: TagToken): void;
  153. onEof(token: EOFToken): void;
  154. onWhitespaceCharacter(token: CharacterToken): void;
  155. }
  156. export {};
  157. //# sourceMappingURL=index.d.ts.map