react_element.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.test = exports.serialize = undefined;
  6. var _markup = require('./lib/markup');
  7. /**
  8. * Copyright (c) 2014-present, Facebook, Inc. 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. *
  14. */
  15. const elementSymbol = Symbol.for('react.element');
  16. const fragmentSymbol = Symbol.for('react.fragment');
  17. const forwardRefSymbol = Symbol.for('react.forward_ref');
  18. const providerSymbol = Symbol.for('react.provider');
  19. const contextSymbol = Symbol.for('react.context');
  20. // Given element.props.children, or subtree during recursive traversal,
  21. // return flattened array of children.
  22. const getChildren = function(arg) {
  23. let children =
  24. arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  25. if (Array.isArray(arg)) {
  26. arg.forEach(item => {
  27. getChildren(item, children);
  28. });
  29. } else if (arg != null && arg !== false) {
  30. children.push(arg);
  31. }
  32. return children;
  33. };
  34. const getType = element => {
  35. const type = element.type;
  36. if (typeof type === 'string') {
  37. return type;
  38. }
  39. if (typeof type === 'function') {
  40. return type.displayName || type.name || 'Unknown';
  41. }
  42. if (type === fragmentSymbol) {
  43. return 'React.Fragment';
  44. }
  45. if (typeof type === 'object' && type !== null) {
  46. if (type.$$typeof === providerSymbol) {
  47. return 'Context.Provider';
  48. }
  49. if (type.$$typeof === contextSymbol) {
  50. return 'Context.Consumer';
  51. }
  52. if (type.$$typeof === forwardRefSymbol) {
  53. const functionName = type.render.displayName || type.render.name || '';
  54. return functionName !== ''
  55. ? 'ForwardRef(' + functionName + ')'
  56. : 'ForwardRef';
  57. }
  58. }
  59. return 'UNDEFINED';
  60. };
  61. const getPropKeys = element => {
  62. const props = element.props;
  63. return Object.keys(props)
  64. .filter(key => key !== 'children' && props[key] !== undefined)
  65. .sort();
  66. };
  67. const serialize = (exports.serialize = (
  68. element,
  69. config,
  70. indentation,
  71. depth,
  72. refs,
  73. printer
  74. ) =>
  75. ++depth > config.maxDepth
  76. ? (0, _markup.printElementAsLeaf)(getType(element), config)
  77. : (0, _markup.printElement)(
  78. getType(element),
  79. (0, _markup.printProps)(
  80. getPropKeys(element),
  81. element.props,
  82. config,
  83. indentation + config.indent,
  84. depth,
  85. refs,
  86. printer
  87. ),
  88. (0, _markup.printChildren)(
  89. getChildren(element.props.children),
  90. config,
  91. indentation + config.indent,
  92. depth,
  93. refs,
  94. printer
  95. ),
  96. config,
  97. indentation
  98. ));
  99. const test = (exports.test = val => val && val.$$typeof === elementSymbol);
  100. exports.default = {serialize: serialize, test: test};