-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat(pretty-format): Enable filtering of children in DOMElement #11130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,9 @@ export const printProps = ( | |
.join(''); | ||
}; | ||
|
||
// https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType#node_type_constants | ||
const NodeTypeTextNode = 3; | ||
|
||
// Return empty string if children is empty. | ||
export const printChildren = ( | ||
children: Array<unknown>, | ||
|
@@ -62,14 +65,23 @@ export const printChildren = ( | |
printer: Printer, | ||
): string => | ||
children | ||
.map( | ||
child => | ||
config.spacingOuter + | ||
indentation + | ||
(typeof child === 'string' | ||
.map(child => { | ||
const printedChild = | ||
typeof child === 'string' | ||
? printText(child, config) | ||
: printer(child, config, indentation, depth, refs)), | ||
) | ||
: printer(child, config, indentation, depth, refs); | ||
|
||
if ( | ||
printedChild === '' && | ||
typeof child === 'object' && | ||
child !== null && | ||
(child as Node).nodeType !== NodeTypeTextNode | ||
) { | ||
// A plugin serialized this Node to '' meaning we should ignore it. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm not so excited about taking away the ability of plugins to output empty text for a node, esp. in a breaking way. Would it be an option to allow returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My question would be: Why is that desired? Why write a plugin that takes a Node, serializes it to an empty string but also expect that empty string to be included in the snapshot.
I've tried this route but it propagated the possibly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not saying I have a concrete use case, but I know how many usages are out there pretty-printing element trees that are abstractions over HTML (if you need a horrifying example, take a look at 'ExtReact'). If in just one of them one renders plain text not with string children, but with Regarding an explicit omission return value, we would want that limited specifically to type NullablePrinter = (...args: Parameters<Printer>) => ReturnType<Printer> | null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I've just looked at the way you implemented your plugin, and this would not really help you because you are not in control of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we have both been thinking about this the wrong way. Printing
and expecting the
and expecting the
I think what should really be done to omit children from the output is not pass them into a printer in the first place. Replace the DOMElement plugin with a plugin under your control, that clones the node, throws out any children you don't want, then pass it to the DOMElement plugin to reuse its code. Let me know if this works for you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Did you see the test added? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the sentence you quoted referred to the previous comment, not to the implementation in this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you can say the current implementation doesn't help me when the test that uses the new implementation is exactly my desired outcome. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, I am not saying what you implemented here does not work for you, I am aware it does. The second of the three comments says that what I suggested in the second half of the first comment actually would not work for you after I reconsidered it. |
||
return ''; | ||
} | ||
return config.spacingOuter + indentation + printedChild; | ||
}) | ||
.join(''); | ||
|
||
export const printText = (text: string, config: Config): string => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible I think it would be easier to compare this to the
expected
if this was being constructed via