Parser.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import Tokenizer, { Callbacks, QuoteType } from "./Tokenizer.js";
  2. export interface ParserOptions {
  3. /**
  4. * Indicates whether special tags (`<script>`, `<style>`, and `<title>`) should get special treatment
  5. * and if "empty" tags (eg. `<br>`) can have children. If `false`, the content of special tags
  6. * will be text only. For feeds and other XML content (documents that don't consist of HTML),
  7. * set this to `true`.
  8. *
  9. * @default false
  10. */
  11. xmlMode?: boolean;
  12. /**
  13. * Decode entities within the document.
  14. *
  15. * @default true
  16. */
  17. decodeEntities?: boolean;
  18. /**
  19. * If set to true, all tags will be lowercased.
  20. *
  21. * @default !xmlMode
  22. */
  23. lowerCaseTags?: boolean;
  24. /**
  25. * If set to `true`, all attribute names will be lowercased. This has noticeable impact on speed.
  26. *
  27. * @default !xmlMode
  28. */
  29. lowerCaseAttributeNames?: boolean;
  30. /**
  31. * If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled.
  32. * NOTE: If xmlMode is set to `true` then CDATA sections will always be recognized as text.
  33. *
  34. * @default xmlMode
  35. */
  36. recognizeCDATA?: boolean;
  37. /**
  38. * If set to `true`, self-closing tags will trigger the onclosetag event even if xmlMode is not set to `true`.
  39. * NOTE: If xmlMode is set to `true` then self-closing tags will always be recognized.
  40. *
  41. * @default xmlMode
  42. */
  43. recognizeSelfClosing?: boolean;
  44. /**
  45. * Allows the default tokenizer to be overwritten.
  46. */
  47. Tokenizer?: typeof Tokenizer;
  48. }
  49. export interface Handler {
  50. onparserinit(parser: Parser): void;
  51. /**
  52. * Resets the handler back to starting state
  53. */
  54. onreset(): void;
  55. /**
  56. * Signals the handler that parsing is done
  57. */
  58. onend(): void;
  59. onerror(error: Error): void;
  60. onclosetag(name: string, isImplied: boolean): void;
  61. onopentagname(name: string): void;
  62. /**
  63. *
  64. * @param name Name of the attribute
  65. * @param value Value of the attribute.
  66. * @param quote Quotes used around the attribute. `null` if the attribute has no quotes around the value, `undefined` if the attribute has no value.
  67. */
  68. onattribute(name: string, value: string, quote?: string | undefined | null): void;
  69. onopentag(name: string, attribs: {
  70. [s: string]: string;
  71. }, isImplied: boolean): void;
  72. ontext(data: string): void;
  73. oncomment(data: string): void;
  74. oncdatastart(): void;
  75. oncdataend(): void;
  76. oncommentend(): void;
  77. onprocessinginstruction(name: string, data: string): void;
  78. }
  79. export declare class Parser implements Callbacks {
  80. private readonly options;
  81. /** The start index of the last event. */
  82. startIndex: number;
  83. /** The end index of the last event. */
  84. endIndex: number;
  85. /**
  86. * Store the start index of the current open tag,
  87. * so we can update the start index for attributes.
  88. */
  89. private openTagStart;
  90. private tagname;
  91. private attribname;
  92. private attribvalue;
  93. private attribs;
  94. private stack;
  95. private readonly foreignContext;
  96. private readonly cbs;
  97. private readonly lowerCaseTagNames;
  98. private readonly lowerCaseAttributeNames;
  99. private readonly tokenizer;
  100. private readonly buffers;
  101. private bufferOffset;
  102. /** The index of the last written buffer. Used when resuming after a `pause()`. */
  103. private writeIndex;
  104. /** Indicates whether the parser has finished running / `.end` has been called. */
  105. private ended;
  106. constructor(cbs?: Partial<Handler> | null, options?: ParserOptions);
  107. /** @internal */
  108. ontext(start: number, endIndex: number): void;
  109. /** @internal */
  110. ontextentity(cp: number): void;
  111. protected isVoidElement(name: string): boolean;
  112. /** @internal */
  113. onopentagname(start: number, endIndex: number): void;
  114. private emitOpenTag;
  115. private endOpenTag;
  116. /** @internal */
  117. onopentagend(endIndex: number): void;
  118. /** @internal */
  119. onclosetag(start: number, endIndex: number): void;
  120. /** @internal */
  121. onselfclosingtag(endIndex: number): void;
  122. private closeCurrentTag;
  123. /** @internal */
  124. onattribname(start: number, endIndex: number): void;
  125. /** @internal */
  126. onattribdata(start: number, endIndex: number): void;
  127. /** @internal */
  128. onattribentity(cp: number): void;
  129. /** @internal */
  130. onattribend(quote: QuoteType, endIndex: number): void;
  131. private getInstructionName;
  132. /** @internal */
  133. ondeclaration(start: number, endIndex: number): void;
  134. /** @internal */
  135. onprocessinginstruction(start: number, endIndex: number): void;
  136. /** @internal */
  137. oncomment(start: number, endIndex: number, offset: number): void;
  138. /** @internal */
  139. oncdata(start: number, endIndex: number, offset: number): void;
  140. /** @internal */
  141. onend(): void;
  142. /**
  143. * Resets the parser to a blank state, ready to parse a new HTML document
  144. */
  145. reset(): void;
  146. /**
  147. * Resets the parser, then parses a complete document and
  148. * pushes it to the handler.
  149. *
  150. * @param data Document to parse.
  151. */
  152. parseComplete(data: string): void;
  153. private getSlice;
  154. private shiftBuffer;
  155. /**
  156. * Parses a chunk of data and calls the corresponding callbacks.
  157. *
  158. * @param chunk Chunk to parse.
  159. */
  160. write(chunk: string): void;
  161. /**
  162. * Parses the end of the buffer and clears the stack, calls onend.
  163. *
  164. * @param chunk Optional final chunk to parse.
  165. */
  166. end(chunk?: string): void;
  167. /**
  168. * Pauses parsing. The parser won't emit events until `resume` is called.
  169. */
  170. pause(): void;
  171. /**
  172. * Resumes parsing after `pause` was called.
  173. */
  174. resume(): void;
  175. /**
  176. * Alias of `write`, for backwards compatibility.
  177. *
  178. * @param chunk Chunk to parse.
  179. * @deprecated
  180. */
  181. parseChunk(chunk: string): void;
  182. /**
  183. * Alias of `end`, for backwards compatibility.
  184. *
  185. * @param chunk Optional final chunk to parse.
  186. * @deprecated
  187. */
  188. done(chunk?: string): void;
  189. }
  190. //# sourceMappingURL=Parser.d.ts.map