index.d.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { type ParserOptions } from './parser/index.js';
  2. import type { DefaultTreeAdapterMap } from './tree-adapters/default.js';
  3. import type { TreeAdapterTypeMap } from './tree-adapters/interface.js';
  4. export { type DefaultTreeAdapterMap, defaultTreeAdapter } from './tree-adapters/default.js';
  5. import type * as DefaultTreeAdapter from './tree-adapters/default.js';
  6. export declare namespace DefaultTreeAdapterTypes {
  7. type Document = DefaultTreeAdapter.Document;
  8. type DocumentFragment = DefaultTreeAdapter.DocumentFragment;
  9. type Element = DefaultTreeAdapter.Element;
  10. type CommentNode = DefaultTreeAdapter.CommentNode;
  11. type TextNode = DefaultTreeAdapter.TextNode;
  12. type Template = DefaultTreeAdapter.Template;
  13. type DocumentType = DefaultTreeAdapter.DocumentType;
  14. type ParentNode = DefaultTreeAdapter.ParentNode;
  15. type ChildNode = DefaultTreeAdapter.ChildNode;
  16. type Node = DefaultTreeAdapter.Node;
  17. type DefaultTreeAdapterMap = DefaultTreeAdapter.DefaultTreeAdapterMap;
  18. }
  19. export type { TreeAdapter, TreeAdapterTypeMap } from './tree-adapters/interface.js';
  20. export { type ParserOptions, /** @internal */ Parser } from './parser/index.js';
  21. export { serialize, serializeOuter, type SerializerOptions } from './serializer/index.js';
  22. export { ERR as ErrorCodes, type ParserError, type ParserErrorHandler } from './common/error-codes.js';
  23. /** @internal */
  24. export * as foreignContent from './common/foreign-content.js';
  25. export * as html from './common/html.js';
  26. export * as Token from './common/token.js';
  27. /** @internal */
  28. export { Tokenizer, type TokenizerOptions, TokenizerMode, type TokenHandler } from './tokenizer/index.js';
  29. /**
  30. * Parses an HTML string.
  31. *
  32. * @param html Input HTML string.
  33. * @param options Parsing options.
  34. * @returns Document
  35. *
  36. * @example
  37. *
  38. * ```js
  39. * const parse5 = require('parse5');
  40. *
  41. * const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
  42. *
  43. * console.log(document.childNodes[1].tagName); //> 'html'
  44. *```
  45. */
  46. export declare function parse<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap>(html: string, options?: ParserOptions<T>): T['document'];
  47. /**
  48. * Parses an HTML fragment.
  49. *
  50. * @example
  51. *
  52. * ```js
  53. * const parse5 = require('parse5');
  54. *
  55. * const documentFragment = parse5.parseFragment('<table></table>');
  56. *
  57. * console.log(documentFragment.childNodes[0].tagName); //> 'table'
  58. *
  59. * // Parses the html fragment in the context of the parsed <table> element.
  60. * const trFragment = parse5.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
  61. *
  62. * console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
  63. * ```
  64. *
  65. * @param fragmentContext Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's `innerHTML` property.
  66. * @param html Input HTML fragment string.
  67. * @param options Parsing options.
  68. * @returns DocumentFragment
  69. */
  70. export declare function parseFragment<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap>(fragmentContext: T['parentNode'] | null, html: string, options: ParserOptions<T>): T['documentFragment'];
  71. export declare function parseFragment<T extends TreeAdapterTypeMap = DefaultTreeAdapterMap>(html: string, options?: ParserOptions<T>): T['documentFragment'];