-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add filterNode option to prettyDOM for filtering browser assertion error output #9475
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9ccdc54
Initial plan
Copilot 5169daa
Add filterNode option to prettyDOM for filtering output in browser mode
Copilot 37380ac
Address PR feedback: add inline snapshot test, mark as experimental, …
Copilot 7c66451
Simplify API: filterNode now accepts string directly, remove createNo…
Copilot a6927f7
Add inline snapshots to tests with expected filtered output
Copilot 15d1697
Move version tags to heading line and add custom anchors in docs
Copilot 8e179ee
Update docs/api/browser/context.md
sheremet-va 4baa437
Filter out whitespace-only text nodes to prevent empty lines in output
Copilot 89b3b50
Remove trailing spaces and run linter
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /** | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import type { Config, NewPlugin, Printer, Refs } from '../types' | ||
| import { | ||
| printChildren, | ||
| printComment, | ||
| printElement, | ||
| printElementAsLeaf, | ||
| printProps, | ||
| printShadowRoot, | ||
| printText, | ||
| } from './lib/markup' | ||
|
|
||
| const ELEMENT_NODE = 1 | ||
| const TEXT_NODE = 3 | ||
| const COMMENT_NODE = 8 | ||
| const FRAGMENT_NODE = 11 | ||
|
|
||
| const ELEMENT_REGEXP = /^(?:(?:HTML|SVG)\w*)?Element$/ | ||
|
|
||
| function testHasAttribute(val: any) { | ||
| try { | ||
| return typeof val.hasAttribute === 'function' && val.hasAttribute('is') | ||
| } | ||
| catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| function testNode(val: any) { | ||
| const constructorName = val.constructor.name | ||
| const { nodeType, tagName } = val | ||
| const isCustomElement | ||
| = (typeof tagName === 'string' && tagName.includes('-')) | ||
| || testHasAttribute(val) | ||
|
|
||
| return ( | ||
| (nodeType === ELEMENT_NODE | ||
| && (ELEMENT_REGEXP.test(constructorName) || isCustomElement)) | ||
| || (nodeType === TEXT_NODE && constructorName === 'Text') | ||
| || (nodeType === COMMENT_NODE && constructorName === 'Comment') | ||
| || (nodeType === FRAGMENT_NODE && constructorName === 'DocumentFragment') | ||
| ) | ||
| } | ||
|
|
||
| export const test: NewPlugin['test'] = (val: any) => | ||
| val?.constructor?.name && testNode(val) | ||
|
|
||
| type HandledType = Element | Text | Comment | DocumentFragment | ||
|
|
||
| function nodeIsText(node: HandledType): node is Text { | ||
| return node.nodeType === TEXT_NODE | ||
| } | ||
|
|
||
| function nodeIsComment(node: HandledType): node is Comment { | ||
| return node.nodeType === COMMENT_NODE | ||
| } | ||
|
|
||
| function nodeIsFragment(node: HandledType): node is DocumentFragment { | ||
| return node.nodeType === FRAGMENT_NODE | ||
| } | ||
|
|
||
| export interface FilterConfig extends Config { | ||
| filterNode?: (node: any) => boolean | ||
| } | ||
|
|
||
| function filterChildren(children: any[], filterNode?: (node: any) => boolean): any[] { | ||
| if (!filterNode) { | ||
| return children | ||
| } | ||
| return children.filter(filterNode) | ||
| } | ||
|
|
||
| export function createDOMElementFilter(filterNode?: (node: any) => boolean): NewPlugin { | ||
| return { | ||
| test, | ||
| serialize: ( | ||
| node: HandledType, | ||
| config: Config, | ||
| indentation: string, | ||
| depth: number, | ||
| refs: Refs, | ||
| printer: Printer, | ||
| ) => { | ||
| if (nodeIsText(node)) { | ||
| return printText(node.data, config) | ||
| } | ||
|
|
||
| if (nodeIsComment(node)) { | ||
| return printComment(node.data, config) | ||
| } | ||
|
|
||
| const type = nodeIsFragment(node) | ||
| ? 'DocumentFragment' | ||
| : node.tagName.toLowerCase() | ||
|
|
||
| if (++depth > config.maxDepth) { | ||
| return printElementAsLeaf(type, config) | ||
| } | ||
|
|
||
| const children = Array.prototype.slice.call(node.childNodes || node.children) | ||
| const filteredChildren = filterChildren(children, filterNode) | ||
|
|
||
| const shadowChildren = (nodeIsFragment(node) || !node.shadowRoot) | ||
| ? [] | ||
| : Array.prototype.slice.call(node.shadowRoot.children) | ||
| const filteredShadowChildren = filterChildren(shadowChildren, filterNode) | ||
|
|
||
| return printElement( | ||
| type, | ||
| printProps( | ||
| nodeIsFragment(node) | ||
| ? [] | ||
| : Array.from(node.attributes, attr => attr.name).sort(), | ||
| nodeIsFragment(node) | ||
| ? {} | ||
| : [...node.attributes].reduce<Record<string, string>>( | ||
| (props, attribute) => { | ||
| props[attribute.name] = attribute.value | ||
| return props | ||
| }, | ||
| {}, | ||
| ), | ||
| config, | ||
| indentation + config.indent, | ||
| depth, | ||
| refs, | ||
| printer, | ||
| ), | ||
| (filteredShadowChildren.length > 0 | ||
| ? printShadowRoot(filteredShadowChildren, config, indentation + config.indent, depth, refs, printer) | ||
| : '') | ||
| + printChildren( | ||
| filteredChildren, | ||
| config, | ||
| indentation + config.indent, | ||
| depth, | ||
| refs, | ||
| printer, | ||
| ), | ||
| config, | ||
| indentation, | ||
| ) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| export default createDOMElementFilter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.