index.d.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { SAXParser, type EndTag, type StartTag, type Doctype, type Text, type Comment, type SaxToken } from 'parse5-sax-parser';
  2. /**
  3. * Streaming [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML rewriter.
  4. * A [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) (which means you can pipe _through_ it, see example).
  5. *
  6. * The rewriter uses the raw source representation of tokens if they are not modified by the user. Therefore, the resulting
  7. * HTML is not affected by parser error-recovery mechanisms as in a classical parsing-serialization roundtrip.
  8. *
  9. * @example
  10. *
  11. * ```js
  12. * const RewritingStream = require('parse5-html-rewriting-stream');
  13. * const http = require('http');
  14. * const fs = require('fs');
  15. *
  16. * const file = fs.createWriteStream('/home/google.com.html');
  17. * const rewriter = new RewritingStream();
  18. *
  19. * // Replace divs with spans
  20. * rewriter.on('startTag', startTag => {
  21. * if (startTag.tagName === 'span') {
  22. * startTag.tagName = 'div';
  23. * }
  24. *
  25. * rewriter.emitStartTag(startTag);
  26. * });
  27. *
  28. * rewriter.on('endTag', endTag => {
  29. * if (endTag.tagName === 'span') {
  30. * endTag.tagName = 'div';
  31. * }
  32. *
  33. * rewriter.emitEndTag(endTag);
  34. * });
  35. *
  36. * // Wrap all text nodes with an <i> tag
  37. * rewriter.on('text', (_, raw) => {
  38. * // Use the raw representation of text without HTML entities decoding
  39. * rewriter.emitRaw(`<i>${raw}</i>`);
  40. * });
  41. *
  42. * http.get('http://google.com', res => {
  43. * // Assumes response is UTF-8.
  44. * res.setEncoding('utf8');
  45. * // `RewritingStream` is a `Transform` stream, which means you can pipe
  46. * // through it.
  47. * res.pipe(rewriter).pipe(file);
  48. * });
  49. * ```
  50. */
  51. export declare class RewritingStream extends SAXParser {
  52. /** Note: `sourceCodeLocationInfo` is always enabled. */
  53. constructor();
  54. _transformChunk(chunk: string): string;
  55. private _getRawHtml;
  56. protected emitIfListenerExists(eventName: string, token: SaxToken): boolean;
  57. protected _emitToken(eventName: string, token: SaxToken): void;
  58. /** Emits a serialized document type token into the output stream. */
  59. emitDoctype(token: Doctype): void;
  60. /** Emits a serialized start tag token into the output stream. */
  61. emitStartTag(token: StartTag): void;
  62. /** Emits a serialized end tag token into the output stream. */
  63. emitEndTag(token: EndTag): void;
  64. /** Emits a serialized text token into the output stream. */
  65. emitText({ text }: Text): void;
  66. /** Emits a serialized comment token into the output stream. */
  67. emitComment(token: Comment): void;
  68. /** Emits a raw HTML string into the output stream. */
  69. emitRaw(html: string): void;
  70. }
  71. export interface RewritingStream {
  72. /** Raised when the rewriter encounters a start tag. */
  73. on(event: 'startTag', listener: (startTag: StartTag, rawHtml: string) => void): this;
  74. /** Raised when rewriter encounters an end tag. */
  75. on(event: 'endTag', listener: (endTag: EndTag, rawHtml: string) => void): this;
  76. /** Raised when rewriter encounters a comment. */
  77. on(event: 'comment', listener: (comment: Comment, rawHtml: string) => void): this;
  78. /** Raised when rewriter encounters text content. */
  79. on(event: 'text', listener: (text: Text, rawHtml: string) => void): this;
  80. /** Raised when rewriter encounters a [document type declaration](https://en.wikipedia.org/wiki/Document_type_declaration). */
  81. on(event: 'doctype', listener: (doctype: Doctype, rawHtml: string) => void): this;
  82. /**
  83. * Base event handler.
  84. *
  85. * @param event Name of the event
  86. * @param handler Event handler
  87. */
  88. on(event: string, handler: (...args: any[]) => void): this;
  89. }
  90. //# sourceMappingURL=index.d.ts.map