From 879b4abde722cb66463ca81a4cf6ac5465ef276d Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Sun, 7 Aug 2022 18:19:16 +0200 Subject: [PATCH] [bugfix] Markdown formatting updates (#743) * add minify dependency specifically for markdown * rearrange markdown formatting * update markdown tests --- README.md | 1 + go.mod | 2 + go.sum | 10 + .../api/client/status/statuscreate_test.go | 2 +- internal/text/markdown.go | 32 +- internal/text/markdown_test.go | 43 +- .../tdewolff/minify/v2/.gitattributes | 2 + .../github.com/tdewolff/minify/v2/.gitignore | 27 + .../tdewolff/minify/v2/.golangci.yml | 16 + .../github.com/tdewolff/minify/v2/Dockerfile | 17 + vendor/github.com/tdewolff/minify/v2/LICENSE | 22 + vendor/github.com/tdewolff/minify/v2/Makefile | 58 + .../github.com/tdewolff/minify/v2/README.md | 723 +++++++++ .../github.com/tdewolff/minify/v2/common.go | 515 +++++++ .../tdewolff/minify/v2/html/buffer.go | 137 ++ .../tdewolff/minify/v2/html/hash.go | 543 +++++++ .../tdewolff/minify/v2/html/html.go | 513 +++++++ .../tdewolff/minify/v2/html/table.go | 1346 +++++++++++++++++ .../github.com/tdewolff/minify/v2/minify.go | 371 +++++ .../tdewolff/parse/v2/.gitattributes | 1 + .../github.com/tdewolff/parse/v2/.gitignore | 5 + .../tdewolff/parse/v2/.golangci.yml | 16 + .../github.com/tdewolff/parse/v2/LICENSE.md | 22 + vendor/github.com/tdewolff/parse/v2/README.md | 64 + .../tdewolff/parse/v2/buffer/buffer.go | 12 + .../tdewolff/parse/v2/buffer/lexer.go | 164 ++ .../tdewolff/parse/v2/buffer/reader.go | 44 + .../tdewolff/parse/v2/buffer/streamlexer.go | 223 +++ .../tdewolff/parse/v2/buffer/writer.go | 65 + vendor/github.com/tdewolff/parse/v2/common.go | 237 +++ vendor/github.com/tdewolff/parse/v2/error.go | 47 + .../tdewolff/parse/v2/html/README.md | 98 ++ .../github.com/tdewolff/parse/v2/html/hash.go | 81 + .../github.com/tdewolff/parse/v2/html/lex.go | 494 ++++++ .../github.com/tdewolff/parse/v2/html/util.go | 113 ++ vendor/github.com/tdewolff/parse/v2/input.go | 173 +++ .../github.com/tdewolff/parse/v2/position.go | 95 ++ .../tdewolff/parse/v2/strconv/float.go | 257 ++++ .../tdewolff/parse/v2/strconv/int.go | 108 ++ .../tdewolff/parse/v2/strconv/price.go | 83 + vendor/github.com/tdewolff/parse/v2/util.go | 486 ++++++ vendor/modules.txt | 10 + 42 files changed, 7259 insertions(+), 19 deletions(-) create mode 100644 vendor/github.com/tdewolff/minify/v2/.gitattributes create mode 100644 vendor/github.com/tdewolff/minify/v2/.gitignore create mode 100644 vendor/github.com/tdewolff/minify/v2/.golangci.yml create mode 100644 vendor/github.com/tdewolff/minify/v2/Dockerfile create mode 100644 vendor/github.com/tdewolff/minify/v2/LICENSE create mode 100644 vendor/github.com/tdewolff/minify/v2/Makefile create mode 100644 vendor/github.com/tdewolff/minify/v2/README.md create mode 100644 vendor/github.com/tdewolff/minify/v2/common.go create mode 100644 vendor/github.com/tdewolff/minify/v2/html/buffer.go create mode 100644 vendor/github.com/tdewolff/minify/v2/html/hash.go create mode 100644 vendor/github.com/tdewolff/minify/v2/html/html.go create mode 100644 vendor/github.com/tdewolff/minify/v2/html/table.go create mode 100644 vendor/github.com/tdewolff/minify/v2/minify.go create mode 100644 vendor/github.com/tdewolff/parse/v2/.gitattributes create mode 100644 vendor/github.com/tdewolff/parse/v2/.gitignore create mode 100644 vendor/github.com/tdewolff/parse/v2/.golangci.yml create mode 100644 vendor/github.com/tdewolff/parse/v2/LICENSE.md create mode 100644 vendor/github.com/tdewolff/parse/v2/README.md create mode 100644 vendor/github.com/tdewolff/parse/v2/buffer/buffer.go create mode 100644 vendor/github.com/tdewolff/parse/v2/buffer/lexer.go create mode 100644 vendor/github.com/tdewolff/parse/v2/buffer/reader.go create mode 100644 vendor/github.com/tdewolff/parse/v2/buffer/streamlexer.go create mode 100644 vendor/github.com/tdewolff/parse/v2/buffer/writer.go create mode 100644 vendor/github.com/tdewolff/parse/v2/common.go create mode 100644 vendor/github.com/tdewolff/parse/v2/error.go create mode 100644 vendor/github.com/tdewolff/parse/v2/html/README.md create mode 100644 vendor/github.com/tdewolff/parse/v2/html/hash.go create mode 100644 vendor/github.com/tdewolff/parse/v2/html/lex.go create mode 100644 vendor/github.com/tdewolff/parse/v2/html/util.go create mode 100644 vendor/github.com/tdewolff/parse/v2/input.go create mode 100644 vendor/github.com/tdewolff/parse/v2/position.go create mode 100644 vendor/github.com/tdewolff/parse/v2/strconv/float.go create mode 100644 vendor/github.com/tdewolff/parse/v2/strconv/int.go create mode 100644 vendor/github.com/tdewolff/parse/v2/strconv/price.go create mode 100644 vendor/github.com/tdewolff/parse/v2/util.go diff --git a/README.md b/README.md index 15342bab3..ba99069ad 100644 --- a/README.md +++ b/README.md @@ -245,6 +245,7 @@ The following libraries and frameworks are used by GoToSocial, with gratitude - [superseriousbusiness/activity](https://github.com/superseriousbusiness/activity) forked from [go-fed/activity](https://github.com/go-fed/activity); Golang ActivityPub/ActivityStreams library. [BSD-3-Clause License](https://spdx.org/licenses/BSD-3-Clause.html). - [superseriousbusiness/oauth2](https://github.com/superseriousbusiness/oauth2) forked from [go-oauth2/oauth2](https://github.com/go-oauth2/oauth2); oauth server framework and token handling. [MIT License](https://spdx.org/licenses/MIT.html). - [go-swagger/go-swagger](https://github.com/go-swagger/go-swagger); Swagger OpenAPI spec generation. [Apache-2.0 License](https://spdx.org/licenses/Apache-2.0.html). +- [tdewolff/minify](https://github.com/tdewolff/minify); HTML minification for Markdown-submitted posts. [MIT License](https://spdx.org/licenses/MIT.html). - [uptrace/bun](https://github.com/uptrace/bun); database ORM. [BSD-2-Clause License](https://spdx.org/licenses/BSD-2-Clause.html). - [wagslane/go-password-validator](https://github.com/wagslane/go-password-validator); password strength validation. [MIT License](https://spdx.org/licenses/MIT.html). diff --git a/go.mod b/go.mod index f6a3789ab..80def8a23 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/superseriousbusiness/activity v1.1.0-gts github.com/superseriousbusiness/exif-terminator v0.3.0 github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB + github.com/tdewolff/minify/v2 v2.12.0 github.com/uptrace/bun v1.1.3 github.com/uptrace/bun/dialect/pgdialect v1.1.3 github.com/uptrace/bun/dialect/sqlitedialect v1.1.3 @@ -120,6 +121,7 @@ require ( github.com/spf13/pflag v1.0.5 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/superseriousbusiness/go-jpeg-image-structure/v2 v2.0.0-20220321154430-d89a106fdabe // indirect + github.com/tdewolff/parse/v2 v2.6.1 // indirect github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/ugorji/go/codec v1.2.7 // indirect github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect diff --git a/go.sum b/go.sum index 2d59d6dc5..b8ff5295b 100644 --- a/go.sum +++ b/go.sum @@ -102,6 +102,7 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3 github.com/buckket/go-blurhash v1.1.0 h1:X5M6r0LIvwdvKiUtiNcRL2YlmOfMzYobI3VCKCZc9Do= github.com/buckket/go-blurhash v1.1.0/go.mod h1:aT2iqo5W9vu9GpyoLErKfTHwgODsZp3bQfXjXJUxNb8= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/cheekybits/is v0.0.0-20150225183255-68e9c0620927/go.mod h1:h/aW8ynjgkuj+NQRlZcDbAbM1ORAbXjXX77sX7T289U= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -121,6 +122,7 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/djherbis/atime v1.1.0/go.mod h1:28OF6Y8s3NQWwacXc5eZTsEsiMzp7LF8MbXE+XJPdBE= github.com/dsoprea/go-exif/v2 v2.0.0-20200321225314-640175a69fe4/go.mod h1:Lm2lMM2zx8p4a34ZemkaUV95AnMl4ZvLbCUbwOvLC2E= github.com/dsoprea/go-exif/v3 v3.0.0-20200717053412-08f1b6708903/go.mod h1:0nsO1ce0mh5czxGeLo4+OCZ/C6Eo6ZlMWsz7rH/Gxv8= github.com/dsoprea/go-exif/v3 v3.0.0-20210428042052-dca55bf8ca15/go.mod h1:cg5SNYKHMmzxsr9X6ZeLh/nfBRHHp5PngtEPcujONtk= @@ -396,6 +398,7 @@ github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= @@ -515,6 +518,13 @@ github.com/superseriousbusiness/go-jpeg-image-structure/v2 v2.0.0-20220321154430 github.com/superseriousbusiness/go-jpeg-image-structure/v2 v2.0.0-20220321154430-d89a106fdabe/go.mod h1:gH4P6gN1V+wmIw5o97KGaa1RgXB/tVpC2UNzijhg3E4= github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB h1:PtW2w6budTvRV2J5QAoSvThTHBuvh8t/+BXIZFAaBSc= github.com/superseriousbusiness/oauth2/v4 v4.3.2-SSB/go.mod h1:uYC/W92oVRJ49Vh1GcvTqpeFqHi+Ovrl2sMllQWRAEo= +github.com/tdewolff/minify/v2 v2.12.0 h1:ZyvMKeciyR3vzJrK/oHyBcSmpttQ/V+ah7qOqTZclaU= +github.com/tdewolff/minify/v2 v2.12.0/go.mod h1:8mvf+KglD7XurfvvFZDUYvVURy6bA/r0oTvmakXMnyg= +github.com/tdewolff/parse/v2 v2.6.1 h1:RIfy1erADkO90ynJWvty8VIkqqKYRzf2iLp8ObG174I= +github.com/tdewolff/parse/v2 v2.6.1/go.mod h1:WzaJpRSbwq++EIQHYIRTpbYKNA3gn9it1Ik++q4zyho= +github.com/tdewolff/test v1.0.6/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= +github.com/tdewolff/test v1.0.7 h1:8Vs0142DmPFW/bQeHRP3MV19m1gvndjUb1sn8yy74LM= +github.com/tdewolff/test v1.0.7/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= github.com/tidwall/btree v0.0.0-20191029221954-400434d76274 h1:G6Z6HvJuPjG6XfNGi/feOATzeJrfgTNJY+rGrHbA04E= github.com/tidwall/btree v0.0.0-20191029221954-400434d76274/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8= github.com/tidwall/buntdb v1.1.2 h1:noCrqQXL9EKMtcdwJcmuVKSEjqu1ua99RHHgbLTEHRo= diff --git a/internal/api/client/status/statuscreate_test.go b/internal/api/client/status/statuscreate_test.go index a42654a42..2a82ff8a5 100644 --- a/internal/api/client/status/statuscreate_test.go +++ b/internal/api/client/status/statuscreate_test.go @@ -44,7 +44,7 @@ type StatusCreateTestSuite struct { const ( statusWithLinksAndTags = "#test alright, should be able to post #links with fragments in them now, let's see........\n\nhttps://docs.gotosocial.org/en/latest/user_guide/posts/#links\n\n#gotosocial\n\n(tobi remember to pull the docker image challenge)" statusMarkdown = "# Title\n\n## Smaller title\n\nThis is a post written in [markdown](https://www.markdownguide.org/)\n\n" - statusMarkdownExpected = "

Title

\n\n

Smaller title

\n\n

This is a post written in markdown

\n\n

\n" + statusMarkdownExpected = "

Title

Smaller title

This is a post written in markdown

" ) // Post a new status with some custom visibility settings diff --git a/internal/text/markdown.go b/internal/text/markdown.go index a5c62f23f..8952b99d6 100644 --- a/internal/text/markdown.go +++ b/internal/text/markdown.go @@ -23,17 +23,39 @@ import ( "github.com/russross/blackfriday/v2" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" + "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/html" ) -func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { - // do the markdown parsing *first* - contentBytes := blackfriday.Run([]byte(md)) +var m *minify.M +func (f *formatter) FromMarkdown(ctx context.Context, md string, mentions []*gtsmodel.Mention, tags []*gtsmodel.Tag) string { // format tags nicely - content := f.ReplaceTags(ctx, string(contentBytes), tags) + content := f.ReplaceTags(ctx, md, tags) // format mentions nicely content = f.ReplaceMentions(ctx, content, mentions) - return SanitizeHTML(content) + // parse markdown + contentBytes := blackfriday.Run([]byte(content)) + + // clean anything dangerous out of it + content = SanitizeHTML(string(contentBytes)) + + if m == nil { + m = minify.New() + m.Add("text/html", &html.Minifier{ + KeepEndTags: true, + KeepQuotes: true, + KeepWhitespace: true, + }) + } + + minified, err := m.String("text/html", content) + if err != nil { + log.Errorf("error minifying markdown text: %s", err) + } + + return minified } diff --git a/internal/text/markdown_test.go b/internal/text/markdown_test.go index 74a18a685..af4a4fef6 100644 --- a/internal/text/markdown_test.go +++ b/internal/text/markdown_test.go @@ -44,19 +44,23 @@ that was some JSON :) ` const ( - simpleMarkdown = "# Title\n\nHere's a simple text in markdown.\n\nHere's a [link](https://example.org)." - simpleMarkdownExpected = "

Title

\n\n

Here’s a simple text in markdown.

\n\n

Here’s a link.

\n" - withCodeBlockExpected = "

Title

\n\n

Below is some JSON.

\n\n
{\n  "key": "value",\n  "another_key": [\n    "value1",\n    "value2"\n  ]\n}\n
\n\n

that was some JSON :)

\n" - withInlineCode = "`Nobody tells you about the SECRET CODE, do they?`" - withInlineCodeExpected = "

Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?

\n" - withInlineCode2 = "`Nobody tells you about the SECRET CODE, do they?`" - withInlineCode2Expected = "

Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?

\n" - withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!" - withHashtagExpected = "

Title

\n\n

here’s a simple status that uses hashtag #Hashtag!

\n" - mdWithHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a link.\n\nHere's an image: \"The" - mdWithHTMLExpected = "

Title

\n\n

Here’s a simple text in markdown.

\n\n

Here’s a link.

\n\n

Here’s an image: \"The

\n" - mdWithCheekyHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a cheeky little script: " - mdWithCheekyHTMLExpected = "

Title

\n\n

Here’s a simple text in markdown.

\n\n

Here’s a cheeky little script:

\n" + simpleMarkdown = "# Title\n\nHere's a simple text in markdown.\n\nHere's a [link](https://example.org)." + simpleMarkdownExpected = "

Title

Here’s a simple text in markdown.

Here’s a link.

" + withCodeBlockExpected = "

Title

Below is some JSON.

{\n  "key": "value",\n  "another_key": [\n    "value1",\n    "value2"\n  ]\n}\n

that was some JSON :)

" + withInlineCode = "`Nobody tells you about the SECRET CODE, do they?`" + withInlineCodeExpected = "

Nobody tells you about the <code><del>SECRET CODE</del></code>, do they?

" + withInlineCode2 = "`Nobody tells you about the
SECRET CODE, do they?`" + withInlineCode2Expected = "

Nobody tells you about the </code><del>SECRET CODE</del><code>, do they?

" + withHashtag = "# Title\n\nhere's a simple status that uses hashtag #Hashtag!" + withHashtagExpected = "

Title

here’s a simple status that uses hashtag #Hashtag!

" + mdWithHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a link.\n\nHere's an image: \"The" + mdWithHTMLExpected = "

Title

Here’s a simple text in markdown.

Here’s a link.

Here’s an image: \"The

" + mdWithCheekyHTML = "# Title\n\nHere's a simple text in markdown.\n\nHere's a cheeky little script: " + mdWithCheekyHTMLExpected = "

Title

Here’s a simple text in markdown.

Here’s a cheeky little script:

" + mdWithHashtagInitial = "#welcome #Hashtag" + mdWithHashtagInitialExpected = "

#welcome #Hashtag

" + mdCodeBlockWithNewlines = "some code coming up\n\n```\n\n\n\n```\nthat was some code" + mdCodeBlockWithNewlinesExpected = "

some code coming up

\n\n\n

that was some code

" ) type MarkdownTestSuite struct { @@ -102,6 +106,19 @@ func (suite *MarkdownTestSuite) TestParseWithCheekyHTML() { suite.Equal(mdWithCheekyHTMLExpected, s) } +func (suite *MarkdownTestSuite) TestParseWithHashtagInitial() { + s := suite.formatter.FromMarkdown(context.Background(), mdWithHashtagInitial, nil, []*gtsmodel.Tag{ + suite.testTags["Hashtag"], + suite.testTags["welcome"], + }) + suite.Equal(mdWithHashtagInitialExpected, s) +} + +func (suite *MarkdownTestSuite) TestParseCodeBlockWithNewlines() { + s := suite.formatter.FromMarkdown(context.Background(), mdCodeBlockWithNewlines, nil, nil) + suite.Equal(mdCodeBlockWithNewlinesExpected, s) +} + func TestMarkdownTestSuite(t *testing.T) { suite.Run(t, new(MarkdownTestSuite)) } diff --git a/vendor/github.com/tdewolff/minify/v2/.gitattributes b/vendor/github.com/tdewolff/minify/v2/.gitattributes new file mode 100644 index 000000000..16a3a8b06 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/.gitattributes @@ -0,0 +1,2 @@ +benchmarks/sample_* linguist-generated +tests/*/corpus/* linguist-generated diff --git a/vendor/github.com/tdewolff/minify/v2/.gitignore b/vendor/github.com/tdewolff/minify/v2/.gitignore new file mode 100644 index 000000000..341023d5e --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/.gitignore @@ -0,0 +1,27 @@ +release.sh +dist/ +benchmarks/* +!benchmarks/*.go +!benchmarks/sample_* +tests/*/fuzz-fuzz.zip +tests/*/crashers +tests/*/suppressions +tests/*/corpus/* +!tests/*/corpus/*.* +parse/tests/*/fuzz-fuzz.zip +parse/tests/*/crashers +parse/tests/*/suppressions +parse/tests/*/corpus/* +!parse/tests/*/corpus/*.* +bindings/js/build +bindings/js/prebuilds +bindings/js/minify.h +bindings/js/minify.a +bindings/js/node_modules +bindings/js/example/package-lock.json +bindings/js/example/node_modules +bindings/js/example/test.min.html +bindings/py/minify.h +bindings/py/minify.so +bindings/py/tdewolff_minify.egg-info +bindings/py/example/example.min.html diff --git a/vendor/github.com/tdewolff/minify/v2/.golangci.yml b/vendor/github.com/tdewolff/minify/v2/.golangci.yml new file mode 100644 index 000000000..7009f9201 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/.golangci.yml @@ -0,0 +1,16 @@ +linters: + enable: + - depguard + - dogsled + - gofmt + - goimports + - golint + - gosec + - govet + - megacheck + - misspell + - nakedret + - prealloc + - unconvert + - unparam + - wastedassign diff --git a/vendor/github.com/tdewolff/minify/v2/Dockerfile b/vendor/github.com/tdewolff/minify/v2/Dockerfile new file mode 100644 index 000000000..0f7fde443 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/Dockerfile @@ -0,0 +1,17 @@ +# Use this image to build the executable +FROM golang:1.18-alpine AS build + +WORKDIR /go/src/github.com/tdewolff/minify +COPY . /go/src/github.com/tdewolff/minify/ + +RUN apk add --no-cache git ca-certificates make bash +RUN /usr/bin/env bash -c make install + + +# Final image containing the executable from the previous step +FROM alpine:3 + +COPY --from=build /go/bin/minify /usr/bin/minify +COPY "containerfiles/container-entrypoint.sh" "/init.sh" + +ENTRYPOINT ["/init.sh"] diff --git a/vendor/github.com/tdewolff/minify/v2/LICENSE b/vendor/github.com/tdewolff/minify/v2/LICENSE new file mode 100644 index 000000000..41677de41 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2015 Taco de Wolff + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/github.com/tdewolff/minify/v2/Makefile b/vendor/github.com/tdewolff/minify/v2/Makefile new file mode 100644 index 000000000..9eede2839 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/Makefile @@ -0,0 +1,58 @@ +SHELL=/usr/bin/env bash +NAME=minify +CMD=./cmd/minify +TARGETS=linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 freebsd_amd64 netbsd_amd64 openbsd_amd64 windows_amd64 +VERSION=`git describe --tags` +FLAGS=-ldflags "-s -w -X 'main.Version=${VERSION}'" -trimpath +ENVS=GO111MODULES=on CGO_ENABLED=0 + +all: install + +install: + echo "Installing ${VERSION}" + ${ENVS} go install ${FLAGS} ./cmd/minify + . cmd/minify/bash_completion + +release: + TAG=$(shell git describe --tags --exact-match 2> /dev/null); + if [ "${.SHELLSTATUS}" -eq 0 ]; then \ + echo "Releasing ${VERSION}"; \ + else \ + echo "ERROR: commit is not tagged with a version"; \ + echo ""; \ + exit 1; \ + fi + rm -rf dist + mkdir -p dist + for t in ${TARGETS}; do \ + echo Building $$t...; \ + mkdir dist/$$t; \ + os=$$(echo $$t | cut -f1 -d_); \ + arch=$$(echo $$t | cut -f2 -d_); \ + ${ENVS} GOOS=$$os GOARCH=$$arch go build ${FLAGS} -o dist/$$t/${NAME} ${CMD}; \ + \ + cp LICENSE dist/$$t/.; \ + cp cmd/minify/README.md dist/$$t/.; \ + if [ "$$os" == "windows" ]; then \ + mv dist/$$t/${NAME} dist/$$t/${NAME}.exe; \ + zip -jq dist/${NAME}_$$t.zip dist/$$t/*; \ + cd dist; \ + sha256sum ${NAME}_$$t.zip >> checksums.txt; \ + cd ..; \ + else \ + cp cmd/minify/bash_completion dist/$$t/.; \ + cd dist/$$t; \ + tar -cf - * | gzip -9 > ../${NAME}_$$t.tar.gz; \ + cd ..; \ + sha256sum ${NAME}_$$t.tar.gz >> checksums.txt; \ + cd ..; \ + fi; \ + rm -rf dist/$$t; \ + done + +clean: + echo "Cleaning dist/" + rm -rf dist + +.PHONY: install release clean +.SILENT: install release clean diff --git a/vendor/github.com/tdewolff/minify/v2/README.md b/vendor/github.com/tdewolff/minify/v2/README.md new file mode 100644 index 000000000..51917f776 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/README.md @@ -0,0 +1,723 @@ +# Minify [![API reference](https://img.shields.io/badge/godoc-reference-5272B4)](https://pkg.go.dev/github.com/tdewolff/minify/v2?tab=doc) [![Go Report Card](https://goreportcard.com/badge/github.com/tdewolff/minify)](https://goreportcard.com/report/github.com/tdewolff/minify) [![codecov](https://codecov.io/gh/tdewolff/minify/branch/master/graph/badge.svg?token=Cr7r2EKPj2)](https://codecov.io/gh/tdewolff/minify) [![Donate](https://img.shields.io/badge/patreon-donate-DFB317)](https://www.patreon.com/tdewolff) + +**[Online demo](https://go.tacodewolff.nl/minify)** if you need to minify files *now*. + +**[Binaries](https://github.com/tdewolff/minify/releases) of CLI for various platforms.** See [CLI](https://github.com/tdewolff/minify/tree/master/cmd/minify) for more installation instructions. + +**[Python bindings](https://pypi.org/project/tdewolff-minify/)** install with `pip install tdewolff-minify` + +**[JavaScript bindings](https://www.npmjs.com/package/@tdewolff/minify)** install with `npm i @tdewolff/minify` + +--- + +*Did you know that the shortest valid piece of HTML5 is `x`? See for yourself at the [W3C Validator](http://validator.w3.org/)!* + +Minify is a minifier package written in [Go][1]. It provides HTML5, CSS3, JS, JSON, SVG and XML minifiers and an interface to implement any other minifier. Minification is the process of removing bytes from a file (such as whitespace) without changing its output and therefore shrinking its size and speeding up transmission over the internet and possibly parsing. The implemented minifiers are designed for high performance. + +The core functionality associates mimetypes with minification functions, allowing embedded resources (like CSS or JS within HTML files) to be minified as well. Users can add new implementations that are triggered based on a mimetype (or pattern), or redirect to an external command (like ClosureCompiler, UglifyCSS, ...). + +### Sponsors + +[![SiteGround](https://www.siteground.com/img/downloads/siteground-logo-black-transparent-vector.svg)](https://www.siteground.com/) + +Please see https://www.patreon.com/tdewolff for ways to contribute, otherwise please contact me directly! + +#### Table of Contents + +- [Minify](#minify) + - [Prologue](#prologue) + - [Installation](#installation) + - [API stability](#api-stability) + - [Testing](#testing) + - [Performance](#performance) + - [HTML](#html) + - [Whitespace removal](#whitespace-removal) + - [CSS](#css) + - [JS](#js) + - [Comparison with other tools](#comparison-with-other-tools) + - [Compression ratio (lower is better)](#compression-ratio-lower-is-better) + - [Time (lower is better)](#time-lower-is-better) + - [JSON](#json) + - [SVG](#svg) + - [XML](#xml) + - [Usage](#usage) + - [New](#new) + - [From reader](#from-reader) + - [From bytes](#from-bytes) + - [From string](#from-string) + - [To reader](#to-reader) + - [To writer](#to-writer) + - [Middleware](#middleware) + - [Custom minifier](#custom-minifier) + - [Mediatypes](#mediatypes) + - [Examples](#examples) + - [Common minifiers](#common-minifiers) + - [External minifiers](#external-minifiers) + - [Closure Compiler](#closure-compiler) + - [UglifyJS](#uglifyjs) + - [esbuild](#esbuild) + - [Custom minifier](#custom-minifier-example) + - [ResponseWriter](#responsewriter) + - [Templates](#templates) + - [License](#license) + +### Roadmap + +- [ ] Use ASM/SSE to further speed-up core parts of the parsers/minifiers +- [x] Improve JS minifiers by shortening variables and proper semicolon omission +- [ ] Speed-up SVG minifier, it is very slow +- [x] Proper parser error reporting and line number + column information +- [ ] Generation of source maps (uncertain, might slow down parsers too much if it cannot run separately nicely) +- [ ] Create a cmd to pack webfiles (much like webpack), ie. merging CSS and JS files, inlining small external files, minification and gzipping. This would work on HTML files. + +## Prologue +Minifiers or bindings to minifiers exist in almost all programming languages. Some implementations are merely using several regular expressions to trim whitespace and comments (even though regex for parsing HTML/XML is ill-advised, for a good read see [Regular Expressions: Now You Have Two Problems](http://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)). Some implementations are much more profound, such as the [YUI Compressor](http://yui.github.io/yuicompressor/) and [Google Closure Compiler](https://github.com/google/closure-compiler) for JS. As most existing implementations either use JavaScript, use regexes, and don't focus on performance, they are pretty slow. + +This minifier proves to be that fast and extensive minifier that can handle HTML and any other filetype it may contain (CSS, JS, ...). It is usually orders of magnitude faster than existing minifiers. + +## Installation +Make sure you have [Git](https://git-scm.com/) and [Go](https://golang.org/dl/) (1.13 or higher) installed, run +``` +mkdir Project +cd Project +go mod init +go get -u github.com/tdewolff/minify/v2 +``` + +Then add the following imports to be able to use the various minifiers +``` go +import ( + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/css" + "github.com/tdewolff/minify/v2/html" + "github.com/tdewolff/minify/v2/js" + "github.com/tdewolff/minify/v2/json" + "github.com/tdewolff/minify/v2/svg" + "github.com/tdewolff/minify/v2/xml" +) +``` + +You can optionally run `go mod tidy` to clean up the `go.mod` and `go.sum` files. + +See [CLI tool](https://github.com/tdewolff/minify/tree/master/cmd/minify) for installation instructions of the binary. + +### Docker + +If you want to use Docker, please see https://hub.docker.com/r/tdewolff/minify. + +```bash +$ docker run -it tdewolff/minify --help +``` + +## API stability +There is no guarantee for absolute stability, but I take issues and bugs seriously and don't take API changes lightly. The library will be maintained in a compatible way unless vital bugs prevent me from doing so. There has been one API change after v1 which added options support and I took the opportunity to push through some more API clean up as well. There are no plans whatsoever for future API changes. + +## Testing +For all subpackages and the imported `parse` package, test coverage of 100% is pursued. Besides full coverage, the minifiers are [fuzz tested](https://github.com/tdewolff/fuzz) using [github.com/dvyukov/go-fuzz](http://www.github.com/dvyukov/go-fuzz), see [the wiki](https://github.com/tdewolff/minify/wiki) for the most important bugs found by fuzz testing. These tests ensure that everything works as intended and that the code does not crash (whatever the input). If you still encounter a bug, please file a [bug report](https://github.com/tdewolff/minify/issues)! + +## Performance +The benchmarks directory contains a number of standardized samples used to compare performance between changes. To give an indication of the speed of this library, I've ran the tests on my Thinkpad T460 (i5-6300U quad-core 2.4GHz running Arch Linux) using Go 1.15. + +``` +name time/op +CSS/sample_bootstrap.css-4 2.70ms ± 0% +CSS/sample_gumby.css-4 3.57ms ± 0% +CSS/sample_fontawesome.css-4 767µs ± 0% +CSS/sample_normalize.css-4 85.5µs ± 0% +HTML/sample_amazon.html-4 15.2ms ± 0% +HTML/sample_bbc.html-4 3.90ms ± 0% +HTML/sample_blogpost.html-4 420µs ± 0% +HTML/sample_es6.html-4 15.6ms ± 0% +HTML/sample_stackoverflow.html-4 3.73ms ± 0% +HTML/sample_wikipedia.html-4 6.60ms ± 0% +JS/sample_ace.js-4 28.7ms ± 0% +JS/sample_dot.js-4 357µs ± 0% +JS/sample_jquery.js-4 10.0ms ± 0% +JS/sample_jqueryui.js-4 20.4ms ± 0% +JS/sample_moment.js-4 3.47ms ± 0% +JSON/sample_large.json-4 3.25ms ± 0% +JSON/sample_testsuite.json-4 1.74ms ± 0% +JSON/sample_twitter.json-4 24.2µs ± 0% +SVG/sample_arctic.svg-4 34.7ms ± 0% +SVG/sample_gopher.svg-4 307µs ± 0% +SVG/sample_usa.svg-4 57.4ms ± 0% +SVG/sample_car.svg-4 18.0ms ± 0% +SVG/sample_tiger.svg-4 5.61ms ± 0% +XML/sample_books.xml-4 54.7µs ± 0% +XML/sample_catalog.xml-4 33.0µs ± 0% +XML/sample_omg.xml-4 7.17ms ± 0% + +name speed +CSS/sample_bootstrap.css-4 50.7MB/s ± 0% +CSS/sample_gumby.css-4 52.1MB/s ± 0% +CSS/sample_fontawesome.css-4 61.2MB/s ± 0% +CSS/sample_normalize.css-4 70.8MB/s ± 0% +HTML/sample_amazon.html-4 31.1MB/s ± 0% +HTML/sample_bbc.html-4 29.5MB/s ± 0% +HTML/sample_blogpost.html-4 49.8MB/s ± 0% +HTML/sample_es6.html-4 65.6MB/s ± 0% +HTML/sample_stackoverflow.html-4 55.0MB/s ± 0% +HTML/sample_wikipedia.html-4 67.5MB/s ± 0% +JS/sample_ace.js-4 22.4MB/s ± 0% +JS/sample_dot.js-4 14.5MB/s ± 0% +JS/sample_jquery.js-4 24.8MB/s ± 0% +JS/sample_jqueryui.js-4 23.0MB/s ± 0% +JS/sample_moment.js-4 28.6MB/s ± 0% +JSON/sample_large.json-4 234MB/s ± 0% +JSON/sample_testsuite.json-4 394MB/s ± 0% +JSON/sample_twitter.json-4 63.0MB/s ± 0% +SVG/sample_arctic.svg-4 42.4MB/s ± 0% +SVG/sample_gopher.svg-4 19.0MB/s ± 0% +SVG/sample_usa.svg-4 17.8MB/s ± 0% +SVG/sample_car.svg-4 29.3MB/s ± 0% +SVG/sample_tiger.svg-4 12.2MB/s ± 0% +XML/sample_books.xml-4 81.0MB/s ± 0% +XML/sample_catalog.xml-4 58.6MB/s ± 0% +XML/sample_omg.xml-4 159MB/s ± 0% +``` + +## HTML + +HTML (with JS and CSS) minification typically shaves off about 10%. + +The HTML5 minifier uses these minifications: + +- strip unnecessary whitespace and otherwise collapse it to one space (or newline if it originally contained a newline) +- strip superfluous quotes, or uses single/double quotes whichever requires fewer escapes +- strip default attribute values and attribute boolean values +- strip some empty attributes +- strip unrequired tags (`html`, `head`, `body`, ...) +- strip unrequired end tags (`tr`, `td`, `li`, ... and often `p`) +- strip default protocols (`http:`, `https:` and `javascript:`) +- strip all comments (including conditional comments, old IE versions are not supported anymore by Microsoft) +- shorten `doctype` and `meta` charset +- lowercase tags, attributes and some values to enhance gzip compression + +Options: + +- `KeepConditionalComments` preserve all IE conditional comments such as `` and ``, see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax +- `KeepDefaultAttrVals` preserve default attribute values such as `` // Faulty JS + req := httptest.NewRequest(http.MethodGet, "/", nil) + rec := httptest.NewRecorder() + m.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + _, _ = w.Write([]byte(input)) + + if err = w.(io.Closer).Close(); err != nil { + panic(err) + } + })).ServeHTTP(rec, req) +} +``` + +#### ResponseWriter +``` go +func Serve(w http.ResponseWriter, r *http.Request) { + mw := m.ResponseWriter(w, r) + defer mw.Close() + w = mw + + http.ServeFile(w, r, path.Join("www", r.URL.Path)) +} +``` + +#### Custom response writer +ResponseWriter example which returns a ResponseWriter that minifies the content and then writes to the original ResponseWriter. Any write after applying this filter will be minified. +``` go +type MinifyResponseWriter struct { + http.ResponseWriter + io.WriteCloser +} + +func (m MinifyResponseWriter) Write(b []byte) (int, error) { + return m.WriteCloser.Write(b) +} + +// MinifyResponseWriter must be closed explicitly by calling site. +func MinifyFilter(mediatype string, res http.ResponseWriter) MinifyResponseWriter { + m := minify.New() + // add minfiers + + mw := m.Writer(mediatype, res) + return MinifyResponseWriter{res, mw} +} +``` + +``` go +// Usage +func(w http.ResponseWriter, req *http.Request) { + w = MinifyFilter("text/html", w) + if _, err := io.WriteString(w, "

This HTTP response will be minified.

"); err != nil { + panic(err) + } + if err := w.Close(); err != nil { + panic(err) + } + // Output:

This HTTP response will be minified. +} +``` + +### Templates + +Here's an example of a replacement for `template.ParseFiles` from `template/html`, which automatically minifies each template before parsing it. + +Be aware that minifying templates will work in most cases but not all. Because the HTML minifier only works for valid HTML5, your template must be valid HTML5 of itself. Template tags are parsed as regular text by the minifier. + +``` go +func compileTemplates(filenames ...string) (*template.Template, error) { + m := minify.New() + m.AddFunc("text/html", html.Minify) + + var tmpl *template.Template + for _, filename := range filenames { + name := filepath.Base(filename) + if tmpl == nil { + tmpl = template.New(name) + } else { + tmpl = tmpl.New(name) + } + + b, err := ioutil.ReadFile(filename) + if err != nil { + return nil, err + } + + mb, err := m.Bytes("text/html", b) + if err != nil { + return nil, err + } + tmpl.Parse(string(mb)) + } + return tmpl, nil +} +``` + +Example usage: + +``` go +templates := template.Must(compileTemplates("view.html", "home.html")) +``` + +## License +Released under the [MIT license](LICENSE.md). + +[1]: http://golang.org/ "Go Language" diff --git a/vendor/github.com/tdewolff/minify/v2/common.go b/vendor/github.com/tdewolff/minify/v2/common.go new file mode 100644 index 000000000..67dc0d121 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/common.go @@ -0,0 +1,515 @@ +package minify + +import ( + "bytes" + "encoding/base64" + + "github.com/tdewolff/parse/v2" + "github.com/tdewolff/parse/v2/strconv" +) + +var ( + textMimeBytes = []byte("text/plain") + charsetASCIIBytes = []byte("charset=us-ascii") + dataBytes = []byte("data:") + base64Bytes = []byte(";base64") +) + +// Epsilon is the closest number to zero that is not considered to be zero. +var Epsilon = 0.00001 + +// Mediatype minifies a given mediatype by removing all whitespace. +func Mediatype(b []byte) []byte { + j := 0 + start := 0 + inString := false + for i, c := range b { + if !inString && parse.IsWhitespace(c) { + if start != 0 { + j += copy(b[j:], b[start:i]) + } else { + j += i + } + start = i + 1 + } else if c == '"' { + inString = !inString + } + } + if start != 0 { + j += copy(b[j:], b[start:]) + return parse.ToLower(b[:j]) + } + return parse.ToLower(b) +} + +// DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt. +func DataURI(m *M, dataURI []byte) []byte { + origData := parse.Copy(dataURI) + mediatype, data, err := parse.DataURI(dataURI) + if err != nil { + return dataURI + } + + data, _ = m.Bytes(string(mediatype), data) + base64Len := len(";base64") + base64.StdEncoding.EncodedLen(len(data)) + asciiLen := len(data) + for _, c := range data { + if parse.DataURIEncodingTable[c] { + asciiLen += 2 + } + if asciiLen > base64Len { + break + } + } + if len(origData) < base64Len && len(origData) < asciiLen { + return origData + } + if base64Len < asciiLen { + encoded := make([]byte, base64Len-len(";base64")) + base64.StdEncoding.Encode(encoded, data) + data = encoded + mediatype = append(mediatype, base64Bytes...) + } else { + data = parse.EncodeURL(data, parse.DataURIEncodingTable) + } + if len("text/plain") <= len(mediatype) && parse.EqualFold(mediatype[:len("text/plain")], textMimeBytes) { + mediatype = mediatype[len("text/plain"):] + } + for i := 0; i+len(";charset=us-ascii") <= len(mediatype); i++ { + // must start with semicolon and be followed by end of mediatype or semicolon + if mediatype[i] == ';' && parse.EqualFold(mediatype[i+1:i+len(";charset=us-ascii")], charsetASCIIBytes) && (i+len(";charset=us-ascii") >= len(mediatype) || mediatype[i+len(";charset=us-ascii")] == ';') { + mediatype = append(mediatype[:i], mediatype[i+len(";charset=us-ascii"):]...) + break + } + } + return append(append(append(dataBytes, mediatype...), ','), data...) +} + +// MaxInt is the maximum value of int. +const MaxInt = int(^uint(0) >> 1) + +// MinInt is the minimum value of int. +const MinInt = -MaxInt - 1 + +// Decimal minifies a given byte slice containing a decimal and removes superfluous characters. It differs from Number in that it does not parse exponents. +// It does not parse or output exponents. prec is the number of significant digits. When prec is zero it will keep all digits. Only digits after the dot can be removed to reach the number of significant digits. Very large number may thus have more significant digits. +func Decimal(num []byte, prec int) []byte { + if len(num) <= 1 { + return num + } + + // omit first + and register mantissa start and end, whether it's negative and the exponent + neg := false + start := 0 + dot := -1 + end := len(num) + if 0 < end && (num[0] == '+' || num[0] == '-') { + if num[0] == '-' { + neg = true + } + start++ + } + for i, c := range num[start:] { + if c == '.' { + dot = start + i + break + } + } + if dot == -1 { + dot = end + } + + // trim leading zeros but leave at least one digit + for start < end-1 && num[start] == '0' { + start++ + } + // trim trailing zeros + i := end - 1 + for ; dot < i; i-- { + if num[i] != '0' { + end = i + 1 + break + } + } + if i == dot { + end = dot + if start == end { + num[start] = '0' + return num[start : start+1] + } + } else if start == end-1 && num[start] == '0' { + return num[start:end] + } + + // apply precision + if 0 < prec && dot <= start+prec { + precEnd := start + prec + 1 // include dot + if dot == start { // for numbers like .012 + digit := start + 1 + for digit < end && num[digit] == '0' { + digit++ + } + precEnd = digit + prec + } + if precEnd < end { + end = precEnd + + // process either an increase from a lesser significant decimal (>= 5) + // or remove trailing zeros after the dot, or both + i := end - 1 + inc := '5' <= num[end] + for ; start < i; i-- { + if i == dot { + // no-op + } else if inc && num[i] != '9' { + num[i]++ + inc = false + break + } else if inc && i < dot { // end inc for integer + num[i] = '0' + } else if !inc && (i < dot || num[i] != '0') { + break + } + } + if i < dot { + end = dot + } else { + end = i + 1 + } + + if inc { + if dot == start && end == start+1 { + num[start] = '1' + } else if num[start] == '9' { + num[start] = '1' + num[start+1] = '0' + end++ + } else { + num[start]++ + } + } + } + } + + if neg { + start-- + num[start] = '-' + } + return num[start:end] +} + +// Number minifies a given byte slice containing a number and removes superfluous characters. +func Number(num []byte, prec int) []byte { + if len(num) <= 1 { + return num + } + + // omit first + and register mantissa start and end, whether it's negative and the exponent + neg := false + start := 0 + dot := -1 + end := len(num) + origExp := 0 + if num[0] == '+' || num[0] == '-' { + if num[0] == '-' { + neg = true + } + start++ + } + for i, c := range num[start:] { + if c == '.' { + dot = start + i + } else if c == 'e' || c == 'E' { + end = start + i + i += start + 1 + if i < len(num) && num[i] == '+' { + i++ + } + if tmpOrigExp, n := strconv.ParseInt(num[i:]); 0 < n && int64(MinInt) <= tmpOrigExp && tmpOrigExp <= int64(MaxInt) { + // range checks for when int is 32 bit + origExp = int(tmpOrigExp) + } else { + return num + } + break + } + } + if dot == -1 { + dot = end + } + + // trim leading zeros but leave at least one digit + for start < end-1 && num[start] == '0' { + start++ + } + // trim trailing zeros + i := end - 1 + for ; dot < i; i-- { + if num[i] != '0' { + end = i + 1 + break + } + } + if i == dot { + end = dot + if start == end { + num[start] = '0' + return num[start : start+1] + } + } else if start == end-1 && num[start] == '0' { + return num[start:end] + } + + // apply precision + if 0 < prec { //&& (dot <= start+prec || start+prec+1 < dot || 0 < origExp) { // don't minify 9 to 10, but do 999 to 1e3 and 99e1 to 1e3 + precEnd := start + prec + if dot == start { // for numbers like .012 + digit := start + 1 + for digit < end && num[digit] == '0' { + digit++ + } + precEnd = digit + prec + } else if dot < precEnd { // for numbers where precision will include the dot + precEnd++ + } + if precEnd < end && (dot < end || 1 < dot-precEnd+origExp) { // do not minify 9=>10 or 99=>100 or 9e1=>1e2 (but 90), but 999=>1e3 and 99e1=>1e3 + end = precEnd + inc := '5' <= num[end] + if dot == end { + inc = end+1 < len(num) && '5' <= num[end+1] + } + if precEnd < dot { + origExp += dot - precEnd + dot = precEnd + } + // process either an increase from a lesser significant decimal (>= 5) + // and remove trailing zeros + i := end - 1 + for ; start < i; i-- { + if i == dot { + // no-op + } else if inc && num[i] != '9' { + num[i]++ + inc = false + break + } else if !inc && num[i] != '0' { + break + } + } + end = i + 1 + if end < dot { + origExp += dot - end + dot = end + } + if inc { // single digit left + if dot == start { + num[start] = '1' + dot = start + 1 + } else if num[start] == '9' { + num[start] = '1' + origExp++ + } else { + num[start]++ + } + } + } + } + + // n is the number of significant digits + // normExp would be the exponent if it were normalised (0.1 <= f < 1) + n := 0 + normExp := 0 + if dot == start { + for i = dot + 1; i < end; i++ { + if num[i] != '0' { + n = end - i + normExp = dot - i + 1 + break + } + } + } else if dot == end { + normExp = end - start + for i = end - 1; start <= i; i-- { + if num[i] != '0' { + n = i + 1 - start + end = i + 1 + break + } + } + } else { + n = end - start - 1 + normExp = dot - start + } + + if origExp < 0 && (normExp < MinInt-origExp || normExp-n < MinInt-origExp) || 0 < origExp && (MaxInt-origExp < normExp || MaxInt-origExp < normExp-n) { + return num // exponent overflow + } + normExp += origExp + + // intExp would be the exponent if it were an integer + intExp := normExp - n + lenIntExp := strconv.LenInt(int64(intExp)) + lenNormExp := strconv.LenInt(int64(normExp)) + + // there are three cases to consider when printing the number + // case 1: without decimals and with a positive exponent (large numbers: 5e4) + // case 2: with decimals and with a negative exponent (small numbers with many digits: .123456e-4) + // case 3: with decimals and without an exponent (around zero: 5.6) + // case 4: without decimals and with a negative exponent (small numbers: 123456e-9) + if n <= normExp { + // case 1: print number with positive exponent + if dot < end { + // remove dot, either from the front or copy the smallest part + if dot == start { + start = end - n + } else if dot-start < end-dot-1 { + copy(num[start+1:], num[start:dot]) + start++ + } else { + copy(num[dot:], num[dot+1:end]) + end-- + } + } + if n+3 <= normExp { + num[end] = 'e' + end++ + for i := end + lenIntExp - 1; end <= i; i-- { + num[i] = byte(intExp%10) + '0' + intExp /= 10 + } + end += lenIntExp + } else if n+2 == normExp { + num[end] = '0' + num[end+1] = '0' + end += 2 + } else if n+1 == normExp { + num[end] = '0' + end++ + } + } else if normExp < -3 && lenNormExp < lenIntExp && dot < end { + // case 2: print normalized number (0.1 <= f < 1) + zeroes := -normExp + origExp + if 0 < zeroes { + copy(num[start+1:], num[start+1+zeroes:end]) + end -= zeroes + } else if zeroes < 0 { + copy(num[start+1:], num[start:dot]) + num[start] = '.' + } + num[end] = 'e' + num[end+1] = '-' + end += 2 + for i := end + lenNormExp - 1; end <= i; i-- { + num[i] = -byte(normExp%10) + '0' + normExp /= 10 + } + end += lenNormExp + } else if -lenIntExp-1 <= normExp { + // case 3: print number without exponent + zeroes := -normExp + if 0 < zeroes { + // dot placed at the front and negative exponent, adding zeroes + newDot := end - n - zeroes - 1 + if newDot != dot { + d := start - newDot + if 0 < d { + if dot < end { + // copy original digits after the dot towards the end + copy(num[dot+1+d:], num[dot+1:end]) + if start < dot { + // copy original digits before the dot towards the end + copy(num[start+d+1:], num[start:dot]) + } + } else if start < dot { + // copy original digits before the dot towards the end + copy(num[start+d:], num[start:dot]) + } + newDot = start + end += d + } else { + start += -d + } + num[newDot] = '.' + for i := 0; i < zeroes; i++ { + num[newDot+1+i] = '0' + } + } + } else { + // dot placed in the middle of the number + if dot == start { + // when there are zeroes after the dot + dot = end - n - 1 + start = dot + } else if end <= dot { + // when input has no dot in it + dot = end + end++ + } + newDot := start + normExp + // move digits between dot and newDot towards the end + if dot < newDot { + copy(num[dot:], num[dot+1:newDot+1]) + } else if newDot < dot { + copy(num[newDot+1:], num[newDot:dot]) + } + num[newDot] = '.' + } + } else { + // case 4: print number with negative exponent + // find new end, considering moving numbers to the front, removing the dot and increasing the length of the exponent + newEnd := end + if dot == start { + newEnd = start + n + } else { + newEnd-- + } + newEnd += 2 + lenIntExp + + exp := intExp + lenExp := lenIntExp + if newEnd < len(num) { + // it saves space to convert the decimal to an integer and decrease the exponent + if dot < end { + if dot == start { + copy(num[start:], num[end-n:end]) + end = start + n + } else { + copy(num[dot:], num[dot+1:end]) + end-- + } + } + } else { + // it does not save space and will panic, so we revert to the original representation + exp = origExp + lenExp = 1 + if origExp <= -10 || 10 <= origExp { + lenExp = strconv.LenInt(int64(origExp)) + } + } + num[end] = 'e' + num[end+1] = '-' + end += 2 + for i := end + lenExp - 1; end <= i; i-- { + num[i] = -byte(exp%10) + '0' + exp /= 10 + } + end += lenExp + } + + if neg { + start-- + num[start] = '-' + } + return num[start:end] +} + +func UpdateErrorPosition(err error, input *parse.Input, offset int) error { + if perr, ok := err.(*parse.Error); ok { + r := bytes.NewBuffer(input.Bytes()) + line, column, _ := parse.Position(r, offset) + perr.Line += line - 1 + perr.Column += column - 1 + return perr + } + return err +} diff --git a/vendor/github.com/tdewolff/minify/v2/html/buffer.go b/vendor/github.com/tdewolff/minify/v2/html/buffer.go new file mode 100644 index 000000000..f58367b44 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/html/buffer.go @@ -0,0 +1,137 @@ +package html + +import ( + "github.com/tdewolff/parse/v2" + "github.com/tdewolff/parse/v2/html" +) + +// Token is a single token unit with an attribute value (if given) and hash of the data. +type Token struct { + html.TokenType + Hash Hash + Data []byte + Text []byte + AttrVal []byte + Traits traits + Offset int +} + +// TokenBuffer is a buffer that allows for token look-ahead. +type TokenBuffer struct { + r *parse.Input + l *html.Lexer + + buf []Token + pos int + + attrBuffer []*Token +} + +// NewTokenBuffer returns a new TokenBuffer. +func NewTokenBuffer(r *parse.Input, l *html.Lexer) *TokenBuffer { + return &TokenBuffer{ + r: r, + l: l, + buf: make([]Token, 0, 8), + } +} + +func (z *TokenBuffer) read(t *Token) { + t.Offset = z.r.Offset() + t.TokenType, t.Data = z.l.Next() + t.Text = z.l.Text() + if t.TokenType == html.AttributeToken { + t.Offset += 1 + len(t.Text) + 1 + t.AttrVal = z.l.AttrVal() + if len(t.AttrVal) > 1 && (t.AttrVal[0] == '"' || t.AttrVal[0] == '\'') { + t.Offset++ + t.AttrVal = t.AttrVal[1 : len(t.AttrVal)-1] // quotes will be readded in attribute loop if necessary + } + t.Hash = ToHash(t.Text) + t.Traits = attrMap[t.Hash] + } else if t.TokenType == html.StartTagToken || t.TokenType == html.EndTagToken { + t.AttrVal = nil + t.Hash = ToHash(t.Text) + t.Traits = tagMap[t.Hash] // zero if not exist + } else { + t.AttrVal = nil + t.Hash = 0 + t.Traits = 0 + } +} + +// Peek returns the ith element and possibly does an allocation. +// Peeking past an error will panic. +func (z *TokenBuffer) Peek(pos int) *Token { + pos += z.pos + if pos >= len(z.buf) { + if len(z.buf) > 0 && z.buf[len(z.buf)-1].TokenType == html.ErrorToken { + return &z.buf[len(z.buf)-1] + } + + c := cap(z.buf) + d := len(z.buf) - z.pos + p := pos - z.pos + 1 // required peek length + var buf []Token + if 2*p > c { + buf = make([]Token, 0, 2*c+p) + } else { + buf = z.buf + } + copy(buf[:d], z.buf[z.pos:]) + + buf = buf[:p] + pos -= z.pos + for i := d; i < p; i++ { + z.read(&buf[i]) + if buf[i].TokenType == html.ErrorToken { + buf = buf[:i+1] + pos = i + break + } + } + z.pos, z.buf = 0, buf + } + return &z.buf[pos] +} + +// Shift returns the first element and advances position. +func (z *TokenBuffer) Shift() *Token { + if z.pos >= len(z.buf) { + t := &z.buf[:1][0] + z.read(t) + return t + } + t := &z.buf[z.pos] + z.pos++ + return t +} + +// Attributes extracts the gives attribute hashes from a tag. +// It returns in the same order pointers to the requested token data or nil. +func (z *TokenBuffer) Attributes(hashes ...Hash) []*Token { + n := 0 + for { + if t := z.Peek(n); t.TokenType != html.AttributeToken { + break + } + n++ + } + if len(hashes) > cap(z.attrBuffer) { + z.attrBuffer = make([]*Token, len(hashes)) + } else { + z.attrBuffer = z.attrBuffer[:len(hashes)] + for i := range z.attrBuffer { + z.attrBuffer[i] = nil + } + } + for i := z.pos; i < z.pos+n; i++ { + attr := &z.buf[i] + for j, hash := range hashes { + if hash == attr.Hash { + z.attrBuffer[j] = attr + } + } + } + return z.attrBuffer +} diff --git a/vendor/github.com/tdewolff/minify/v2/html/hash.go b/vendor/github.com/tdewolff/minify/v2/html/hash.go new file mode 100644 index 000000000..3b91cbbd5 --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/html/hash.go @@ -0,0 +1,543 @@ +package html + +// generated by hasher -type=Hash -file=hash.go; DO NOT EDIT, except for adding more constants to the list and rerun go generate + +// uses github.com/tdewolff/hasher +//go:generate hasher -type=Hash -file=hash.go + +// Hash defines perfect hashes for a predefined list of strings +type Hash uint32 + +// Unique hash definitions to be used instead of strings +const ( + A Hash = 0x1 // a + Abbr Hash = 0x37a04 // abbr + About Hash = 0x5 // about + Accept Hash = 0x1106 // accept + Accept_Charset Hash = 0x110e // accept-charset + Action Hash = 0x23f06 // action + Address Hash = 0x5a07 // address + Align Hash = 0x32705 // align + Alink Hash = 0x7005 // alink + Allowfullscreen Hash = 0x2ad0f // allowfullscreen + Amp_Boilerplate Hash = 0x610f // amp-boilerplate + Area Hash = 0x1e304 // area + Article Hash = 0x2707 // article + Aside Hash = 0xb405 // aside + Async Hash = 0xac05 // async + Audio Hash = 0xd105 // audio + Autofocus Hash = 0xe409 // autofocus + Autoplay Hash = 0x10808 // autoplay + Axis Hash = 0x11004 // axis + B Hash = 0x101 // b + Background Hash = 0x300a // background + Base Hash = 0x19604 // base + Bb Hash = 0x37b02 // bb + Bdi Hash = 0x7503 // bdi + Bdo Hash = 0x31f03 // bdo + Bgcolor Hash = 0x12607 // bgcolor + Blockquote Hash = 0x13e0a // blockquote + Body Hash = 0xd04 // body + Br Hash = 0x37c02 // br + Button Hash = 0x14806 // button + Canvas Hash = 0xb006 // canvas + Caption Hash = 0x21f07 // caption + Charset Hash = 0x1807 // charset + Checked Hash = 0x1b307 // checked + Cite Hash = 0xfb04 // cite + Class Hash = 0x15905 // class + Classid Hash = 0x15907 // classid + Clear Hash = 0x2b05 // clear + Code Hash = 0x19204 // code + Codebase Hash = 0x19208 // codebase + Codetype Hash = 0x1a408 // codetype + Col Hash = 0x12803 // col + Colgroup Hash = 0x1bb08 // colgroup + Color Hash = 0x12805 // color + Cols Hash = 0x1cf04 // cols + Colspan Hash = 0x1cf07 // colspan + Compact Hash = 0x1ec07 // compact + Content Hash = 0x28407 // content + Controls Hash = 0x20108 // controls + Data Hash = 0x1f04 // data + Datalist Hash = 0x1f08 // datalist + Datatype Hash = 0x4d08 // datatype + Dd Hash = 0x5b02 // dd + Declare Hash = 0xb707 // declare + Default Hash = 0x7f07 // default + DefaultChecked Hash = 0x1730e // defaultChecked + DefaultMuted Hash = 0x7f0c // defaultMuted + DefaultSelected Hash = 0x8a0f // defaultSelected + Defer Hash = 0x9805 // defer + Del Hash = 0x10503 // del + Details Hash = 0x15f07 // details + Dfn Hash = 0x16c03 // dfn + Dialog Hash = 0xa606 // dialog + Dir Hash = 0x7603 // dir + Disabled Hash = 0x18008 // disabled + Div Hash = 0x18703 // div + Dl Hash = 0x1b902 // dl + Dt Hash = 0x23102 // dt + Em Hash = 0x4302 // em + Embed Hash = 0x4905 // embed + Enabled Hash = 0x26c07 // enabled + Enctype Hash = 0x1fa07 // enctype + Face Hash = 0x5604 // face + Fieldset Hash = 0x21408 // fieldset + Figcaption Hash = 0x21c0a // figcaption + Figure Hash = 0x22606 // figure + Footer Hash = 0xdb06 // footer + For Hash = 0x23b03 // for + Form Hash = 0x23b04 // form + Formaction Hash = 0x23b0a // formaction + Formnovalidate Hash = 0x2450e // formnovalidate + Frame Hash = 0x28c05 // frame + Frameborder Hash = 0x28c0b // frameborder + H1 Hash = 0x2e002 // h1 + H2 Hash = 0x25302 // h2 + H3 Hash = 0x25502 // h3 + H4 Hash = 0x25702 // h4 + H5 Hash = 0x25902 // h5 + H6 Hash = 0x25b02 // h6 + Head Hash = 0x2d204 // head + Header Hash = 0x2d206 // header + Hgroup Hash = 0x25d06 // hgroup + Hidden Hash = 0x26806 // hidden + Hr Hash = 0x32d02 // hr + Href Hash = 0x32d04 // href + Hreflang Hash = 0x32d08 // hreflang + Html Hash = 0x27304 // html + Http_Equiv Hash = 0x2770a // http-equiv + I Hash = 0x2401 // i + Icon Hash = 0x28304 // icon + Id Hash = 0xb602 // id + Iframe Hash = 0x28b06 // iframe + Img Hash = 0x29703 // img + Inert Hash = 0xf605 // inert + Inlist Hash = 0x29a06 // inlist + Input Hash = 0x2a405 // input + Ins Hash = 0x2a903 // ins + Ismap Hash = 0x11205 // ismap + Itemscope Hash = 0xfc09 // itemscope + Kbd Hash = 0x7403 // kbd + Keygen Hash = 0x1f606 // keygen + Label Hash = 0xbe05 // label + Lang Hash = 0x33104 // lang + Language Hash = 0x33108 // language + Legend Hash = 0x2c506 // legend + Li Hash = 0x2302 // li + Link Hash = 0x7104 // link + Longdesc Hash = 0xc208 // longdesc + Main Hash = 0xf404 // main + Manifest Hash = 0x2bc08 // manifest + Map Hash = 0xee03 // map + Mark Hash = 0x2cb04 // mark + Math Hash = 0x2cf04 // math + Max Hash = 0x2d803 // max + Maxlength Hash = 0x2d809 // maxlength + Media Hash = 0xa405 // media + Menu Hash = 0x12204 // menu + Meta Hash = 0x2e204 // meta + Meter Hash = 0x2f705 // meter + Method Hash = 0x2fc06 // method + Multiple Hash = 0x30208 // multiple + Muted Hash = 0x30a05 // muted + Name Hash = 0xa204 // name + Nav Hash = 0x32403 // nav + Nohref Hash = 0x32b06 // nohref + Noresize Hash = 0x13608 // noresize + Noscript Hash = 0x14d08 // noscript + Noshade Hash = 0x16e07 // noshade + Novalidate Hash = 0x2490a // novalidate + Nowrap Hash = 0x1d506 // nowrap + Object Hash = 0xd506 // object + Ol Hash = 0xcb02 // ol + Open Hash = 0x32104 // open + Optgroup Hash = 0x35608 // optgroup + Option Hash = 0x30f06 // option + Output Hash = 0x206 // output + P Hash = 0x501 // p + Param Hash = 0xf005 // param + Pauseonexit Hash = 0x1160b // pauseonexit + Picture Hash = 0x1c207 // picture + Plaintext Hash = 0x1da09 // plaintext + Poster Hash = 0x26206 // poster + Pre Hash = 0x35d03 // pre + Prefix Hash = 0x35d06 // prefix + Profile Hash = 0x36407 // profile + Progress Hash = 0x34208 // progress + Property Hash = 0x31508 // property + Q Hash = 0x14301 // q + Rb Hash = 0x2f02 // rb + Readonly Hash = 0x1e408 // readonly + Rel Hash = 0xbc03 // rel + Required Hash = 0x22a08 // required + Resource Hash = 0x1c708 // resource + Rev Hash = 0x7803 // rev + Reversed Hash = 0x7808 // reversed + Rows Hash = 0x9c04 // rows + Rowspan Hash = 0x9c07 // rowspan + Rp Hash = 0x6a02 // rp + Rt Hash = 0x2802 // rt + Rtc Hash = 0xf903 // rtc + Ruby Hash = 0xe004 // ruby + Rules Hash = 0x12c05 // rules + S Hash = 0x1c01 // s + Samp Hash = 0x6004 // samp + Scope Hash = 0x10005 // scope + Scoped Hash = 0x10006 // scoped + Script Hash = 0x14f06 // script + Scrolling Hash = 0xc809 // scrolling + Seamless Hash = 0x19808 // seamless + Section Hash = 0x13007 // section + Select Hash = 0x16506 // select + Selected Hash = 0x16508 // selected + Shape Hash = 0x19f05 // shape + Size Hash = 0x13a04 // size + Slot Hash = 0x20804 // slot + Small Hash = 0x2ab05 // small + Sortable Hash = 0x2ef08 // sortable + Source Hash = 0x1c906 // source + Span Hash = 0x9f04 // span + Src Hash = 0x34903 // src + Srcset Hash = 0x34906 // srcset + Start Hash = 0x2505 // start + Strong Hash = 0x29e06 // strong + Style Hash = 0x2c205 // style + Sub Hash = 0x31d03 // sub + Summary Hash = 0x33907 // summary + Sup Hash = 0x34003 // sup + Svg Hash = 0x34f03 // svg + Tabindex Hash = 0x2e408 // tabindex + Table Hash = 0x2f205 // table + Target Hash = 0x706 // target + Tbody Hash = 0xc05 // tbody + Td Hash = 0x1e02 // td + Template Hash = 0x4208 // template + Text Hash = 0x1df04 // text + Textarea Hash = 0x1df08 // textarea + Tfoot Hash = 0xda05 // tfoot + Th Hash = 0x2d102 // th + Thead Hash = 0x2d105 // thead + Time Hash = 0x12004 // time + Title Hash = 0x15405 // title + Tr Hash = 0x1f202 // tr + Track Hash = 0x1f205 // track + Translate Hash = 0x20b09 // translate + Truespeed Hash = 0x23209 // truespeed + Type Hash = 0x5104 // type + Typemustmatch Hash = 0x1a80d // typemustmatch + Typeof Hash = 0x5106 // typeof + U Hash = 0x301 // u + Ul Hash = 0x8302 // ul + Undeterminate Hash = 0x370d // undeterminate + Usemap Hash = 0xeb06 // usemap + Valign Hash = 0x32606 // valign + Value Hash = 0x18905 // value + Valuetype Hash = 0x18909 // valuetype + Var Hash = 0x28003 // var + Video Hash = 0x35205 // video + Visible Hash = 0x36b07 // visible + Vlink Hash = 0x37205 // vlink + Vocab Hash = 0x37705 // vocab + Wbr Hash = 0x37e03 // wbr + Xmlns Hash = 0x2eb05 // xmlns + Xmp Hash = 0x36203 // xmp +) + +// String returns the hash' name. +func (i Hash) String() string { + start := uint32(i >> 8) + n := uint32(i & 0xff) + if start+n > uint32(len(_Hash_text)) { + return "" + } + return _Hash_text[start : start+n] +} + +// ToHash returns the hash whose name is s. It returns zero if there is no +// such hash. It is case sensitive. +func ToHash(s []byte) Hash { + if len(s) == 0 || len(s) > _Hash_maxLen { + return 0 + } + h := uint32(_Hash_hash0) + for i := 0; i < len(s); i++ { + h ^= uint32(s[i]) + h *= 16777619 + } + if i := _Hash_table[h&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { + t := _Hash_text[i>>8 : i>>8+i&0xff] + for i := 0; i < len(s); i++ { + if t[i] != s[i] { + goto NEXT + } + } + return i + } +NEXT: + if i := _Hash_table[(h>>16)&uint32(len(_Hash_table)-1)]; int(i&0xff) == len(s) { + t := _Hash_text[i>>8 : i>>8+i&0xff] + for i := 0; i < len(s); i++ { + if t[i] != s[i] { + return 0 + } + } + return i + } + return 0 +} + +const _Hash_hash0 = 0x9acb0442 +const _Hash_maxLen = 15 +const _Hash_text = "aboutputargetbodyaccept-charsetdatalistarticlearbackgroundet" + + "erminatemplatembedatatypeofaceaddressamp-boilerplatealinkbdi" + + "reversedefaultMutedefaultSelectedeferowspanamedialogasyncanv" + + "asideclarelabelongdescrollingaudiobjectfooterubyautofocusema" + + "paramainertcitemscopedelautoplayaxismapauseonexitimenubgcolo" + + "rulesectionoresizeblockquotebuttonoscriptitleclassidetailsel" + + "ectedfnoshadefaultCheckedisabledivaluetypecodebaseamlesshape" + + "codetypemustmatcheckedlcolgroupicturesourcecolspanowraplaint" + + "extareadonlycompactrackeygenctypecontrolslotranslatefieldset" + + "figcaptionfigurequiredtruespeedformactionformnovalidateh2h3h" + + "4h5h6hgrouposterhiddenabledhtmlhttp-equivaricontentiframebor" + + "derimginlistronginputinsmallowfullscreenmanifestylegendmarkm" + + "atheadermaxlength1metabindexmlnsortablemetermethodmultiplemu" + + "tedoptionpropertysubdopenavalignohreflanguagesummarysuprogre" + + "ssrcsetsvgvideoptgrouprefixmprofilevisiblevlinkvocabbrwbr" + +var _Hash_table = [1 << 9]Hash{ + 0x0: 0x1df08, // textarea + 0x4: 0x32d02, // hr + 0x8: 0x1c207, // picture + 0xb: 0x18905, // value + 0xf: 0x2e408, // tabindex + 0x12: 0x15905, // class + 0x15: 0x37e03, // wbr + 0x18: 0x1a80d, // typemustmatch + 0x1a: 0x1b902, // dl + 0x1d: 0xf903, // rtc + 0x1e: 0x25702, // h4 + 0x22: 0x2ef08, // sortable + 0x24: 0x4208, // template + 0x25: 0x28c0b, // frameborder + 0x28: 0x37a04, // abbr + 0x29: 0x28b06, // iframe + 0x2a: 0x610f, // amp-boilerplate + 0x2c: 0x1e408, // readonly + 0x30: 0x23f06, // action + 0x33: 0x28c05, // frame + 0x35: 0x12c05, // rules + 0x36: 0x30208, // multiple + 0x38: 0x31f03, // bdo + 0x39: 0x1d506, // nowrap + 0x3e: 0x21408, // fieldset + 0x3f: 0x7503, // bdi + 0x46: 0x7f0c, // defaultMuted + 0x49: 0x35205, // video + 0x4c: 0x19808, // seamless + 0x4d: 0x13608, // noresize + 0x4f: 0xb602, // id + 0x51: 0x25d06, // hgroup + 0x52: 0x23102, // dt + 0x55: 0x12805, // color + 0x56: 0x34003, // sup + 0x59: 0x370d, // undeterminate + 0x5a: 0x35608, // optgroup + 0x5b: 0x2d206, // header + 0x5c: 0xb405, // aside + 0x5f: 0x10005, // scope + 0x60: 0x101, // b + 0x61: 0xcb02, // ol + 0x64: 0x32b06, // nohref + 0x65: 0x1da09, // plaintext + 0x66: 0x20804, // slot + 0x67: 0x11004, // axis + 0x68: 0x12803, // col + 0x69: 0x32606, // valign + 0x6c: 0x2d105, // thead + 0x70: 0x34906, // srcset + 0x71: 0x26806, // hidden + 0x76: 0x1bb08, // colgroup + 0x78: 0x34f03, // svg + 0x7b: 0x2cb04, // mark + 0x7e: 0x33104, // lang + 0x81: 0x1cf04, // cols + 0x86: 0x5a07, // address + 0x8b: 0xf404, // main + 0x8c: 0x4302, // em + 0x8f: 0x32d08, // hreflang + 0x93: 0x1b307, // checked + 0x94: 0x25902, // h5 + 0x95: 0x301, // u + 0x96: 0x32705, // align + 0x97: 0x14301, // q + 0x99: 0xd506, // object + 0x9b: 0x28407, // content + 0x9d: 0xc809, // scrolling + 0x9f: 0x36407, // profile + 0xa0: 0x34903, // src + 0xa1: 0xda05, // tfoot + 0xa3: 0x2f705, // meter + 0xa4: 0x37705, // vocab + 0xa6: 0xd04, // body + 0xa8: 0x19204, // code + 0xac: 0x20108, // controls + 0xb0: 0x2ab05, // small + 0xb1: 0x18008, // disabled + 0xb5: 0x5604, // face + 0xb6: 0x501, // p + 0xb9: 0x2302, // li + 0xbb: 0xe409, // autofocus + 0xbf: 0x27304, // html + 0xc2: 0x4d08, // datatype + 0xc6: 0x35d06, // prefix + 0xcb: 0x35d03, // pre + 0xcc: 0x1106, // accept + 0xd1: 0x23b03, // for + 0xd5: 0x29e06, // strong + 0xd6: 0x9c07, // rowspan + 0xd7: 0x25502, // h3 + 0xd8: 0x2cf04, // math + 0xde: 0x16e07, // noshade + 0xdf: 0x19f05, // shape + 0xe1: 0x10006, // scoped + 0xe3: 0x706, // target + 0xe6: 0x21c0a, // figcaption + 0xe9: 0x1df04, // text + 0xea: 0x1c708, // resource + 0xec: 0xee03, // map + 0xf0: 0x29a06, // inlist + 0xf1: 0x16506, // select + 0xf2: 0x1f606, // keygen + 0xf3: 0x5106, // typeof + 0xf6: 0xb006, // canvas + 0xf7: 0x30f06, // option + 0xf8: 0xbe05, // label + 0xf9: 0xbc03, // rel + 0xfb: 0x1f04, // data + 0xfd: 0x6004, // samp + 0x100: 0x110e, // accept-charset + 0x101: 0xeb06, // usemap + 0x103: 0x2bc08, // manifest + 0x109: 0xa204, // name + 0x10a: 0x14806, // button + 0x10b: 0x2b05, // clear + 0x10e: 0x33907, // summary + 0x10f: 0x2e204, // meta + 0x110: 0x33108, // language + 0x112: 0x300a, // background + 0x113: 0x2707, // article + 0x116: 0x23b0a, // formaction + 0x119: 0x1, // a + 0x11b: 0x5, // about + 0x11c: 0xfc09, // itemscope + 0x11e: 0x14d08, // noscript + 0x11f: 0x15907, // classid + 0x120: 0x36203, // xmp + 0x121: 0x19604, // base + 0x123: 0x1c01, // s + 0x124: 0x36b07, // visible + 0x126: 0x37b02, // bb + 0x127: 0x9c04, // rows + 0x12d: 0x2450e, // formnovalidate + 0x131: 0x1f205, // track + 0x135: 0x18703, // div + 0x136: 0xac05, // async + 0x137: 0x31508, // property + 0x13a: 0x16c03, // dfn + 0x13e: 0xf605, // inert + 0x142: 0x10503, // del + 0x144: 0x25302, // h2 + 0x147: 0x2c205, // style + 0x149: 0x29703, // img + 0x14a: 0xc05, // tbody + 0x14b: 0x7603, // dir + 0x14c: 0x2eb05, // xmlns + 0x14e: 0x1f08, // datalist + 0x14f: 0x32d04, // href + 0x150: 0x1f202, // tr + 0x151: 0x13e0a, // blockquote + 0x152: 0x18909, // valuetype + 0x155: 0xdb06, // footer + 0x157: 0x14f06, // script + 0x158: 0x1cf07, // colspan + 0x15d: 0x1730e, // defaultChecked + 0x15f: 0x2490a, // novalidate + 0x164: 0x1a408, // codetype + 0x165: 0x2c506, // legend + 0x16b: 0x1160b, // pauseonexit + 0x16c: 0x21f07, // caption + 0x16f: 0x26c07, // enabled + 0x173: 0x26206, // poster + 0x175: 0x30a05, // muted + 0x176: 0x11205, // ismap + 0x178: 0x2a903, // ins + 0x17a: 0xe004, // ruby + 0x17b: 0x37c02, // br + 0x17c: 0x8a0f, // defaultSelected + 0x17d: 0x7403, // kbd + 0x17f: 0x1c906, // source + 0x182: 0x9f04, // span + 0x184: 0x2d803, // max + 0x18a: 0x5b02, // dd + 0x18b: 0x13a04, // size + 0x18c: 0xa405, // media + 0x18d: 0x19208, // codebase + 0x18f: 0x4905, // embed + 0x192: 0x5104, // type + 0x193: 0xf005, // param + 0x194: 0x25b02, // h6 + 0x197: 0x28304, // icon + 0x198: 0x12607, // bgcolor + 0x199: 0x2ad0f, // allowfullscreen + 0x19a: 0x12004, // time + 0x19b: 0x7803, // rev + 0x19d: 0x34208, // progress + 0x19e: 0x22606, // figure + 0x1a0: 0x6a02, // rp + 0x1a2: 0xa606, // dialog + 0x1a4: 0x2802, // rt + 0x1a7: 0x1e304, // area + 0x1a8: 0x7808, // reversed + 0x1aa: 0x32104, // open + 0x1ac: 0x2d204, // head + 0x1ad: 0x7005, // alink + 0x1af: 0x28003, // var + 0x1b0: 0x15f07, // details + 0x1b1: 0x2401, // i + 0x1b3: 0x1e02, // td + 0x1b4: 0xb707, // declare + 0x1b5: 0x8302, // ul + 0x1ba: 0x2fc06, // method + 0x1bd: 0x13007, // section + 0x1be: 0x22a08, // required + 0x1c2: 0x9805, // defer + 0x1c3: 0x37205, // vlink + 0x1c4: 0x15405, // title + 0x1c5: 0x2770a, // http-equiv + 0x1c6: 0x1fa07, // enctype + 0x1c7: 0x1ec07, // compact + 0x1c8: 0x2d809, // maxlength + 0x1c9: 0x16508, // selected + 0x1cc: 0xd105, // audio + 0x1cd: 0xc208, // longdesc + 0x1d1: 0xfb04, // cite + 0x1da: 0x2505, // start + 0x1de: 0x2d102, // th + 0x1df: 0x10808, // autoplay + 0x1e2: 0x7104, // link + 0x1e3: 0x206, // output + 0x1e5: 0x12204, // menu + 0x1e6: 0x2a405, // input + 0x1eb: 0x32403, // nav + 0x1ec: 0x31d03, // sub + 0x1ee: 0x1807, // charset + 0x1ef: 0x7f07, // default + 0x1f3: 0x2f205, // table + 0x1f4: 0x23b04, // form + 0x1f5: 0x23209, // truespeed + 0x1f6: 0x2f02, // rb + 0x1fb: 0x20b09, // translate + 0x1fd: 0x2e002, // h1 +} diff --git a/vendor/github.com/tdewolff/minify/v2/html/html.go b/vendor/github.com/tdewolff/minify/v2/html/html.go new file mode 100644 index 000000000..3431ad3be --- /dev/null +++ b/vendor/github.com/tdewolff/minify/v2/html/html.go @@ -0,0 +1,513 @@ +// Package html minifies HTML5 following the specifications at http://www.w3.org/TR/html5/syntax.html. +package html + +import ( + "bytes" + "io" + + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/parse/v2" + "github.com/tdewolff/parse/v2/buffer" + "github.com/tdewolff/parse/v2/html" +) + +var ( + gtBytes = []byte(">") + isBytes = []byte("=") + spaceBytes = []byte(" ") + doctypeBytes = []byte("") + jsMimeBytes = []byte("application/javascript") + cssMimeBytes = []byte("text/css") + htmlMimeBytes = []byte("text/html") + svgMimeBytes = []byte("image/svg+xml") + formMimeBytes = []byte("application/x-www-form-urlencoded") + mathMimeBytes = []byte("application/mathml+xml") + dataSchemeBytes = []byte("data:") + jsSchemeBytes = []byte("javascript:") + httpBytes = []byte("http") + radioBytes = []byte("radio") + onBytes = []byte("on") + textBytes = []byte("text") + noneBytes = []byte("none") + submitBytes = []byte("submit") + allBytes = []byte("all") + rectBytes = []byte("rect") + dataBytes = []byte("data") + getBytes = []byte("get") + autoBytes = []byte("auto") + oneBytes = []byte("one") + inlineParams = map[string]string{"inline": "1"} +) + +//////////////////////////////////////////////////////////////// + +// Minifier is an HTML minifier. +type Minifier struct { + KeepComments bool + KeepConditionalComments bool + KeepDefaultAttrVals bool + KeepDocumentTags bool + KeepEndTags bool + KeepQuotes bool + KeepWhitespace bool +} + +// Minify minifies HTML data, it reads from r and writes to w. +func Minify(m *minify.M, w io.Writer, r io.Reader, params map[string]string) error { + return (&Minifier{}).Minify(m, w, r, params) +} + +// Minify minifies HTML data, it reads from r and writes to w. +func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, _ map[string]string) error { + var rawTagHash Hash + var rawTagMediatype []byte + + omitSpace := true // if true the next leading space is omitted + inPre := false + + attrMinifyBuffer := buffer.NewWriter(make([]byte, 0, 64)) + attrByteBuffer := make([]byte, 0, 64) + + z := parse.NewInput(r) + defer z.Restore() + + l := html.NewLexer(z) + tb := NewTokenBuffer(z, l) + for { + t := *tb.Shift() + switch t.TokenType { + case html.ErrorToken: + if _, err := w.Write(nil); err != nil { + return err + } + if l.Err() == io.EOF { + return nil + } + return l.Err() + case html.DoctypeToken: + w.Write(doctypeBytes) + case html.CommentToken: + if o.KeepComments { + w.Write(t.Data) + } else if o.KeepConditionalComments && 6 < len(t.Text) && (bytes.HasPrefix(t.Text, []byte("[if ")) || bytes.HasSuffix(t.Text, []byte("[endif]")) || bytes.HasSuffix(t.Text, []byte("[endif]--"))) { + // [if ...] is always 7 or more characters, [endif] is only encountered for downlevel-revealed + // see https://msdn.microsoft.com/en-us/library/ms537512(v=vs.85).aspx#syntax + if bytes.HasPrefix(t.Data, []byte("")) { // downlevel-hidden + begin := bytes.IndexByte(t.Data, '>') + 1 + end := len(t.Data) - len("") + w.Write(t.Data[:begin]) + if err := o.Minify(m, w, buffer.NewReader(t.Data[begin:end]), nil); err != nil { + return minify.UpdateErrorPosition(err, z, t.Offset) + } + w.Write(t.Data[end:]) + } else { + w.Write(t.Data) // downlevel-revealed or short downlevel-hidden + } + } else if 1 < len(t.Text) && t.Text[0] == '#' { + // SSI tags + w.Write(t.Data) + } + case html.SvgToken: + if err := m.MinifyMimetype(svgMimeBytes, w, buffer.NewReader(t.Data), nil); err != nil { + if err != minify.ErrNotExist { + return minify.UpdateErrorPosition(err, z, t.Offset) + } + w.Write(t.Data) + } + case html.MathToken: + if err := m.MinifyMimetype(mathMimeBytes, w, buffer.NewReader(t.Data), nil); err != nil { + if err != minify.ErrNotExist { + return minify.UpdateErrorPosition(err, z, t.Offset) + } + w.Write(t.Data) + } + case html.TextToken: + // CSS and JS minifiers for inline code + if rawTagHash != 0 { + if rawTagHash == Style || rawTagHash == Script || rawTagHash == Iframe { + var mimetype []byte + var params map[string]string + if rawTagHash == Iframe { + mimetype = htmlMimeBytes + } else if len(rawTagMediatype) > 0 { + mimetype, params = parse.Mediatype(rawTagMediatype) + } else if rawTagHash == Script { + mimetype = jsMimeBytes + } else if rawTagHash == Style { + mimetype = cssMimeBytes + } + if err := m.MinifyMimetype(mimetype, w, buffer.NewReader(t.Data), params); err != nil { + if err != minify.ErrNotExist { + return minify.UpdateErrorPosition(err, z, t.Offset) + } + w.Write(t.Data) + } + } else { + w.Write(t.Data) + } + } else if inPre { + w.Write(t.Data) + } else { + t.Data = parse.ReplaceMultipleWhitespaceAndEntities(t.Data, EntitiesMap, TextRevEntitiesMap) + + // whitespace removal; trim left + if omitSpace && parse.IsWhitespace(t.Data[0]) { + t.Data = t.Data[1:] + } + + // whitespace removal; trim right + omitSpace = false + if len(t.Data) == 0 { + omitSpace = true + } else if parse.IsWhitespace(t.Data[len(t.Data)-1]) { + omitSpace = true + i := 0 + for { + next := tb.Peek(i) + // trim if EOF, text token with leading whitespace or block token + if next.TokenType == html.ErrorToken { + t.Data = t.Data[:len(t.Data)-1] + omitSpace = false + break + } else if next.TokenType == html.TextToken { + // this only happens when a comment, doctype or phrasing end tag (only for !o.KeepWhitespace) was in between + // remove if the text token starts with a whitespace + if len(next.Data) > 0 && parse.IsWhitespace(next.Data[0]) { + t.Data = t.Data[:len(t.Data)-1] + omitSpace = false + } + break + } else if next.TokenType == html.StartTagToken || next.TokenType == html.EndTagToken { + if o.KeepWhitespace { + break + } + // remove when followed up by a block tag + if next.Traits&nonPhrasingTag != 0 { + t.Data = t.Data[:len(t.Data)-1] + omitSpace = false + break + } else if next.TokenType == html.StartTagToken { + break + } + } + i++ + } + } + + w.Write(t.Data) + } + case html.StartTagToken, html.EndTagToken: + rawTagHash = 0 + hasAttributes := false + if t.TokenType == html.StartTagToken { + if next := tb.Peek(0); next.TokenType == html.AttributeToken { + hasAttributes = true + } + if t.Traits&rawTag != 0 { + // ignore empty script and style tags + if !hasAttributes && (t.Hash == Script || t.Hash == Style) { + if next := tb.Peek(1); next.TokenType == html.EndTagToken { + tb.Shift() + tb.Shift() + break + } + } + rawTagHash = t.Hash + rawTagMediatype = nil + + // do not minify content of