index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 builder.
  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 builder.
  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.
  36. * @param options Optional options for the parser and DOM builder.
  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 createDomStream(callback, options, elementCallback) {
  40. const handler = new DomHandler(callback, options, elementCallback);
  41. return new Parser(handler, options);
  42. }
  43. export { default as Tokenizer, } from "./Tokenizer.js";
  44. /*
  45. * All of the following exports exist for backwards-compatibility.
  46. * They should probably be removed eventually.
  47. */
  48. export * as ElementType from "domelementtype";
  49. import { getFeed } from "domutils";
  50. export { getFeed } from "domutils";
  51. const parseFeedDefaultOptions = { xmlMode: true };
  52. /**
  53. * Parse a feed.
  54. *
  55. * @param feed The feed that should be parsed, as a string.
  56. * @param options Optionally, options for parsing. When using this, you should set `xmlMode` to `true`.
  57. */
  58. export function parseFeed(feed, options = parseFeedDefaultOptions) {
  59. return getFeed(parseDOM(feed, options));
  60. }
  61. export * as DomUtils from "domutils";
  62. //# sourceMappingURL=index.js.map