react_element.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. 'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.test = exports.serialize = undefined;
  2. var _markup = require('./lib/markup'); /**
  3. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. *
  9. */const elementSymbol = Symbol.for('react.element');
  10. // Given element.props.children, or subtree during recursive traversal,
  11. // return flattened array of children.
  12. const getChildren = function (arg) {let children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  13. if (Array.isArray(arg)) {
  14. arg.forEach(item => {
  15. getChildren(item, children);
  16. });
  17. } else if (arg != null && arg !== false) {
  18. children.push(arg);
  19. }
  20. return children;
  21. };
  22. const getType = element => {
  23. if (typeof element.type === 'string') {
  24. return element.type;
  25. }
  26. if (typeof element.type === 'function') {
  27. return element.type.displayName || element.type.name || 'Unknown';
  28. }
  29. return 'UNDEFINED';
  30. };
  31. const serialize = exports.serialize = (
  32. element,
  33. config,
  34. indentation,
  35. depth,
  36. refs,
  37. printer) =>
  38. ++depth > config.maxDepth ?
  39. (0, _markup.printElementAsLeaf)(getType(element), config) :
  40. (0, _markup.printElement)(
  41. getType(element),
  42. (0, _markup.printProps)(
  43. Object.keys(element.props).
  44. filter(key => key !== 'children').
  45. sort(),
  46. element.props,
  47. config,
  48. indentation + config.indent,
  49. depth,
  50. refs,
  51. printer),
  52. (0, _markup.printChildren)(
  53. getChildren(element.props.children),
  54. config,
  55. indentation + config.indent,
  56. depth,
  57. refs,
  58. printer),
  59. config,
  60. indentation);
  61. const test = exports.test = val => val && val.$$typeof === elementSymbol;exports.default =
  62. { serialize, test };