renderer.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const merge = require('merge-options');
  2. const renderer = require('posthtml-render');
  3. const api = require('posthtml/lib/api');
  4. const defaultOptions = {
  5. closingSingleTag: 'slash',
  6. singleTags: [
  7. 'circle',
  8. 'path',
  9. 'ellipse',
  10. 'line',
  11. 'path',
  12. 'polygon',
  13. 'polyline',
  14. 'rect',
  15. 'use',
  16. 'animateTransform',
  17. 'stop'
  18. ]
  19. };
  20. /**
  21. * @param {PostHTMLTree} tree
  22. * @param {Object|null} [options] {@see https://github.com/posthtml/posthtml-render#options}
  23. */
  24. module.exports = function xmlRenderer(tree, options) {
  25. const opts = merge(defaultOptions, options || {});
  26. /**
  27. * Workaround for https://github.com/fb55/htmlparser2/issues/187
  28. * Also see https://github.com/fb55/htmlparser2/pull/129
  29. */
  30. opts.singleTags = opts.singleTags.filter((tag) => {
  31. let hasContent = false;
  32. api.match.call(tree, { tag }, (node) => {
  33. if (typeof node.content !== 'undefined' && !hasContent) {
  34. hasContent = true;
  35. }
  36. return node;
  37. });
  38. return !hasContent;
  39. });
  40. return renderer(tree, opts);
  41. };
  42. module.exports.defaultOptions = defaultOptions;