1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = exports.serialize = exports.test = void 0;
|
7 |
|
8 | var _markup = require('./lib/markup');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const ELEMENT_NODE = 1;
|
17 | const TEXT_NODE = 3;
|
18 | const COMMENT_NODE = 8;
|
19 | const FRAGMENT_NODE = 11;
|
20 | const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
|
21 |
|
22 | const testHasAttribute = val => {
|
23 | try {
|
24 | return typeof val.hasAttribute === 'function' && val.hasAttribute('is');
|
25 | } catch {
|
26 | return false;
|
27 | }
|
28 | };
|
29 |
|
30 | const testNode = val => {
|
31 | const constructorName = val.constructor.name;
|
32 | const {nodeType, tagName} = val;
|
33 | const isCustomElement =
|
34 | (typeof tagName === 'string' && tagName.includes('-')) ||
|
35 | testHasAttribute(val);
|
36 | return (
|
37 | (nodeType === ELEMENT_NODE &&
|
38 | (ELEMENT_REGEXP.test(constructorName) || isCustomElement)) ||
|
39 | (nodeType === TEXT_NODE && constructorName === 'Text') ||
|
40 | (nodeType === COMMENT_NODE && constructorName === 'Comment') ||
|
41 | (nodeType === FRAGMENT_NODE && constructorName === 'DocumentFragment')
|
42 | );
|
43 | };
|
44 |
|
45 | const test = val => {
|
46 | var _val$constructor;
|
47 |
|
48 | return (
|
49 | (val === null || val === void 0
|
50 | ? void 0
|
51 | : (_val$constructor = val.constructor) === null ||
|
52 | _val$constructor === void 0
|
53 | ? void 0
|
54 | : _val$constructor.name) && testNode(val)
|
55 | );
|
56 | };
|
57 |
|
58 | exports.test = test;
|
59 |
|
60 | function nodeIsText(node) {
|
61 | return node.nodeType === TEXT_NODE;
|
62 | }
|
63 |
|
64 | function nodeIsComment(node) {
|
65 | return node.nodeType === COMMENT_NODE;
|
66 | }
|
67 |
|
68 | function nodeIsFragment(node) {
|
69 | return node.nodeType === FRAGMENT_NODE;
|
70 | }
|
71 |
|
72 | const serialize = (node, config, indentation, depth, refs, printer) => {
|
73 | if (nodeIsText(node)) {
|
74 | return (0, _markup.printText)(node.data, config);
|
75 | }
|
76 |
|
77 | if (nodeIsComment(node)) {
|
78 | return (0, _markup.printComment)(node.data, config);
|
79 | }
|
80 |
|
81 | const type = nodeIsFragment(node)
|
82 | ? `DocumentFragment`
|
83 | : node.tagName.toLowerCase();
|
84 |
|
85 | if (++depth > config.maxDepth) {
|
86 | return (0, _markup.printElementAsLeaf)(type, config);
|
87 | }
|
88 |
|
89 | return (0, _markup.printElement)(
|
90 | type,
|
91 | (0, _markup.printProps)(
|
92 | nodeIsFragment(node)
|
93 | ? []
|
94 | : Array.from(node.attributes)
|
95 | .map(attr => attr.name)
|
96 | .sort(),
|
97 | nodeIsFragment(node)
|
98 | ? {}
|
99 | : Array.from(node.attributes).reduce((props, attribute) => {
|
100 | props[attribute.name] = attribute.value;
|
101 | return props;
|
102 | }, {}),
|
103 | config,
|
104 | indentation + config.indent,
|
105 | depth,
|
106 | refs,
|
107 | printer
|
108 | ),
|
109 | (0, _markup.printChildren)(
|
110 | Array.prototype.slice.call(node.childNodes || node.children),
|
111 | config,
|
112 | indentation + config.indent,
|
113 | depth,
|
114 | refs,
|
115 | printer
|
116 | ),
|
117 | config,
|
118 | indentation
|
119 | );
|
120 | };
|
121 |
|
122 | exports.serialize = serialize;
|
123 | const plugin = {
|
124 | serialize,
|
125 | test
|
126 | };
|
127 | var _default = plugin;
|
128 | exports.default = _default;
|