index.d.ts 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /// <reference types="node" resolution-mode="require"/>
  2. import { Writable } from 'node:stream';
  3. import { Parser, type ParserOptions, type TreeAdapterTypeMap, type DefaultTreeAdapterMap } from 'parse5';
  4. /**
  5. * Streaming HTML parser with scripting support.
  6. * A [writable stream](https://nodejs.org/api/stream.html#stream_class_stream_writable).
  7. *
  8. * @example
  9. *
  10. * ```js
  11. * const ParserStream = require('parse5-parser-stream');
  12. * const http = require('http');
  13. * const { finished } = require('node:stream');
  14. *
  15. * // Fetch the page content and obtain it's <head> node
  16. * http.get('http://inikulin.github.io/parse5/', res => {
  17. * const parser = new ParserStream();
  18. *
  19. * finished(parser, () => {
  20. * console.log(parser.document.childNodes[1].childNodes[0].tagName); //> 'head'
  21. * });
  22. *
  23. * res.pipe(parser);
  24. * });
  25. * ```
  26. *
  27. */
  28. export declare class ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> extends Writable {
  29. parser: Parser<T>;
  30. static getFragmentStream<T extends TreeAdapterTypeMap>(fragmentContext?: T['parentNode'] | null, options?: ParserOptions<T>): ParserStream<T>;
  31. private lastChunkWritten;
  32. private writeCallback;
  33. private pendingHtmlInsertions;
  34. /** The resulting document node. */
  35. get document(): T['document'];
  36. getFragment(): T['documentFragment'];
  37. /**
  38. * @param options Parsing options.
  39. */
  40. constructor(options?: ParserOptions<T>, parser?: Parser<T>);
  41. _write(chunk: string, _encoding: string, callback: () => void): void;
  42. end(chunk?: any, encoding?: any, callback?: any): any;
  43. }
  44. export interface ParserStream<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap> {
  45. /**
  46. * Raised when parser encounters a `<script>` element. If this event has listeners, parsing will be suspended once
  47. * it is emitted. So, if `<script>` has the `src` attribute, you can fetch it, execute and then resume parsing just
  48. * like browsers do.
  49. *
  50. * @example
  51. *
  52. * ```js
  53. * const ParserStream = require('parse5-parser-stream');
  54. * const http = require('http');
  55. *
  56. * const parser = new ParserStream();
  57. *
  58. * parser.on('script', (scriptElement, documentWrite, resume) => {
  59. * const src = scriptElement.attrs.find(({ name }) => name === 'src').value;
  60. *
  61. * http.get(src, res => {
  62. * // Fetch the script content, execute it with DOM built around `parser.document` and
  63. * // `document.write` implemented using `documentWrite`.
  64. * ...
  65. * // Then resume parsing.
  66. * resume();
  67. * });
  68. * });
  69. *
  70. * parser.end('<script src="example.com/script.js"></script>');
  71. * ```
  72. *
  73. * @param event Name of the event
  74. * @param handler
  75. */
  76. on(event: 'script', handler: (scriptElement: T['element'], documentWrite: (html: string) => void, resume: () => void) => void): void;
  77. /**
  78. * Base event handler.
  79. *
  80. * @param event Name of the event
  81. * @param handler Event handler
  82. */
  83. on(event: string, handler: (...args: any[]) => void): this;
  84. }
  85. //# sourceMappingURL=index.d.ts.map