-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Templates : HTML comments cause the Site Editor to crash. #47212
Comments
Here is what's happening:
A few ideas on how to solve this at different layers. Most of these suggestions are mutually compatible:
|
thanks @mcsf for jumping in and investigating!
sounds great to me! or build a small criterion for discarding them, since we could have the same kind of issues we've seen with extra newlines. /**
* Checks if given HTML is likely empty.
*
* Naive criteria for whether HTML represents meaningful content:
* - contains non-whitespace #text content, e.g. "this bare text implies HTML structure"
* - there exist _any_ tags/elements, e.g. "<div class=placeholder></div>"
*
* Specifically, these things are not considered meaningful content:
* - HTML comments
* - CDATA sections
* - "blank" #text nodes, e.g. newlines, medium mathematical space, non-breaking space
*/
const htmlLikelyEmpty = html => {
const div = document.createElement( 'div' );
div.innerHTML = html;
// If we have text content then it's probably supposed to render.
if ( div.innerText.trim() ) {
return false;
}
for ( const { nodeType } of div.childNodes ) {
// If we have any HTML elements we probably need to render
// even if the text content is empty.
if ( Node.ELEMENT_NODE === nodeType ) {
return false;
}
}
return true;
} |
This also happens if we just add an arbitrary HTML in the template file. For instance, add a
I think this might be a better option as it's less destructive, and users can still keep their content no matter if it's accidentally added or not. Comments-only freeform block can still be handled by not rendering anything in the visual editor, but available in the code editor. In the end, comments are code, not visible to the users, ...I think 😆 . |
There's a follow-up for this issue that is tracked at #48408 |
What problem does this address?
When testing #38984, the modified template causes the Site Editor to crash.
To replicate:
What is your proposed solution?
We should allow HTMLs comment in template files!
The text was updated successfully, but these errors were encountered: