index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. var Parser = require('./parser'),
  3. Serializer = require('./serializer');
  4. // Shorthands
  5. exports.parse = function parse(html, options) {
  6. var parser = new Parser(options);
  7. return parser.parse(html);
  8. };
  9. exports.parseFragment = function parseFragment(fragmentContext, html, options) {
  10. if (typeof fragmentContext === 'string') {
  11. options = html;
  12. html = fragmentContext;
  13. fragmentContext = null;
  14. }
  15. var parser = new Parser(options);
  16. return parser.parseFragment(html, fragmentContext);
  17. };
  18. exports.serialize = function (node, options) {
  19. var serializer = new Serializer(node, options);
  20. return serializer.serialize();
  21. };
  22. // Tree adapters
  23. exports.treeAdapters = {
  24. default: require('./tree_adapters/default'),
  25. htmlparser2: require('./tree_adapters/htmlparser2')
  26. };
  27. // Streaming
  28. // NOTE: streaming API is lazy loadable to enable bundling for platforms
  29. // that are different from Node.js.
  30. // See https://github.com/inikulin/parse5/issues/235.
  31. var streamingAPI = {
  32. ParserStream: './parser/parser_stream',
  33. PlainTextConversionStream: './parser/plain_text_conversion_stream',
  34. SerializerStream: './serializer/serializer_stream',
  35. SAXParser: './sax'
  36. };
  37. Object.keys(streamingAPI).forEach(function (cls) {
  38. Object.defineProperty(exports, cls, {
  39. get: function () {
  40. try {
  41. return require(streamingAPI[cls]);
  42. }
  43. catch (e) {
  44. throw new Error(
  45. cls + ' is supported only for Node.js.' +
  46. 'See https://github.com/inikulin/parse5/issues/235 for the details.'
  47. );
  48. }
  49. }
  50. });
  51. });