pretty-dom.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.logDOM = void 0;
  7. exports.prettyDOM = prettyDOM;
  8. exports.prettyFormat = void 0;
  9. var prettyFormat = _interopRequireWildcard(require("pretty-format"));
  10. exports.prettyFormat = prettyFormat;
  11. var _DOMElementFilter = _interopRequireDefault(require("./DOMElementFilter"));
  12. var _getUserCodeFrame = require("./get-user-code-frame");
  13. var _helpers = require("./helpers");
  14. var _config = require("./config");
  15. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  16. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  17. const shouldHighlight = () => {
  18. let colors;
  19. try {
  20. var _process, _process$env;
  21. colors = JSON.parse((_process = process) == null ? void 0 : (_process$env = _process.env) == null ? void 0 : _process$env.COLORS);
  22. } catch (e) {
  23. // If this throws, process?.env?.COLORS wasn't parsable. Since we only
  24. // care about `true` or `false`, we can safely ignore the error.
  25. }
  26. if (typeof colors === 'boolean') {
  27. // If `colors` is set explicitly (both `true` and `false`), use that value.
  28. return colors;
  29. } else {
  30. // If `colors` is not set, colorize if we're in node.
  31. return typeof process !== 'undefined' && process.versions !== undefined && process.versions.node !== undefined;
  32. }
  33. };
  34. const {
  35. DOMCollection
  36. } = prettyFormat.plugins;
  37. // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants
  38. const ELEMENT_NODE = 1;
  39. const COMMENT_NODE = 8;
  40. // https://github.com/facebook/jest/blob/615084195ae1ae61ddd56162c62bbdda17587569/packages/pretty-format/src/plugins/DOMElement.ts#L50
  41. function filterCommentsAndDefaultIgnoreTagsTags(value) {
  42. return value.nodeType !== COMMENT_NODE && (value.nodeType !== ELEMENT_NODE || !value.matches((0, _config.getConfig)().defaultIgnore));
  43. }
  44. function prettyDOM(dom, maxLength, options = {}) {
  45. if (!dom) {
  46. dom = (0, _helpers.getDocument)().body;
  47. }
  48. if (typeof maxLength !== 'number') {
  49. maxLength = typeof process !== 'undefined' && process.env.DEBUG_PRINT_LIMIT || 7000;
  50. }
  51. if (maxLength === 0) {
  52. return '';
  53. }
  54. if (dom.documentElement) {
  55. dom = dom.documentElement;
  56. }
  57. let domTypeName = typeof dom;
  58. if (domTypeName === 'object') {
  59. domTypeName = dom.constructor.name;
  60. } else {
  61. // To don't fall with `in` operator
  62. dom = {};
  63. }
  64. if (!('outerHTML' in dom)) {
  65. throw new TypeError(`Expected an element or document but got ${domTypeName}`);
  66. }
  67. const {
  68. filterNode = filterCommentsAndDefaultIgnoreTagsTags,
  69. ...prettyFormatOptions
  70. } = options;
  71. const debugContent = prettyFormat.format(dom, {
  72. plugins: [(0, _DOMElementFilter.default)(filterNode), DOMCollection],
  73. printFunctionName: false,
  74. highlight: shouldHighlight(),
  75. ...prettyFormatOptions
  76. });
  77. return maxLength !== undefined && dom.outerHTML.length > maxLength ? `${debugContent.slice(0, maxLength)}...` : debugContent;
  78. }
  79. const logDOM = (...args) => {
  80. const userCodeFrame = (0, _getUserCodeFrame.getUserCodeFrame)();
  81. if (userCodeFrame) {
  82. console.log(`${prettyDOM(...args)}\n\n${userCodeFrame}`);
  83. } else {
  84. console.log(prettyDOM(...args));
  85. }
  86. };
  87. exports.logDOM = logDOM;