pretty-dom.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
  16. const shouldHighlight = () => {
  17. if (typeof process === 'undefined') {
  18. // Don't colorize in non-node environments (e.g. Browsers)
  19. return false;
  20. }
  21. let colors;
  22. // Try to safely parse env COLORS: We will default behavior if any step fails.
  23. try {
  24. const colorsJSON = process.env?.COLORS;
  25. if (colorsJSON) {
  26. colors = JSON.parse(colorsJSON);
  27. }
  28. } catch {
  29. // If this throws, process.env?.COLORS wasn't parsable. Since we only
  30. // care about `true` or `false`, we can safely ignore the error.
  31. }
  32. if (typeof colors === 'boolean') {
  33. // If `colors` is set explicitly (both `true` and `false`), use that value.
  34. return colors;
  35. } else {
  36. // If `colors` is not set, colorize if we're in node.
  37. return process.versions !== undefined && process.versions.node !== undefined;
  38. }
  39. };
  40. const {
  41. DOMCollection
  42. } = prettyFormat.plugins;
  43. // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants
  44. const ELEMENT_NODE = 1;
  45. const COMMENT_NODE = 8;
  46. // https://github.com/facebook/jest/blob/615084195ae1ae61ddd56162c62bbdda17587569/packages/pretty-format/src/plugins/DOMElement.ts#L50
  47. function filterCommentsAndDefaultIgnoreTagsTags(value) {
  48. return value.nodeType !== COMMENT_NODE && (value.nodeType !== ELEMENT_NODE || !value.matches((0, _config.getConfig)().defaultIgnore));
  49. }
  50. function prettyDOM(dom, maxLength, options = {}) {
  51. if (!dom) {
  52. dom = (0, _helpers.getDocument)().body;
  53. }
  54. if (typeof maxLength !== 'number') {
  55. maxLength = typeof process !== 'undefined' && typeof process.env !== 'undefined' && process.env.DEBUG_PRINT_LIMIT || 7000;
  56. }
  57. if (maxLength === 0) {
  58. return '';
  59. }
  60. if (dom.documentElement) {
  61. dom = dom.documentElement;
  62. }
  63. let domTypeName = typeof dom;
  64. if (domTypeName === 'object') {
  65. domTypeName = dom.constructor.name;
  66. } else {
  67. // To don't fall with `in` operator
  68. dom = {};
  69. }
  70. if (!('outerHTML' in dom)) {
  71. throw new TypeError(`Expected an element or document but got ${domTypeName}`);
  72. }
  73. const {
  74. filterNode = filterCommentsAndDefaultIgnoreTagsTags,
  75. ...prettyFormatOptions
  76. } = options;
  77. const debugContent = prettyFormat.format(dom, {
  78. plugins: [(0, _DOMElementFilter.default)(filterNode), DOMCollection],
  79. printFunctionName: false,
  80. highlight: shouldHighlight(),
  81. ...prettyFormatOptions
  82. });
  83. return maxLength !== undefined && dom.outerHTML.length > maxLength ? `${debugContent.slice(0, maxLength)}...` : debugContent;
  84. }
  85. const logDOM = (...args) => {
  86. const userCodeFrame = (0, _getUserCodeFrame.getUserCodeFrame)();
  87. if (userCodeFrame) {
  88. console.log(`${prettyDOM(...args)}\n\n${userCodeFrame}`);
  89. } else {
  90. console.log(prettyDOM(...args));
  91. }
  92. };
  93. exports.logDOM = logDOM;