DOMCollection.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.test = exports.serialize = exports.default = void 0;
  6. var _collections = require('../collections');
  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. /* eslint-disable local/ban-types-eventually */
  14. const SPACE = ' ';
  15. const OBJECT_NAMES = ['DOMStringMap', 'NamedNodeMap'];
  16. const ARRAY_REGEXP = /^(HTML\w*Collection|NodeList)$/;
  17. const testName = name =>
  18. OBJECT_NAMES.indexOf(name) !== -1 || ARRAY_REGEXP.test(name);
  19. const test = val =>
  20. val &&
  21. val.constructor &&
  22. !!val.constructor.name &&
  23. testName(val.constructor.name);
  24. exports.test = test;
  25. const isNamedNodeMap = collection =>
  26. collection.constructor.name === 'NamedNodeMap';
  27. const serialize = (collection, config, indentation, depth, refs, printer) => {
  28. const name = collection.constructor.name;
  29. if (++depth > config.maxDepth) {
  30. return '[' + name + ']';
  31. }
  32. return (
  33. (config.min ? '' : name + SPACE) +
  34. (OBJECT_NAMES.indexOf(name) !== -1
  35. ? '{' +
  36. (0, _collections.printObjectProperties)(
  37. isNamedNodeMap(collection)
  38. ? Array.from(collection).reduce((props, attribute) => {
  39. props[attribute.name] = attribute.value;
  40. return props;
  41. }, {})
  42. : {...collection},
  43. config,
  44. indentation,
  45. depth,
  46. refs,
  47. printer
  48. ) +
  49. '}'
  50. : '[' +
  51. (0, _collections.printListItems)(
  52. Array.from(collection),
  53. config,
  54. indentation,
  55. depth,
  56. refs,
  57. printer
  58. ) +
  59. ']')
  60. );
  61. };
  62. exports.serialize = serialize;
  63. const plugin = {
  64. serialize,
  65. test
  66. };
  67. var _default = plugin;
  68. exports.default = _default;