| 1 |
// Copyright 2018 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package minifiers_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"encoding/json" |
| 19 |
"strings" |
| 20 |
"testing" |
| 21 |
|
| 22 |
qt "github.com/frankban/quicktest" |
| 23 |
"github.com/gohugoio/hugo/config" |
| 24 |
"github.com/gohugoio/hugo/config/testconfig" |
| 25 |
"github.com/gohugoio/hugo/media" |
| 26 |
"github.com/gohugoio/hugo/minifiers" |
| 27 |
"github.com/gohugoio/hugo/output" |
| 28 |
"github.com/spf13/afero" |
| 29 |
) |
| 30 |
|
| 31 |
func TestNew(t *testing.T) { |
| 32 |
c := qt.New(t) |
| 33 |
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), nil)) |
| 34 |
|
| 35 |
var rawJS string |
| 36 |
var minJS string |
| 37 |
rawJS = " var foo =1 ; foo ++ ; " |
| 38 |
minJS = "var foo=1;foo++" |
| 39 |
|
| 40 |
var rawJSON string |
| 41 |
var minJSON string |
| 42 |
rawJSON = " { \"a\" : 123 , \"b\":2, \"c\": 5 } " |
| 43 |
minJSON = "{\"a\":123,\"b\":2,\"c\":5}" |
| 44 |
|
| 45 |
for _, test := range []struct { |
| 46 |
tp media.Type |
| 47 |
rawString string |
| 48 |
expectedMinString string |
| 49 |
}{ |
| 50 |
{media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}"}, |
| 51 |
{media.Builtin.RSSType, " <hello> Hugo! </hello> ", "<hello>Hugo!</hello>"}, // RSS should be handled as XML |
| 52 |
{media.Builtin.JSONType, rawJSON, minJSON}, |
| 53 |
{media.Builtin.JavascriptType, rawJS, minJS}, |
| 54 |
// JS Regex minifiers |
| 55 |
{media.Type{Type: "application/ecmascript", MainType: "application", SubType: "ecmascript"}, rawJS, minJS}, |
| 56 |
{media.Type{Type: "application/javascript", MainType: "application", SubType: "javascript"}, rawJS, minJS}, |
| 57 |
{media.Type{Type: "application/x-javascript", MainType: "application", SubType: "x-javascript"}, rawJS, minJS}, |
| 58 |
{media.Type{Type: "application/x-ecmascript", MainType: "application", SubType: "x-ecmascript"}, rawJS, minJS}, |
| 59 |
{media.Type{Type: "text/ecmascript", MainType: "text", SubType: "ecmascript"}, rawJS, minJS}, |
| 60 |
{media.Type{Type: "application/javascript", MainType: "text", SubType: "javascript"}, rawJS, minJS}, |
| 61 |
// JSON Regex minifiers |
| 62 |
{media.Type{Type: "application/json", MainType: "application", SubType: "json"}, rawJSON, minJSON}, |
| 63 |
{media.Type{Type: "application/x-json", MainType: "application", SubType: "x-json"}, rawJSON, minJSON}, |
| 64 |
{media.Type{Type: "application/ld+json", MainType: "application", SubType: "ld+json"}, rawJSON, minJSON}, |
| 65 |
{media.Type{Type: "application/json", MainType: "text", SubType: "json"}, rawJSON, minJSON}, |
| 66 |
} { |
| 67 |
var b bytes.Buffer |
| 68 |
|
| 69 |
c.Assert(m.Minify(test.tp, &b, strings.NewReader(test.rawString)), qt.IsNil) |
| 70 |
c.Assert(b.String(), qt.Equals, test.expectedMinString) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func TestConfigureMinify(t *testing.T) { |
| 75 |
c := qt.New(t) |
| 76 |
v := config.New() |
| 77 |
v.Set("minify", map[string]any{ |
| 78 |
"disablexml": true, |
| 79 |
"tdewolff": map[string]any{ |
| 80 |
"html": map[string]any{ |
| 81 |
"keepwhitespace": true, |
| 82 |
}, |
| 83 |
}, |
| 84 |
}) |
| 85 |
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(afero.NewMemMapFs(), v)) |
| 86 |
|
| 87 |
for _, test := range []struct { |
| 88 |
tp media.Type |
| 89 |
rawString string |
| 90 |
expectedMinString string |
| 91 |
errorExpected bool |
| 92 |
}{ |
| 93 |
{media.Builtin.HTMLType, "<hello> Hugo! </hello>", "<hello> Hugo! </hello>", false}, // configured minifier |
| 94 |
{media.Builtin.CSSType, " body { color: blue; } ", "body{color:blue}", false}, // default minifier |
| 95 |
{media.Builtin.XMLType, " <hello> Hugo! </hello> ", " <hello> Hugo! </hello> ", false}, // disable Xml minification |
| 96 |
} { |
| 97 |
var b bytes.Buffer |
| 98 |
if !test.errorExpected { |
| 99 |
c.Assert(m.Minify(test.tp, &b, strings.NewReader(test.rawString)), qt.IsNil) |
| 100 |
c.Assert(b.String(), qt.Equals, test.expectedMinString) |
| 101 |
} else { |
| 102 |
err := m.Minify(test.tp, &b, strings.NewReader(test.rawString)) |
| 103 |
c.Assert(err, qt.ErrorMatches, "minifier does not exist for mimetype") |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func TestJSONRoundTrip(t *testing.T) { |
| 109 |
c := qt.New(t) |
| 110 |
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, nil)) |
| 111 |
|
| 112 |
for _, test := range []string{`{ |
| 113 |
"glossary": { |
| 114 |
"title": "example glossary", |
| 115 |
"GlossDiv": { |
| 116 |
"title": "S", |
| 117 |
"GlossList": { |
| 118 |
"GlossEntry": { |
| 119 |
"ID": "SGML", |
| 120 |
"SortAs": "SGML", |
| 121 |
"GlossTerm": "Standard Generalized Markup Language", |
| 122 |
"Acronym": "SGML", |
| 123 |
"Abbrev": "ISO 8879:1986", |
| 124 |
"GlossDef": { |
| 125 |
"para": "A meta-markup language, used to create markup languages such as DocBook.", |
| 126 |
"GlossSeeAlso": ["GML", "XML"] |
| 127 |
}, |
| 128 |
"GlossSee": "markup" |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
}`} { |
| 134 |
|
| 135 |
var b bytes.Buffer |
| 136 |
m1 := make(map[string]any) |
| 137 |
m2 := make(map[string]any) |
| 138 |
c.Assert(json.Unmarshal([]byte(test), &m1), qt.IsNil) |
| 139 |
c.Assert(m.Minify(media.Builtin.JSONType, &b, strings.NewReader(test)), qt.IsNil) |
| 140 |
c.Assert(json.Unmarshal(b.Bytes(), &m2), qt.IsNil) |
| 141 |
c.Assert(m1, qt.DeepEquals, m2) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
func TestBugs(t *testing.T) { |
| 146 |
c := qt.New(t) |
| 147 |
v := config.New() |
| 148 |
m, _ := minifiers.New(media.DefaultTypes, output.DefaultFormats, testconfig.GetTestConfig(nil, v)) |
| 149 |
|
| 150 |
for _, test := range []struct { |
| 151 |
tp media.Type |
| 152 |
rawString string |
| 153 |
expectedMinString string |
| 154 |
}{ |
| 155 |
// Issue 5506 |
| 156 |
{media.Builtin.CSSType, " body { color: rgba(000, 000, 000, 0.7); }", "body{color:rgba(0,0,0,.7)}"}, |
| 157 |
// Issue 8332 |
| 158 |
{media.Builtin.HTMLType, "<i class='fas fa-tags fa-fw'></i> Tags", `<i class='fas fa-tags fa-fw'></i> Tags`}, |
| 159 |
// Issue #13082 |
| 160 |
{media.Builtin.HTMLType, "<gcse:searchresults-only></gcse:searchresults-only>", `<gcse:searchresults-only></gcse:searchresults-only>`}, |
| 161 |
// Issue #14669. |
| 162 |
{media.Builtin.SVGType, `<use x-bind:href="myicon">`, `<use x-bind:href="myicon">`}, |
| 163 |
{media.Builtin.SVGType, `<use :href="myicon">`, `<use :href="myicon">`}, |
| 164 |
} { |
| 165 |
var b bytes.Buffer |
| 166 |
|
| 167 |
c.Assert(m.Minify(test.tp, &b, strings.NewReader(test.rawString)), qt.IsNil) |
| 168 |
c.Assert(b.String(), qt.Equals, test.expectedMinString) |
| 169 |
} |
| 170 |
} |