-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Only trim nodes if parent can't contain text nodes #384
Comments
@remarkablemark I don't think so unfortunately. The issue is that I want to trim only certain kind of whitespaces. To do this, I need to be able to specify my own trimming logic instead of this html-react-parser/lib/dom-to-react.js Lines 55 to 60 in a41e352
replace would only help me in specifying which elements to trim, not which kind of whitespaces to trim |
Question: @vidhu in your example above, how do you differentiate if something is a normal space vs In the current code, only text nodes are trimmed so are you asking to change that logic? html-react-parser/lib/dom-to-react.js Lines 53 to 64 in a41e352
|
Hey @remarkablemark. I'm talking about the former case. I want to only trim a subset of white spaces. I would do this like so: const demoTrim = s => {
return s.replaceAll(/^[\f\t\n\v\r ]+|[\f\t\n\v\r ]+$/gm, '');
}
demoTrim(" H ello ") // " H ello "
demoTrim(" sdf. ") // "sdf" Here's a regex101 link to see what's being matched https://regex101.com/r/gJpVYH/1/ |
Gotcha @vidhu, so back to your original example where you say the whitespace before I created a Replit and found that the whitespace is maintained for nodes that are not text (e.g., |
@remarkablemark you're right. not sure how that happens. I've updated the sandbox to show this issue better. I've replaced the characters with html's const html = `
<div> </div>
`;
function App() {
return parse(html, { trim: true });
} |
Thanks for updating your example @vidhu, I think this might be coming from html-dom-parser since I believe the native DOM API parses |
@remarkablemark hmm that confuses me. In some cases, I do have an alternative approach. Could we maintain a list of tags which can't contain spaces (or more generally Looking at react's logic, it doesn't look like a huge list. The logic would look roughly like this. // A set containing elements that can't contain text nodes
// Taken from https://github.com/facebook/react/blob/cae635054e17a6f107a39d328649137b83f25972/packages/react-dom/src/client/validateDOMNesting.js#L213
const elementsWithNoTextChildren = new Set([
'tr',
'tbody',
'thead',
'tfoot',
'colgroup',
'table',
'head',
'html',
'frameset',
'#document',
]);
if (node.type === 'text') {
const { parent } = node;
if (parent && elementsWithNoTextChildren.has(parent.name)) {
// If the parent of this text node can not have text, them trim it.
data = node.data.trim();
if (data) {
result.push(node.data);
}
} else {
result.push(node.data);
}
continue;
} With this modification, react shouldn't complain about |
Hey @remarkablemark Not sure if I was clear in my earlier post but I've opened a PR #394 incase it help you in evaluating if this is a good change or not. |
Thanks, I left comments |
Released in #395 (comment) |
When parsing html, in most cases we need to enable trim to prevent the
validateDOMNesting
errorsHowever this doesn't always work. There may be cases where a div contains a
 
character. During parsing, this gets trimmed out.CodeSandBox Demo
Note, the whitespace before the
Hello
aren't spaces . They are 
.Would you be open to the idea of allowing users to provide a function for trim that would allow specifying a custom trimmer to handle these special spaces? It would look something like this
I could even see it being extended to check if the node type allows spaces or not and trimming based on that although this can be done in the replacer function also as suggested in #205
I'm happy to create a PR if you're open to this idea
The text was updated successfully, but these errors were encountered: