DOMElement.js 3.0 KB

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