Use an actual HTML parser: unexpected behavior around nested `<style>` / `script` tags
See original GitHub issueObligatory https://stackoverflow.com/a/1732454/1422124
Took me a while to track this down, because prettier was also swallowing the error message (I’ll investigate and open an issue in the appropriate repo).
Essentially the offending code I had was something like this (adapted from https://github.com/rmariuzzo/react-new-window/blob/2417b08352f34f193cc5e69524379fb70ccecf5b/src/NewWindow.js#L195 for svelte):
<script>
Array.from(document.styleSheets).forEach(styleSheet => {
// For <style> elements
let rules;
try {
rules = styleSheet.cssRules;
} catch (err) {
console.error(err);
}
if (rules) {
//...
} else if (styleSheet.href) {
// for <link> elements loading CSS from a URL
...
}
});
</script>
<style>...</style>
<div>...</div>
The comment For <style> elements
inside the JavaScript causes the regex based parser to think it’s a style tag.
Sure it’s an edge case, but it might be worth considering switching to an actual HTML parser. Maybe svelte core can be imported into this plugin? I’m sure they’re handling this already.
Thanks for the plugin by the way!
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top Results From Across the Web
In HTML5 Spec, it seems the nested script tags are supported ...
It seems the nested script tags are supported. But what is a real example that uses the nested script tags? like the below...
Read more >prettier-plugin-svelte - Bountysource
Format your svelte components using prettier. ... Use an actual HTML parser: unexpected behavior around nested `<style>` / `script` tags $ 0.
Read more >13.2 Parsing HTML documents - HTML Standard - WhatWG
This error occurs if the parser encounters an attribute in a tag that already has an attribute with the same name. The parser...
Read more >Handling common HTML and CSS problems - MDN Web Docs
For HTML, validation involves making sure all your tags are properly closed and nested, you are using a DOCTYPE, and you are using...
Read more >Chapter 3. The HTML parser
Switch to the tag open state. U+0000 NULL: This is an unexpected-null-character parse error. Emit the current input character as a character token....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Also causes this behavior:
Input:
Output:
Workaround: Hide it in a nested string.
(from https://github.com/sveltejs/prettier-plugin-svelte/issues/59#issuecomment-643336817)
I also have this issue. Are there any news on this? How can I disable this behavior? It’s potentially fucking up code pretty badly that could end up in production, just by accidentally saving with prettier.