react_element.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // Given element.props.children, or subtree during recursive traversal,
  17. // return flattened array of children.
  18. const getChildren = function (arg) {
  19. let children = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  20. if (Array.isArray(arg)) {
  21. arg.forEach(item => {
  22. getChildren(item, children);
  23. });
  24. } else if (arg != null && arg !== false) {
  25. children.push(arg);
  26. }
  27. return children;
  28. };
  29. const getType = element => {
  30. if (typeof element.type === 'string') {
  31. return element.type;
  32. }
  33. if (typeof element.type === 'function') {
  34. return element.type.displayName || element.type.name || 'Unknown';
  35. }
  36. return 'UNDEFINED';
  37. };
  38. const serialize = exports.serialize = (element, config, indentation, depth, refs, printer) => ++depth > config.maxDepth ? (0, _markup.printElementAsLeaf)(getType(element), config) : (0, _markup.printElement)(getType(element), (0, _markup.printProps)(Object.keys(element.props).filter(key => key !== 'children').sort(), element.props, config, indentation + config.indent, depth, refs, printer), (0, _markup.printChildren)(getChildren(element.props.children), config, indentation + config.indent, depth, refs, printer), config, indentation);
  39. const test = exports.test = val => val && val.$$typeof === elementSymbol;
  40. exports.default = { serialize, test };