DOMCollection.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.serialize = exports.test = void 0;
  6. var _collections = require('../collections');
  7. function _objectSpread(target) {
  8. for (var i = 1; i < arguments.length; i++) {
  9. var source = arguments[i] != null ? arguments[i] : {};
  10. var ownKeys = Object.keys(source);
  11. if (typeof Object.getOwnPropertySymbols === 'function') {
  12. ownKeys = ownKeys.concat(
  13. Object.getOwnPropertySymbols(source).filter(function(sym) {
  14. return Object.getOwnPropertyDescriptor(source, sym).enumerable;
  15. })
  16. );
  17. }
  18. ownKeys.forEach(function(key) {
  19. _defineProperty(target, key, source[key]);
  20. });
  21. }
  22. return target;
  23. }
  24. function _defineProperty(obj, key, value) {
  25. if (key in obj) {
  26. Object.defineProperty(obj, key, {
  27. value: value,
  28. enumerable: true,
  29. configurable: true,
  30. writable: true
  31. });
  32. } else {
  33. obj[key] = value;
  34. }
  35. return obj;
  36. }
  37. const SPACE = ' ';
  38. const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
  39. const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;
  40. const testName = name =>
  41. OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name);
  42. const test = val =>
  43. val &&
  44. val.constructor &&
  45. val.constructor.name &&
  46. testName(val.constructor.name); // Convert array of attribute objects to props object.
  47. exports.test = test;
  48. const propsReducer = (props, attribute) => {
  49. props[attribute.name] = attribute.value;
  50. return props;
  51. };
  52. const serialize = (collection, config, indentation, depth, refs, printer) => {
  53. const name = collection.constructor.name;
  54. if (++depth > config.maxDepth) {
  55. return '[' + name + ']';
  56. }
  57. return (
  58. (config.min ? '' : name + SPACE) +
  59. (OBJECT_NAMES.indexOf(name) !== -1
  60. ? '{' +
  61. (0, _collections.printObjectProperties)(
  62. name === 'NamedNodeMap'
  63. ? Array.prototype.reduce.call(collection, propsReducer, {})
  64. : _objectSpread({}, collection),
  65. config,
  66. indentation,
  67. depth,
  68. refs,
  69. printer
  70. ) +
  71. '}'
  72. : '[' +
  73. (0, _collections.printListItems)(
  74. Array.from(collection),
  75. config,
  76. indentation,
  77. depth,
  78. refs,
  79. printer
  80. ) +
  81. ']')
  82. );
  83. };
  84. exports.serialize = serialize;
  85. const plugin = {
  86. serialize,
  87. test
  88. };
  89. var _default = plugin;
  90. exports.default = _default;