Parser.d.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /// <reference types="node" />
  2. import Tokenizer from "./Tokenizer";
  3. import { EventEmitter } from "events";
  4. export interface ParserOptions {
  5. /***
  6. * Indicates whether special tags (<script> and <style>) should get special treatment
  7. * and if "empty" tags (eg. <br>) can have children. If `false`, the content of special tags
  8. * will be text only. For feeds and other XML content (documents that don't consist of HTML),
  9. * set this to `true`. Default: `false`.
  10. */
  11. xmlMode?: boolean;
  12. /***
  13. * If set to true, entities within the document will be decoded. Defaults to `false`.
  14. */
  15. decodeEntities?: boolean;
  16. /***
  17. * If set to true, all tags will be lowercased. If xmlMode is disabled, this defaults to `true`.
  18. */
  19. lowerCaseTags?: boolean;
  20. /***
  21. * If set to `true`, all attribute names will be lowercased. This has noticeable impact on speed, so it defaults to `false`.
  22. */
  23. lowerCaseAttributeNames?: boolean;
  24. /***
  25. * If set to true, CDATA sections will be recognized as text even if the xmlMode option is not enabled.
  26. * NOTE: If xmlMode is set to `true` then CDATA sections will always be recognized as text.
  27. */
  28. recognizeCDATA?: boolean;
  29. /***
  30. * If set to `true`, self-closing tags will trigger the onclosetag event even if xmlMode is not set to `true`.
  31. * NOTE: If xmlMode is set to `true` then self-closing tags will always be recognized.
  32. */
  33. recognizeSelfClosing?: boolean;
  34. /**
  35. * Allows the default tokenizer to be overwritten.
  36. */
  37. Tokenizer?: typeof Tokenizer;
  38. }
  39. export interface Handler {
  40. onparserinit(parser: Parser): void;
  41. /***
  42. * Resets the handler back to starting state
  43. */
  44. onreset(): void;
  45. /***
  46. * Signals the handler that parsing is done
  47. */
  48. onend(): void;
  49. onerror(error: Error): void;
  50. onclosetag(name: string): void;
  51. onopentagname(name: string): void;
  52. onattribute(name: string, value: string): void;
  53. onopentag(name: string, attribs: {
  54. [s: string]: string;
  55. }): void;
  56. ontext(data: string): void;
  57. oncomment(data: string): void;
  58. oncdatastart(): void;
  59. oncdataend(): void;
  60. oncommentend(): void;
  61. onprocessinginstruction(name: string, data: string): void;
  62. }
  63. export declare class Parser extends EventEmitter {
  64. _tagname: string;
  65. _attribname: string;
  66. _attribvalue: string;
  67. _attribs: null | {
  68. [key: string]: string;
  69. };
  70. _stack: string[];
  71. _foreignContext: boolean[];
  72. startIndex: number;
  73. endIndex: number | null;
  74. _cbs: Partial<Handler>;
  75. _options: ParserOptions;
  76. _lowerCaseTagNames: boolean;
  77. _lowerCaseAttributeNames: boolean;
  78. _tokenizer: Tokenizer;
  79. constructor(cbs: Partial<Handler> | null, options?: ParserOptions);
  80. _updatePosition(initialOffset: number): void;
  81. ontext(data: string): void;
  82. onopentagname(name: string): void;
  83. onopentagend(): void;
  84. onclosetag(name: string): void;
  85. onselfclosingtag(): void;
  86. _closeCurrentTag(): void;
  87. onattribname(name: string): void;
  88. onattribdata(value: string): void;
  89. onattribend(): void;
  90. _getInstructionName(value: string): string;
  91. ondeclaration(value: string): void;
  92. onprocessinginstruction(value: string): void;
  93. oncomment(value: string): void;
  94. oncdata(value: string): void;
  95. onerror(err: Error): void;
  96. onend(): void;
  97. reset(): void;
  98. parseComplete(data: string): void;
  99. write(chunk: string): void;
  100. end(chunk?: string): void;
  101. pause(): void;
  102. resume(): void;
  103. parseChunk: (chunk: string) => void;
  104. done: (chunk?: string | undefined) => void;
  105. }
  106. //# sourceMappingURL=Parser.d.ts.map