3 Object.defineProperty(exports, '__esModule', {
6 exports.serialize = exports.test = undefined;
8 var _markup = require('./lib/markup');
11 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
13 * This source code is licensed under the MIT license found in the
14 * LICENSE file in the root directory of this source tree.
19 const ELEMENT_NODE = 1;
21 const COMMENT_NODE = 8;
22 const FRAGMENT_NODE = 11;
24 const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
26 const testNode = (nodeType, name) =>
27 (nodeType === ELEMENT_NODE && ELEMENT_REGEXP.test(name)) ||
28 (nodeType === TEXT_NODE && name === 'Text') ||
29 (nodeType === COMMENT_NODE && name === 'Comment') ||
30 (nodeType === FRAGMENT_NODE && name === 'DocumentFragment');
32 const test = (exports.test = val =>
35 val.constructor.name &&
36 testNode(val.nodeType, val.constructor.name));
38 // Convert array of attribute objects to keys array and props object.
39 const keysMapper = attribute => attribute.name;
40 const propsReducer = (props, attribute) => {
41 props[attribute.name] = attribute.value;
45 const serialize = (exports.serialize = (
53 if (node.nodeType === TEXT_NODE) {
54 return (0, _markup.printText)(node.data, config);
57 if (node.nodeType === COMMENT_NODE) {
58 return (0, _markup.printComment)(node.data, config);
62 node.nodeType === FRAGMENT_NODE
64 : node.tagName.toLowerCase();
66 if (++depth > config.maxDepth) {
67 return (0, _markup.printElementAsLeaf)(type, config);
70 return (0, _markup.printElement)(
72 (0, _markup.printProps)(
73 Array.prototype.map.call(node.attributes || [], keysMapper).sort(),
74 Array.prototype.reduce.call(node.attributes || [], propsReducer, {}),
76 indentation + config.indent,
81 (0, _markup.printChildren)(
82 Array.prototype.slice.call(node.childNodes || node.children),
84 indentation + config.indent,
94 exports.default = {serialize: serialize, test: test};