Skip to content

Commit

Permalink
HTML: make mediatype parameters case sensitive, fixes #545
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Dec 5, 2022
1 parent a52d606 commit 4eb3b3f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 29 deletions.
24 changes: 15 additions & 9 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) ||
Expand Down
25 changes: 13 additions & 12 deletions html/html_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,19 @@ func TestHTML(t *testing.T) {
{`<script><!--<`, `<script><!--<`},

// bugs
{`<svg id="1"></svg>`, `<svg id="1"></svg>`}, // #67
{`<pre> <x> a b </x> </pre>`, `<pre> <x> a b </x> </pre>`}, // #82
{`text <progress></progress> text`, `text <progress></progress> text`}, // #89
{`text <img> text`, `text <img> text`}, // #89
{`<p>text</p><br>text`, `<p>text</p><br>text`}, // #122
{`<amp-analytics type=adobeanalytics_nativeConfig>`, `<amp-analytics type=adobeanalytics_nativeConfig>`}, // #270
{`<input title=""><input lang="">`, `<input title><input lang>`}, // #331
{`<span translate="no">`, `<span translate=no>`}, // #352
{`<i class="fas"> </i> Text`, `<i class=fas></i>Text`}, // #390
{`<i class="fas"></i> Text`, `<i class=fas></i> Text`}, // #390
{`a <span></span> b`, `a <span></span> b`}, // #427
{`<canvas><p>test</p></canvas>`, `<canvas><p>test</p></canvas>`}, // #440
{`<svg id="1"></svg>`, `<svg id="1"></svg>`}, // #67
{`<pre> <x> a b </x> </pre>`, `<pre> <x> a b </x> </pre>`}, // #82
{`text <progress></progress> text`, `text <progress></progress> text`}, // #89
{`text <img> text`, `text <img> text`}, // #89
{`<p>text</p><br>text`, `<p>text</p><br>text`}, // #122
{`<amp-analytics type=adobeanalytics_nativeConfig>`, `<amp-analytics type=adobeanalytics_nativeConfig>`}, // #270
{`<input title=""><input lang="">`, `<input title><input lang>`}, // #331
{`<span translate="no">`, `<span translate=no>`}, // #352
{`<i class="fas"> </i> Text`, `<i class=fas></i>Text`}, // #390
{`<i class="fas"></i> Text`, `<i class=fas></i> Text`}, // #390
{`a <span></span> b`, `a <span></span> b`}, // #427
{`<canvas><p>test</p></canvas>`, `<canvas><p>test</p></canvas>`}, // #440
{`<source type='video/mp4; codecs="av01.0.05M.08"'>`, `<source type='video/mp4;codecs="av01.0.05M.08"'>`}, // #545
}

m := minify.New()
Expand Down
8 changes: 4 additions & 4 deletions html/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var tagMap = map[Hash]traits{
}

var attrMap = map[Hash]traits{
Accept: caselessAttr,
Accept: trimAttr,
Accept_Charset: caselessAttr,
Action: urlAttr,
Align: caselessAttr,
Expand All @@ -156,7 +156,7 @@ var attrMap = map[Hash]traits{
Classid: urlAttr,
Clear: caselessAttr,
Codebase: urlAttr,
Codetype: caselessAttr,
Codetype: trimAttr,
Color: caselessAttr,
Cols: trimAttr,
Colspan: trimAttr,
Expand All @@ -172,7 +172,7 @@ var attrMap = map[Hash]traits{
Dir: caselessAttr,
Disabled: booleanAttr,
Enabled: booleanAttr,
Enctype: caselessAttr,
Enctype: trimAttr,
Face: caselessAttr,
Formaction: urlAttr,
Formnovalidate: booleanAttr,
Expand Down Expand Up @@ -228,7 +228,7 @@ var attrMap = map[Hash]traits{
Text: caselessAttr,
Translate: caselessAttr,
Truespeed: booleanAttr,
Type: caselessAttr,
Type: trimAttr,
Typemustmatch: booleanAttr,
Undeterminate: booleanAttr,
Usemap: urlAttr,
Expand Down

0 comments on commit 4eb3b3f

Please sign in to comment.