8000 [pull] master from tdewolff:master by pull[bot] · Pull Request #174 · RepoBackup/minify · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parse/tests/*/corpus/*
bindings/js/build
bindings/js/.cache
bindings/js/node_modules
bindings/js/pnpm-lock.yaml
bindings/js/example/package-lock.json
bindings/js/example/node_modules
bindings/js/example/test.min.html
Expand Down
31 changes: 24 additions & 7 deletions css/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -174,6 +175,7 @@ func (c *cssMinifier) minifyGrammar() {
semicolonQueued := false
for {
gt, _, data := c.p.Next()
Next:
switch gt {
case css.ErrorGrammar:
if c.p.HasParseError() {
Expand Down Expand Up @@ -236,14 +238,29 @@ func (c *cssMinifier) minifyGrammar() {
}
semicolonQueued = true
case css.BeginAtRuleGrammar:
c.w.Write(data)
for _, val := range c.p.Values() {
c.w.Write(val.Data)
rule := slices.Clone(data)
values := slices.Clone(c.p.Values())
gt, _, data = c.p.Next()
if gt == css.EndAtRuleGrammar {
gt, _, data = c.p.Next()
} else {
c.w.Write(rule)
for _, val := range values {
c.w.Write(val.Data)
}
c.w.Write(leftBracketBytes)
}
c.w.Write(leftBracketBytes)
goto Next
case css.BeginRulesetGrammar:
c.minifySelectors(c.p.Values())
c.w.Write(leftBracketBytes)
selectors := slices.Clone(c.p.Values())
gt, _, data = c.p.Next()
if gt == css.EndRulesetGrammar {
gt, _, data = c.p.Next()
} else {
c.minifySelectors(selectors)
c.w.Write(leftBracketBytes)
}
goto Next
case css.DeclarationGrammar:
c.minifyDeclaration(data, c.p.Values())
semicolonQueued = true
Expand Down Expand Up @@ -272,7 +289,7 @@ func (c *cssMinifier) minifyGrammar() {
func (c *cssMinifier) minifySelectors(values []css.Token) {
inAttr := false
isClass := false
for _, val := range c.p.Values() {
for _, val := range values {
if !inAttr {
if val.TokenType == css.IdentToken {
if !isClass {
Expand Down
19 changes: 10 additions & 9 deletions css/css_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestCSS(t *testing.T) {
{"/*! bang comment */", "/*!bang comment*/"},
{"/*# sourceMappingURL=url */", ""},
{"a;", "a"},
{"i{}/*! bang comment */", "i{}/*!bang comment*/"},
{"i{a:b;}/*! bang comment */", "i{a:b}/*!bang comment*/"},
{"i { key: value; key2: value; }", "i{key:value;key2:value}"},
{".cla .ss > #id { x:y; }", ".cla .ss>#id{x:y}"},
{".cla[id ^= L] { x:y; }", ".cla[id^=L]{x:y}"},
Expand All @@ -36,15 +36,16 @@ func TestCSS(t *testing.T) {
{"input[type=\"radio\" i]{x:y}", "input[type=radio i]{x:y}"},
{"DIV{margin:1em}", "div{margin:1em}"},
{".CLASS{margin:1em}", ".CLASS{margin:1em}"},
{"@MEDIA all{}", "@media all{}"},
{"@media only screen and (max-width : 800px){}", "@media only screen and (max-width:800px){}"},
{"@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:1.5dppx){}", "@media(-webkit-min-device-pixel-ratio:1.5),(min-resolution:1.5dppx){}"},
{"@MEDIA all{a:b}", "@media all{a:b}"},
{"@media only screen and (max-width : 800px){a:b}", "@media only screen and (max-width:800px){a:b}"},
{"@media (-webkit-min-device-pixel-ratio:1.5),(min-resolution:1.5dppx){a:b}", "@media(-webkit-min-device-pixel-ratio:1.5),(min-resolution:1.5dppx){a:b}"},
{"[class^=icon-] i[class^=icon-],i[class*=\" icon-\"]{x:y}", "[class^=icon-] i[class^=icon-],i[class*=\" icon-\"]{x:y}"},
{"html{line-height:1;}html{line-height:1;}", "html{line-height:1}html{line-height:1}"},
{"a { b: 1", "a{b:1}"},
{"@unknown { border:1px solid #000 }", "@unknown{border:1px solid #000 }"},
{":root { --custom-variable:0px; }", ":root{--custom-variable:0px}"},
{"a, b, c {color:red}", "a,b,c{color:red}"},
{"a, b, c { /* lala */ }", ""},

// recurring property overwrites previous
//{"a{color:blue;color:red}", "a{color:red}"},
Expand All @@ -54,7 +55,7 @@ func TestCSS(t *testing.T) {
//{"a{color:blue}a{color:red}", "a{color:blue}a{color:red}"}, // not supported

// case sensitivity
{"@counter-style Ident{}", "@counter-style Ident{}"},
{"@counter-style Ident{a:b}", "@counter-style Ident{a:b}"},

// coverage
{"a, b + c { x:y; }", "a,b+c{x:y}"},
Expand All @@ -65,7 +66,7 @@ func TestCSS(t *testing.T) {
{".clearfix { order:4; *zoom: 1px; color:red; }", ".clearfix{order:4;*zoom:1px;color:red}"},

// go-fuzz
{"input[type=\"\x00\"] { a: b\n}.a{}", "input[type=\"\x00\"]{a:b}.a{}"},
{"input[type=\"\x00\"] { a: b\n}.a{b:c}", "input[type=\"\x00\"]{a:b}.a{b:c}"},
{"a{a:)'''", "a{a:)'''}"},
{"{T:l(", "{t:l()}"},
{"{background:0 0 0", "{background:0 0}"},
Expand All @@ -80,8 +81,8 @@ func TestCSS(t *testing.T) {
{"a{@media screen and (min-width:1024px){ width: 40%; } & h1 { font-size: clamp(2.5rem, 1rem + 3vw, 3.5rem)}}", "a{@media screen and (min-width:1024px){width: 40%;}& h1{font-size:clamp(2.5rem,1rem + 3vw,3.5rem)}}"}, // #602
{"a{padding:calc(var(--dce-edge-xsmall,6px) - 2px) calc(var(--dce-button-horizontal-padding,18px) - 2px)}", "a{padding:calc(var(--dce-edge-xsmall,6px) - 2px)calc(var(--dce-button-horizontal-padding,18px) - 2px)}"}, // #673
{"a{border-color:var(--dce-brand-color,#01A982)var(--dce-brand-color,#01A982)var(--dce-border-weak,#0000001F)}", "a{border-color:var(--dce-brand-color,#01A982)var(--dce-brand-color,#01A982)var(--dce-border-weak,#0000001F)}"}, // #673
{"a{& :is(b) { } }", "a{& :is(b){}}"}, // #908
{"a{&:is(b) :is(c) { } }", "a{&:is(b) :is(c){}}"}, // #908
{"a{& :is(b) {c:d;} }", "a{& :is(b){c:d}}"}, // #908
{"a{&:is(b) :is(c) {d:e;} }", "a{&:is(b) :is(c){d:e}}"}, // #908
}

m := minify.New()
Expand Down Expand Up @@ -485,7 +486,7 @@ func TestWriterErrors(t *testing.T) {
n []int
}{
{`@import 'file'`, []int{0, 2}},
{`@media all{}`, []int{0, 2, 3, 4}},
{`@media all{a:b}`, []int{0, 2, 3, 4}},
{`a[id^="L"]{margin:2in!important;color:red}`, []int{0, 4, 6, 7, 8, 9, 10, 11}},
{`a{color:rgb(255,0,0)}`, []int{4}},
{`a{color:rgb(255,255,255)}`, []int{4}},
Expand Down
2 changes: 2 additions & 0 deletions publish.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ cd -
#cd ../bindings/py
#make publish
#cd -

rm -f v$VERSION.tar.gz
1 change: 0 additions & 1 deletion svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ func TestSVG(t *testing.T) {
{`<svg:rect width="100%" height="100%" fill="green"/>`, `<rect width="100%" height="100%" fill="green"/>`},
{`<xlink:rect xlink:width="100%"/>`, `<xlink:rect xlink:width="100%"/>`},

// from SVGO
{`<!DOCTYPE bla><?xml?><!-- comment --><metadata/>`, ``},

{`<polygon points="-0.1,"/>`, `<polygon points="-0.1,"/>`}, // #45
Expand Down
Loading
0