-
Notifications
You must be signed in to change notification settings - Fork 459
/
pretty-dom.js
68 lines (57 loc) 路 1.6 KB
/
pretty-dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import prettyFormat from 'pretty-format'
import {getDocument} from './helpers'
function inCypress(dom) {
const window =
(dom.ownerDocument && dom.ownerDocument.defaultView) || undefined
return (
(typeof global !== 'undefined' && global.Cypress) ||
(typeof window !== 'undefined' && window.Cypress)
)
}
const inNode = () =>
typeof process !== 'undefined' &&
process.versions !== undefined &&
process.versions.node !== undefined
const getMaxLength = dom =>
inCypress(dom)
? 0
: typeof process !== 'undefined' && process.env.DEBUG_PRINT_LIMIT || 7000
const {DOMElement, DOMCollection} = prettyFormat.plugins
function prettyDOM(dom, maxLength, options) {
if (!dom) {
dom = getDocument().body
}
if (typeof maxLength !== 'number') {
maxLength = getMaxLength(dom)
}
if (maxLength === 0) {
return ''
}
if (dom.documentElement) {
dom = dom.documentElement
}
let domTypeName = typeof dom
if (domTypeName === 'object') {
domTypeName = dom.constructor.name
} else {
// To don't fall with `in` operator
dom = {}
}
if (!('outerHTML' in dom)) {
throw new TypeError(
`Expected an element or document but got ${domTypeName}`,
)
}
const debugContent = prettyFormat(dom, {
plugins: [DOMElement, DOMCollection],
printFunctionName: false,
highlight: inNode(),
...options,
})
return maxLength !== undefined && dom.outerHTML.length > maxLength
? `${debugContent.slice(0, maxLength)}...`
: debugContent
}
const logDOM = (...args) => console.log(prettyDOM(...args))
export {prettyDOM, logDOM}
/* eslint no-console:0 */