Skip to content

Commit

Permalink
HTML: fix bug when parsing inline JavaScript, fixes #631
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 31, 2023
1 parent 808d590 commit 652758d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/fsnotify/fsnotify v1.7.0
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/tdewolff/argp v0.0.0-20231030173501-fa6c54897951
github.com/tdewolff/parse/v2 v2.7.2
github.com/tdewolff/parse/v2 v2.7.3-0.20231031130755-de6a78ee073c
github.com/tdewolff/test v1.0.10
)

Expand Down
2 changes: 1 addition & 1 deletion html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st
val = val[11:]
}
attrMinifyBuffer.Reset()
if err := m.MinifyMimetype(jsMimeBytes, attrMinifyBuffer, buffer.NewReader(val), nil); err == nil {
if err := m.MinifyMimetype(jsMimeBytes, attrMinifyBuffer, buffer.NewReader(val), inlineParams); err == nil {
val = attrMinifyBuffer.Bytes()
} else if err != minify.ErrNotExist {
return minify.UpdateErrorPosition(err, z, attr.Offset)
Expand Down
1 change: 1 addition & 0 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ func TestHTMLCSSJS(t *testing.T) {
{`<script>var a="</scr"+"ipt>"</script>`, `<script>var a="<\/script>"</script>`}, // #355
{`<div style="font-family: Arial, &#39;sans-serif&#39;; font-size: 22px;">`, `<div style=font-family:Arial,sans-serif;font-size:22px>`}, // #272
{`<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;}</style>`, `<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;}</style>`},
{`<button onclick="return false">`, `<button onclick=return!1>`}, // #631
}

m := minify.New()
Expand Down
7 changes: 5 additions & 2 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ func Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) err
}

// Minify minifies JS data, it reads from r and writes to w.
func (o *Minifier) Minify(_ *minify.M, w io.Writer, r io.Reader, _ map[string]string) error {
func (o *Minifier) Minify(_ *minify.M, w io.Writer, r io.Reader, params map[string]string) error {
z := parse.NewInput(r)
ast, err := js.Parse(z, js.Options{WhileToFor: true})
ast, err := js.Parse(z, js.Options{
WhileToFor: true,
Inline: params != nil && params["inline"] == "1",
})
if err != nil {
return err
}
Expand Down

0 comments on commit 652758d

Please sign in to comment.