From 4eb3b3f2bec9faa04da856c3d2ff6726b25b74b1 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Sun, 4 Dec 2022 21:29:16 -0300 Subject: [PATCH] HTML: make mediatype parameters case sensitive, fixes #545 --- common.go | 24 +++++++++++++++--------- html/html.go | 9 +++++---- html/html_test.go | 25 +++++++++++++------------ html/table.go | 8 ++++---- 4 files changed, 37 insertions(+), 29 deletions(-) diff --git a/common.go b/common.go index 67dc0d121b..423b67b0d8 100644 --- a/common.go +++ b/common.go @@ -21,25 +21,31 @@ var Epsilon = 0.00001 // Mediatype minifies a given mediatype by removing all whitespace. func Mediatype(b []byte) []byte { j := 0 - start := 0 inString := false + start, params := 0, len(b) for i, c := range b { - if !inString && parse.IsWhitespace(c) { - if start != 0 { - j += copy(b[j:], b[start:i]) - } else { - j += i + if !inString { + if parse.IsWhitespace(c) { + if start != 0 { + j += copy(b[j:], b[start:i]) + } else { + j += i + } + start = i + 1 + } else if c == ';' { + params = j + (i - start) } - start = i + 1 } else if c == '"' { inString = !inString } } if start != 0 { j += copy(b[j:], b[start:]) - return parse.ToLower(b[:j]) + parse.ToLower(b[:params]) + return b[:j] } - return parse.ToLower(b) + parse.ToLower(b[:params]) + return b } // DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt. diff --git a/html/html.go b/html/html.go index 3431ad3be1..db930d64fe 100644 --- a/html/html.go +++ b/html/html.go @@ -310,7 +310,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st httpEquiv.AttrVal = parse.TrimWhitespace(httpEquiv.AttrVal) if charset := attrs[2]; charset == nil && parse.EqualFold(httpEquiv.AttrVal, []byte("content-type")) { content.AttrVal = minify.Mediatype(content.AttrVal) - if bytes.Equal(content.AttrVal, []byte("text/html;charset=utf-8")) { + if parse.EqualFold(content.AttrVal, []byte("text/html;charset=utf-8")) { httpEquiv.Text = nil content.Text = []byte("charset") content.Hash = Charset @@ -392,14 +392,15 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]st } if attr.Traits&caselessAttr != 0 { val = parse.ToLower(val) - if attr.Hash == Enctype || attr.Hash == Codetype || attr.Hash == Accept || attr.Hash == Type && (t.Hash == A || t.Hash == Link || t.Hash == Embed || t.Hash == Object || t.Hash == Source || t.Hash == Script || t.Hash == Style) { - val = minify.Mediatype(val) - } } if rawTagHash != 0 && attr.Hash == Type { rawTagMediatype = parse.Copy(val) } + if attr.Hash == Enctype || attr.Hash == Codetype || attr.Hash == Accept || attr.Hash == Type && (t.Hash == A || t.Hash == Link || t.Hash == Embed || t.Hash == Object || t.Hash == Source || t.Hash == Script || t.Hash == Style) { + val = minify.Mediatype(val) + } + // default attribute values can be omitted if !o.KeepDefaultAttrVals && (attr.Hash == Type && (t.Hash == Script && jsMimetypes[string(val)] || t.Hash == Style && bytes.Equal(val, cssMimeBytes) || diff --git a/html/html_test.go b/html/html_test.go index a4911e7df2..6b793c7d94 100644 --- a/html/html_test.go +++ b/html/html_test.go @@ -151,18 +151,19 @@ func TestHTML(t *testing.T) { {`