|
1 | 1 | import prettyFormat from 'pretty-format'
|
| 2 | +import {getDocument} from './helpers' |
| 3 | + |
| 4 | +function inCypress(dom) { |
| 5 | + const window = |
| 6 | + (dom.ownerDocument && dom.ownerDocument.defaultView) || undefined |
| 7 | + return ( |
| 8 | + (typeof global !== 'undefined' && global.Cypress) || |
| 9 | + (typeof window !== 'undefined' && window.Cypress) |
| 10 | + ) |
| 11 | +} |
| 12 | + |
| 13 | +const inNode = () => |
| 14 | + typeof process !== 'undefined' && |
| 15 | + process.versions !== undefined && |
| 16 | + process.versions.node !== undefined |
| 17 | + |
| 18 | +const getMaxLength = dom => |
| 19 | + inCypress(dom) ? 0 : process.env.DEBUG_PRINT_LIMIT || 7000 |
2 | 20 |
|
3 | 21 | const {DOMElement, DOMCollection} = prettyFormat.plugins
|
4 | 22 |
|
5 |
| -function prettyDOM(htmlElement, maxLength, options) { |
6 |
| - if (htmlElement.documentElement) { |
7 |
| - htmlElement = htmlElement.documentElement |
| 23 | +function prettyDOM( |
| 24 | + dom = getDocument().body, |
| 25 | + maxLength = getMaxLength(dom), |
| 26 | + options, |
| 27 | +) { |
| 28 | + if (maxLength === 0) { |
| 29 | + return '' |
| 30 | + } |
| 31 | + if (dom.documentElement) { |
| 32 | + dom = dom.documentElement |
8 | 33 | }
|
9 | 34 |
|
10 |
| - const debugContent = prettyFormat(htmlElement, { |
| 35 | + const debugContent = prettyFormat(dom, { |
11 | 36 | plugins: [DOMElement, DOMCollection],
|
12 | 37 | printFunctionName: false,
|
13 |
| - highlight: true, |
| 38 | + highlight: inNode(), |
14 | 39 | ...options,
|
15 | 40 | })
|
16 |
| - return maxLength !== undefined && htmlElement.outerHTML.length > maxLength |
| 41 | + return maxLength !== undefined && dom.outerHTML.length > maxLength |
17 | 42 | ? `${debugContent.slice(0, maxLength)}...`
|
18 | 43 | : debugContent
|
19 | 44 | }
|
20 | 45 |
|
21 |
| -export {prettyDOM} |
| 46 | +const logDOM = (...args) => console.log(prettyDOM(...args)) |
| 47 | + |
| 48 | +export {prettyDOM, logDOM} |
| 49 | + |
| 50 | +/* eslint no-console:0 */ |
0 commit comments