Skip to content

Commit 0a8ad65

Browse files
authored
feat: Support anonymous custom elements when pretty printing DOM (#1319)
1 parent 77448ba commit 0a8ad65

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

src/DOMElementFilter.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -158,24 +158,30 @@ const FRAGMENT_NODE = 11
158158

159159
const ELEMENT_REGEXP = /^((HTML|SVG)\w*)?Element$/
160160

161+
const isCustomElement = (val: any) => {
162+
const {tagName} = val
163+
return Boolean(
164+
(typeof tagName === 'string' && tagName.includes('-')) ||
165+
(typeof val.hasAttribute === 'function' && val.hasAttribute('is')),
166+
)
167+
}
168+
161169
const testNode = (val: any) => {
162170
const constructorName = val.constructor.name
163-
const {nodeType, tagName} = val
164-
const isCustomElement =
165-
(typeof tagName === 'string' && tagName.includes('-')) ||
166-
(typeof val.hasAttribute === 'function' && val.hasAttribute('is'))
171+
172+
const {nodeType} = val
167173

168174
return (
169175
(nodeType === ELEMENT_NODE &&
170-
(ELEMENT_REGEXP.test(constructorName) || isCustomElement)) ||
176+
(ELEMENT_REGEXP.test(constructorName) || isCustomElement(val))) ||
171177
(nodeType === TEXT_NODE && constructorName === 'Text') ||
172178
(nodeType === COMMENT_NODE && constructorName === 'Comment') ||
173179
(nodeType === FRAGMENT_NODE && constructorName === 'DocumentFragment')
174180
)
175181
}
176182

177183
export const test: NewPlugin['test'] = (val: any) =>
178-
val?.constructor?.name && testNode(val)
184+
(val?.constructor?.name || isCustomElement(val)) && testNode(val)
179185

180186
type HandledType = Element | Text | Comment | DocumentFragment
181187

@@ -195,7 +201,8 @@ export default function createDOMElementFilter(
195201
filterNode: (node: Node) => boolean,
196202
): NewPlugin {
197203
return {
198-
test: (val: any) => val?.constructor?.name && testNode(val),
204+
test: (val: any) =>
205+
(val?.constructor?.name || isCustomElement(val)) && testNode(val),
199206
serialize: (
200207
node: HandledType,
201208
config: Config,

src/__tests__/pretty-dom.js

+31
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,34 @@ test('prettyDOM supports a COLORS environment variable', () => {
166166
process.env.COLORS = 'true'
167167
expect(prettyDOM(container)).toEqual(withColors)
168168
})
169+
170+
test('prettyDOM supports named custom elements', () => {
171+
window.customElements.define(
172+
'my-element-1',
173+
class MyElement extends HTMLElement {},
174+
)
175+
176+
const {container} = render('<my-element-1>Hello World!</my-element-1>')
177+
178+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
179+
<div>
180+
<my-element-1>
181+
Hello World!
182+
</my-element-1>
183+
</div>
184+
`)
185+
})
186+
187+
test('prettyDOM supports anonymous custom elements', () => {
188+
window.customElements.define('my-element-2', class extends HTMLElement {})
189+
190+
const {container} = render('<my-element-2>Hello World!</my-element-2>')
191+
192+
expect(prettyDOM(container)).toMatchInlineSnapshot(`
193+
<div>
194+
<my-element-2>
195+
Hello World!
196+
</my-element-2>
197+
</div>
198+
`)
199+
})

0 commit comments

Comments
 (0)