UNPKG

3.07 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = exports.serialize = exports.test = void 0;
7
8var _markup = require('./lib/markup');
9
10/**
11 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
12 *
13 * This source code is licensed under the MIT license found in the
14 * LICENSE file in the root directory of this source tree.
15 */
16const ELEMENT_NODE = 1;
17const TEXT_NODE = 3;
18const COMMENT_NODE = 8;
19const FRAGMENT_NODE = 11;
20const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
21
22const testHasAttribute = val => {
23 try {
24 return typeof val.hasAttribute === 'function' && val.hasAttribute('is');
25 } catch {
26 return false;
27 }
28};
29
30const 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
45const 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
58exports.test = test;
59
60function nodeIsText(node) {
61 return node.nodeType === TEXT_NODE;
62}
63
64function nodeIsComment(node) {
65 return node.nodeType === COMMENT_NODE;
66}
67
68function nodeIsFragment(node) {
69 return node.nodeType === FRAGMENT_NODE;
70}
71
72const 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
122exports.serialize = serialize;
123const plugin = {
124 serialize,
125 test
126};
127var _default = plugin;
128exports.default = _default;