Sha256: f62b3b07ca41b9a15d0bf5fe876ea99c96ef2f6b8e1d2a0462d68966c3aee0a1

Contents?: true

Size: 1.95 KB

Versions: 81

Compression:

Stored size: 1.95 KB

Contents

'use strict';

const printString = require('../printString');

const reactElement = Symbol.for('react.element');

function traverseChildren(opaqueChildren, cb) {
  if (Array.isArray(opaqueChildren)) {
    opaqueChildren.forEach(child => traverseChildren(child, cb));
  } else if (opaqueChildren != null && opaqueChildren !== false) {
    cb(opaqueChildren);
  }
}

function printChildren(flatChildren, print, indent, opts) {
  return flatChildren.map(node => {
    if (typeof node === 'object') {
      return printElement(node, print, indent, opts);
    } else if (typeof node === 'string') {
      return printString(node);
    } else {
      return print(node);
    }
  }).join(opts.edgeSpacing);
}

function printProps(props, print, indent, opts) {
  return Object.keys(props).sort().map(name => {
    if (name === 'children') {
      return '';
    }

    const prop = props[name];
    let printed = print(prop);

    if (typeof prop !== 'string') {
      if (printed.indexOf('\n') !== -1) {
        printed = '{' + opts.edgeSpacing + indent(indent(printed) + opts.edgeSpacing + '}');
      } else {
        printed = '{' + printed + '}';
      }
    }

    return opts.spacing + indent(name + '=') + printed;
  }).join('');
}

function printElement(element, print, indent, opts) {
  let result = '<' + element.type;
  result += printProps(element.props, print, indent, opts);

  const opaqueChildren = element.props.children;
  if (opaqueChildren) {
    let flatChildren = [];
    traverseChildren(opaqueChildren, child => {
      flatChildren.push(child);
    });
    const children = printChildren(flatChildren, print, indent, opts);
    result += '>' + opts.edgeSpacing + indent(children) + opts.edgeSpacing + '</' + element.type + '>';
  } else {
    result += ' />';
  }

  return result;
}

module.exports = {
  test(object) {
    return object && object.$$typeof === reactElement;
  },
  print(val, print, indent, opts) {
    return printElement(val, print, indent, opts);
  }
};

Version data entries

81 entries across 81 versions & 1 rubygems

Version Path
isomorfeus-preact-10.6.50 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.49 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.48 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.47 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.46 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.45 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.44 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.43 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.42 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.41 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.40 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.39 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.38 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.37 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.36 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.35 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.34 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.33 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.32 node_modules/pretty-format/plugins/ReactElement.js
isomorfeus-preact-10.6.31 node_modules/pretty-format/plugins/ReactElement.js