Parser.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import Tokenizer from "./Tokenizer";
  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): 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. }): 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 {
  80. /** The start index of the last event. */
  81. startIndex: number;
  82. /** The end index of the last event. */
  83. endIndex: number | null;
  84. private tagname;
  85. private attribname;
  86. private attribvalue;
  87. private attribs;
  88. private stack;
  89. private readonly foreignContext;
  90. private readonly cbs;
  91. private readonly options;
  92. private readonly lowerCaseTagNames;
  93. private readonly lowerCaseAttributeNames;
  94. private readonly tokenizer;
  95. constructor(cbs: Partial<Handler> | null, options?: ParserOptions);
  96. private updatePosition;
  97. ontext(data: string): void;
  98. onopentagname(name: string): void;
  99. onopentagend(): void;
  100. onclosetag(name: string): void;
  101. onselfclosingtag(): void;
  102. private closeCurrentTag;
  103. onattribname(name: string): void;
  104. onattribdata(value: string): void;
  105. onattribend(quote: string | undefined | null): void;
  106. private getInstructionName;
  107. ondeclaration(value: string): void;
  108. onprocessinginstruction(value: string): void;
  109. oncomment(value: string): void;
  110. oncdata(value: string): void;
  111. onerror(err: Error): void;
  112. onend(): void;
  113. /**
  114. * Resets the parser to a blank state, ready to parse a new HTML document
  115. */
  116. reset(): void;
  117. /**
  118. * Resets the parser, then parses a complete document and
  119. * pushes it to the handler.
  120. *
  121. * @param data Document to parse.
  122. */
  123. parseComplete(data: string): void;
  124. /**
  125. * Parses a chunk of data and calls the corresponding callbacks.
  126. *
  127. * @param chunk Chunk to parse.
  128. */
  129. write(chunk: string): void;
  130. /**
  131. * Parses the end of the buffer and clears the stack, calls onend.
  132. *
  133. * @param chunk Optional final chunk to parse.
  134. */
  135. end(chunk?: string): void;
  136. /**
  137. * Pauses parsing. The parser won't emit events until `resume` is called.
  138. */
  139. pause(): void;
  140. /**
  141. * Resumes parsing after `pause` was called.
  142. */
  143. resume(): void;
  144. /**
  145. * Alias of `write`, for backwards compatibility.
  146. *
  147. * @param chunk Chunk to parse.
  148. * @deprecated
  149. */
  150. parseChunk(chunk: string): void;
  151. /**
  152. * Alias of `end`, for backwards compatibility.
  153. *
  154. * @param chunk Optional final chunk to parse.
  155. * @deprecated
  156. */
  157. done(chunk?: string): void;
  158. }
  159. //# sourceMappingURL=Parser.d.ts.map