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