question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use an actual HTML parser: unexpected behavior around nested `<style>` / `script` tags

See original GitHub issue

Obligatory 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
dummdidummcommented, Jan 6, 2021

Also causes this behavior:

Input:

{@html `<style class="block">
...
</style>`}

Output:

{@html `<style class="block ✂prettier:content✂="LmFuaW1hdGlvbk5hbWUgewogI..."></style>`}

Workaround: Hide it in a nested string.

{@html `<${'style'} class="block">
...
</style>`}

(from https://github.com/sveltejs/prettier-plugin-svelte/issues/59#issuecomment-643336817)

0reactions
tadeohepperlecommented, Aug 27, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found