index.js 2.1 KB

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