.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / pretty-format / build / plugins / dom_element.js
1 'use strict';
2
3 Object.defineProperty(exports, '__esModule', {
4   value: true
5 });
6 exports.serialize = exports.test = undefined;
7
8 var _markup = require('./lib/markup');
9
10 /**
11  * Copyright (c) 2014-present, Facebook, Inc. 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  *
16  *
17  */
18
19 const ELEMENT_NODE = 1;
20 const TEXT_NODE = 3;
21 const COMMENT_NODE = 8;
22 const FRAGMENT_NODE = 11;
23
24 const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/;
25
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');
31
32 const test = (exports.test = val =>
33   val &&
34   val.constructor &&
35   val.constructor.name &&
36   testNode(val.nodeType, val.constructor.name));
37
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;
42   return props;
43 };
44
45 const serialize = (exports.serialize = (
46   node,
47   config,
48   indentation,
49   depth,
50   refs,
51   printer
52 ) => {
53   if (node.nodeType === TEXT_NODE) {
54     return (0, _markup.printText)(node.data, config);
55   }
56
57   if (node.nodeType === COMMENT_NODE) {
58     return (0, _markup.printComment)(node.data, config);
59   }
60
61   const type =
62     node.nodeType === FRAGMENT_NODE
63       ? `DocumentFragment`
64       : node.tagName.toLowerCase();
65
66   if (++depth > config.maxDepth) {
67     return (0, _markup.printElementAsLeaf)(type, config);
68   }
69
70   return (0, _markup.printElement)(
71     type,
72     (0, _markup.printProps)(
73       Array.prototype.map.call(node.attributes || [], keysMapper).sort(),
74       Array.prototype.reduce.call(node.attributes || [], propsReducer, {}),
75       config,
76       indentation + config.indent,
77       depth,
78       refs,
79       printer
80     ),
81     (0, _markup.printChildren)(
82       Array.prototype.slice.call(node.childNodes || node.children),
83       config,
84       indentation + config.indent,
85       depth,
86       refs,
87       printer
88     ),
89     config,
90     indentation
91   );
92 });
93
94 exports.default = {serialize: serialize, test: test};