Skip to content

Commit da85a32

Browse files
authored
perf(pretty-format): combine DOMElement plugins (#9581)
1 parent 92ab06d commit da85a32

File tree

3 files changed

+64
-174
lines changed

3 files changed

+64
-174
lines changed

packages/pretty-format/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Immutable from './plugins/Immutable'
3030
import ReactElement from './plugins/ReactElement'
3131
import ReactTestComponent from './plugins/ReactTestComponent'
3232

33-
export { createDOMElementFilter } from './plugins/DOMElementFilter'
33+
export { createDOMElementFilter } from './plugins/DOMElement'
3434

3535
const toString = Object.prototype.toString
3636
const toISOString = Date.prototype.toISOString

packages/pretty-format/src/plugins/DOMElement.ts

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,40 @@ function nodeIsFragment(node: HandledType): node is DocumentFragment {
6565
return node.nodeType === FRAGMENT_NODE
6666
}
6767

68-
export const serialize: NewPlugin['serialize'] = (
68+
export interface FilterConfig extends Config {
69+
filterNode?: (node: any) => boolean
70+
}
71+
72+
function filterChildren(children: any[], filterNode?: (node: any) => boolean): any[] {
73+
// Filter out text nodes that only contain whitespace to prevent empty lines
74+
// This is done regardless of whether a filterNode is provided
75+
let filtered = children.filter((node) => {
76+
// Filter out text nodes that are only whitespace
77+
if (node.nodeType === TEXT_NODE) {
78+
const text = node.data || ''
79+
// Keep text nodes that have non-whitespace content
80+
return text.trim().length > 0
81+
}
82+
return true
83+
})
84+
85+
// Apply additional user-provided filter if specified
86+
if (filterNode) {
87+
filtered = filtered.filter(filterNode)
88+
}
89+
90+
return filtered
91+
}
92+
93+
function serializeDOM(
6994
node: HandledType,
7095
config: Config,
7196
indentation: string,
7297
depth: number,
7398
refs: Refs,
7499
printer: Printer,
75-
) => {
100+
filterNode?: (node: any) => boolean,
101+
) {
76102
if (nodeIsText(node)) {
77103
return printText(node.data, config)
78104
}
@@ -89,6 +115,14 @@ export const serialize: NewPlugin['serialize'] = (
89115
return printElementAsLeaf(type, config)
90116
}
91117

118+
const children = Array.prototype.slice.call(node.childNodes || node.children)
119+
const shadowChildren = (nodeIsFragment(node) || !node.shadowRoot)
120+
? []
121+
: Array.prototype.slice.call(node.shadowRoot.children)
122+
123+
const resolvedChildren = filterNode ? filterChildren(children, filterNode) : children
124+
const resolvedShadowChildren = filterNode ? filterChildren(shadowChildren, filterNode) : shadowChildren
125+
92126
return printElement(
93127
type,
94128
printProps(
@@ -110,11 +144,11 @@ export const serialize: NewPlugin['serialize'] = (
110144
refs,
111145
printer,
112146
),
113-
((nodeIsFragment(node) || !node.shadowRoot)
114-
? ''
115-
: printShadowRoot(Array.prototype.slice.call(node.shadowRoot.children), config, indentation + config.indent, depth, refs, printer))
147+
(resolvedShadowChildren.length > 0
148+
? printShadowRoot(resolvedShadowChildren, config, indentation + config.indent, depth, refs, printer)
149+
: '')
116150
+ printChildren(
117-
Array.prototype.slice.call(node.childNodes || node.children),
151+
resolvedChildren,
118152
config,
119153
indentation + config.indent,
120154
depth,
@@ -126,6 +160,29 @@ export const serialize: NewPlugin['serialize'] = (
126160
)
127161
}
128162

163+
export const serialize: NewPlugin['serialize'] = (
164+
node: HandledType,
165+
config: Config,
166+
indentation: string,
167+
depth: number,
168+
refs: Refs,
169+
printer: Printer,
170+
) => serializeDOM(node, config, indentation, depth, refs, printer)
171+
172+
export function createDOMElementFilter(filterNode?: (node: any) => boolean): NewPlugin {
173+
return {
174+
test,
175+
serialize: (
176+
node: HandledType,
177+
config: Config,
178+
indentation: string,
179+
depth: number,
180+
refs: Refs,
181+
printer: Printer,
182+
) => serializeDOM(node, config, indentation, depth, refs, printer, filterNode),
183+
}
184+
}
185+
129186
const plugin: NewPlugin = { serialize, test }
130187

131188
export default plugin

packages/pretty-format/src/plugins/DOMElementFilter.ts

Lines changed: 0 additions & 167 deletions
This file was deleted.

0 commit comments

Comments
 (0)