Skip to content
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

Placing Svelte code in an HTML file breaks the document #344

Closed
taylorzane opened this issue Mar 6, 2017 · 5 comments
Closed

Placing Svelte code in an HTML file breaks the document #344

taylorzane opened this issue Mar 6, 2017 · 5 comments

Comments

@taylorzane
Copy link
Contributor

I've run into an issue when using Svelte with webpack (although this issue will more than likely come up in any build environment).

If you place the webpack output into a <script></script> block in an HTML file, the document will be broken upon loading.

The culprit is one line of code in src/parse/read/script.js line 20.

I have made a commit and I am willing to submit a PR, I just wanted to open an issue to be friendly, and create an open discussion.

Example

<html>
<body>
<script>
if ('</script>') {
  console.log('foo')
}
</script>
</body>
</html>

Output (in browser)

') { console.log('foo') }
@Rich-Harris
Copy link
Member

This isn't a Svelte thing so much as an HTML thing — any occurence of </script> will close an open script tag. Try this...

# paste this in to create test HTML file
cat <<EOS > index.html
<script>
if ('</script>') {
  console.log('foo')
}
</script>
EOS

# then open it
open index.html

...and you'll see something like this:

screen shot 2017-03-06 at 3 41 19 pm

The traditional workaround is to do something like '</scr' + 'ipt>' instead, and hope that a minifier doesn't outwit you.

@taylorzane
Copy link
Contributor Author

I agree that this is a "flaw", if you will, of HTML. But I'm not running into this issue within a Svelte component, it's happening on the line I highlighted in the Svelte library itself.

As far as your minifier comment goes, I'm getting mixed output from different minifiers. All of them concat '</scr' + 'ipt>', but only some of them change '<\/script>' to '</script'>.

If you consider this to be too rare of an issue, I can continue to manually replace the line at build.

Otherwise, I'm open to either submitting my PR, or discussing other ways to resolve this, and successfully subvert a minifier.

@Rich-Harris
Copy link
Member

Ah. I'm not entirely sure I understand, but if it solves your issue and doesn't break anything then please do submit the PR!

@taylorzane
Copy link
Contributor Author

I have to say that it's a very unique use case 😀. Everything appears to work as expected. '<\/script>' === '</script>' // evaluates true

@Rich-Harris
Copy link
Member

Released as 1.10.3. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants