index.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Parser } from "./Parser.js";
  2. export { Parser } from "./Parser.js";
  3. import { DomHandler, } from "domhandler";
  4. export { DomHandler,
  5. // Old name for DomHandler
  6. DomHandler as DefaultHandler, } from "domhandler";
  7. // Helper methods
  8. /**
  9. * Parses the data, returns the resulting document.
  10. *
  11. * @param data The data that should be parsed.
  12. * @param options Optional options for the parser and DOM handler.
  13. */
  14. export function parseDocument(data, options) {
  15. const handler = new DomHandler(undefined, options);
  16. new Parser(handler, options).end(data);
  17. return handler.root;
  18. }
  19. /**
  20. * Parses data, returns an array of the root nodes.
  21. *
  22. * Note that the root nodes still have a `Document` node as their parent.
  23. * Use `parseDocument` to get the `Document` node instead.
  24. *
  25. * @param data The data that should be parsed.
  26. * @param options Optional options for the parser and DOM handler.
  27. * @deprecated Use `parseDocument` instead.
  28. */
  29. export function parseDOM(data, options) {
  30. return parseDocument(data, options).children;
  31. }
  32. /**
  33. * Creates a parser instance, with an attached DOM handler.
  34. *
  35. * @param callback A callback that will be called once parsing has been completed, with the resulting document.
  36. * @param options Optional options for the parser and DOM handler.
  37. * @param elementCallback An optional callback that will be called every time a tag has been completed inside of the DOM.
  38. */
  39. export function createDocumentStream(callback, options, elementCallback) {
  40. const handler = new DomHandler((error) => callback(error, handler.root), options, elementCallback);
  41. return new Parser(handler, options);
  42. }
  43. /**
  44. * Creates a parser instance, with an attached DOM handler.
  45. *
  46. * @param callback A callback that will be called once parsing has been completed, with an array of root nodes.
  47. * @param options Optional options for the parser and DOM handler.
  48. * @param elementCallback An optional callback that will be called every time a tag has been completed inside of the DOM.
  49. * @deprecated Use `createDocumentStream` instead.
  50. */
  51. export function createDomStream(callback, options, elementCallback) {
  52. const handler = new DomHandler(callback, options, elementCallback);
  53. return new Parser(handler, options);
  54. }
  55. export { default as Tokenizer, QuoteType, } from "./Tokenizer.js";
  56. /*
  57. * All of the following exports exist for backwards-compatibility.
  58. * They should probably be removed eventually.
  59. */
  60. export * as ElementType from "domelementtype";
  61. import { getFeed } from "domutils";
  62. export { getFeed } from "domutils";
  63. const parseFeedDefaultOptions = { xmlMode: true };
  64. /**
  65. * Parse a feed.
  66. *
  67. * @param feed The feed that should be parsed, as a string.
  68. * @param options Optionally, options for parsing. When using this, you should set `xmlMode` to `true`.
  69. */
  70. export function parseFeed(feed, options = parseFeedDefaultOptions) {
  71. return getFeed(parseDOM(feed, options));
  72. }
  73. export * as DomUtils from "domutils";
  74. //# sourceMappingURL=index.js.map