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

html/js: Angle bracket inside script tag breaks minification #413

Closed
bep opened this issue May 11, 2021 · 1 comment
Closed

html/js: Angle bracket inside script tag breaks minification #413

bep opened this issue May 11, 2021 · 1 comment

Comments

@bep
Copy link

bep commented May 11, 2021

Reopening of #396

package main

import (
	"os"
	"regexp"
	"strings"

	"github.com/tdewolff/minify/v2"
	"github.com/tdewolff/minify/v2/html"
	"github.com/tdewolff/minify/v2/js"
)

func main() {
	input := `<script><</script><div class="foo"></div>`
	m := minify.New()
	m.AddFunc("text/html", html.Minify)
	m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)

	if err := m.Minify("text/html", os.Stdout, strings.NewReader(input)); err != nil {
		panic(err)
	}
}

Fails with:

<script>panic: unexpected < in expression on line 1 and column 9
    1: <
       ^
@tdewolff
Copy link
Owner

tdewolff commented May 11, 2021

What result were you expecting instead? You're trying to parse < as JavaScript, which clearly is invalid. See e.g. this demo in ESLint. In which case did this problem arise?

I assume this is from gohugoio/hugo#7567. However, parsing a script tag in HTML, there are only two special cases: finding </script> or <!-- inside JavaScript. The first case is actually not handled in minify yet (working on it now), the second case is. See relevant specification here: https://html.spec.whatwg.org/multipage/parsing.html#script-data-less-than-sign-state. This is not the problem of gohugoio/hugo#7567 hpwever, not sure what is the problem there...

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