8000 [pull] master from tdewolff:master by pull[bot] · Pull Request #194 · 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
2,356 changes: 1,427 additions & 929 deletions bindings/js/package-lock.json

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions bindings/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
"go"
],
"devDependencies": {
"@types/node": "^26.1.0",
"tsdown": "^0.22.3",
"typescript": "^6.0.3",
"vitest": "^4.1.9"
"@types/node": "^26.1.1",
"tsdown": "^0.22.7",
"typescript": "^7.0.2",
"vitest": "^4.1.10"
},
"dependencies": {
"koffi": "^3.0.2"
"koffi": "^3.1.1"
}
}
2 changes: 1 addition & 1 deletion common.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func Number(num []byte, prec int) []byte {
start += -d
}
num[newDot] = '.'
for i := 0; i < zeroes; i++ {
for i := range zeroes {
num[newDot+1+i] = '0'
}
}
Expand Down
4 changes: 2 additions & 2 deletions common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ var numbers [][]byte

func TestMain(t *testing.T) {
numbers = make([][]byte, 0, n)
for j := 0; j < n; j++ {
for range n {
numbers = append(numbers, RandNumBytes(true))
}
}
Expand Down Expand Up @@ -389,7 +389,7 @@ func RandNumBytes(withExp bool) []byte {

func BenchmarkNumber(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < n; j++ {
for j := range n {
Number(numbers[j], -1)
}
}
Expand Down
16 changes: 8 additions & 8 deletions css/css.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func (c *cssMinifier) minifyTokens(prop Hash, fun Hash, values []Token) []Token

if a == 1.0 && (len(vals) == 3 || len(vals) == 4) { // only minify color if fully opaque
if fun == Rgb || fun == Rgba {
for j := 0; j < 3; j++ {
for j := range 3 {
if args[j*2].TokenType == css.NumberToken {
vals[j] /= 255.0
if vals[j] < minify.Epsilon {
Expand Down Expand Up @@ -628,14 +628,14 @@ func (c *cssMinifier) minifyTokens(prop Hash, fun Hash, values []Token) []Token
if 3 <= len(vals) && (fun == Rgb || fun == Rgba) {
// 0%, 20%, 40%, 60%, 80% and 100% can be represented exactly as, 51, 102, 153, 204, and 255 respectively
removePercentage := true
for j := 0; j < 3; j++ {
for j := range 3 {
if args[j*2].TokenType != css.PercentageToken || 2.0*minify.Epsilon <= math.Mod(vals[j]+minify.Epsilon, 0.2) {
removePercentage = false
break
}
}
if removePercentage {
for j := 0; j < 3; j++ {
for j := range 3 {
args[j*2].TokenType = css.NumberToken
if vals[j] < minify.Epsilon {
args[j*2].Data = zeroBytes
Expand Down Expand Up @@ -735,7 +735,7 @@ func (c *cssMinifier) minifyProperty(prop Hash, values []Token) []Token {
parse.ToLower(value.Data)
s := value.Data[1 : len(value.Data)-1]
if 0 < len(s) {
for _, split := range bytes.Split(s, spaceBytes) {
for split := range bytes.SplitSeq(s, spaceBytes) {
// if len is zero, it contains two consecutive spaces
if len(split) == 0 || !css.IsIdent(split) {
unquote = false
Expand Down Expand Up @@ -1337,7 +1337,7 @@ func (c *cssMinifier) minifyProperty(prop Hash, values []Token) []Token {
values = append(values, Token{css.CommaToken, commaBytes, nil, 0, None})
}
if ran[0] == ran[1] {
urange := []byte(fmt.Sprintf("U+%X", ran[0]))
urange := fmt.Appendf(nil, "U+%X", ran[0])
values = append(values, Token{css.UnicodeRangeToken, urange, nil, 0, None})
} else if ran[0] == 0 && ran[1] == 0x10FFFF {
values = append(values, Token{css.IdentToken, initialBytes, nil, 0, None})
Expand All @@ -1357,12 +1357,12 @@ func (c *cssMinifier) minifyProperty(prop Hash, values []Token) []Token {
var urange []byte
if wildcards != 0 {
if ran[0]>>(wildcards*4) == 0 {
urange = []byte(fmt.Sprintf("U+%s", strings.Repeat("?", wildcards)))
urange = fmt.Appendf(nil, "U+%s", strings.Repeat("?", wildcards))
} else {
urange = []byte(fmt.Sprintf("U+%X%s", ran[0]>>(wildcards*4), strings.Repeat("?", wildcards)))
urange = fmt.Appendf(nil, "U+%X%s", ran[0]>>(wildcards*4), strings.Repeat("?", wildcards))
}
} else {
urange = []byte(fmt.Sprintf("U+%X-%X", ran[0], ran[1]))
urange = fmt.Appendf(nil, "U+%X-%X", ran[0], ran[1])
}
values = append(values, Token{css.UnicodeRangeToken, urange, nil, 0, None})
}
Expand Down
6 changes: 3 additions & 3 deletions css/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,13 +877,13 @@ func ToHash(s []byte) Hash {
// return HashMap[string(s)]
//}
h := uint32(_Hash_hash0)
for i := 0; i < len(s); i++ {
for i := range s {
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++ {
for i := range s {
if t[i] != s[i] {
goto NEXT
}
Expand All @@ -893,7 +893,7 @@ func ToHash(s []byte) Hash {
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++ {
for i := range s {
if t[i] != s[i] {
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ require (
require (
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/sys v0.47.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/tdewolff/test v1.0.12 h1:7F21DqIajswxuche0geHdrUZRCWE4oko4b7bcmkkrxk=
github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
6 changes: 3 additions & 3 deletions html/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,13 @@ func ToHash(s []byte) Hash {
// return HashMap[string(s)]
//}
h := uint32(_Hash_hash0)
for i := 0; i < len(s); i++ {
for i := range s {
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++ {
for i := range s {
if t[i] != s[i] {
goto NEXT
}
Expand All @@ -583,7 +583,7 @@ func ToHash(s []byte) Hash {
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++ {
for i := range s {
if t[i] != s[i] {
return 0
}
Expand Down
4 changes: 2 additions & 2 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ func TestRenamerIndices(t *testing.T) {
j := renamer.getIndex(name)
test.T(t, j, i, string(name))
}
for i := 0; i < 100000; i++ {
for i := range 100000 {
name := renamer.getName([]byte{' '}, i)
j := renamer.getIndex(name)
test.T(t, j, i, string(name))
Expand All @@ -1027,7 +1027,7 @@ func BenchmarkJQuery(b *testing.B) {
if err != nil {
panic(err)
}
for j := 0; j < 10; j++ {
for j := range 10 {
b.Run(fmt.Sprintf("%d", j), func(b *testing.B) {
b.SetBytes(int64(len(buf)))
for i := 0; i < b.N; i++ {
Expand Down
7 changes: 3 additions & 4 deletions js/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package js
import (
"bytes"
"encoding/hex"
"slices"
stdStrconv "strconv"
"unicode/utf8"

Expand Down Expand Up @@ -366,10 +367,8 @@ func hasSideEffects(i js.IExpr) bool {
case *js.CondExpr:
return hasSideEffects(expr.Cond) || hasSideEffects(expr.X) || hasSideEffects(expr.Y)
case *js.CommaExpr:
for _, item := range expr.List {
if hasSideEffects(item) {
return true
}
if slices.ContainsFunc(expr.List, hasSideEffects) {
return true
}
case *js.ArrayExpr:
for _, item := range expr.List {
Expand Down
7 changes: 2 additions & 5 deletions js/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,8 @@ func (m *jsMinifier) optimizeVarOrder(decl *js.VarDecl) {
if item.Default != nil {
refs = appendExprVars(refs[:0], item.Default)
for _, ref := range refs {
for _, binding := range bindings {
if binding == ref {
interferes = true
break
}
if slices.Contains(bindings, ref) {
interferes = true
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,12 @@ func (z *writer) Close() error {
func (m *M) Writer(mediatype string, w io.Writer) io.WriteCloser {
pr, pw := io.Pipe()
z := &writer{pw, sync.WaitGroup{}, false, nil}
z.wg.Add(1)
go func() {
defer z.wg.Done()
z.wg.Go(func() {
defer pr.Close()
if err := m.Minify(mediatype, w, pr); err != nil {
z.err = err
}
}()
})
return z
}

Expand Down Expand Up @@ -341,14 +339,12 @@ func (w *responseWriter) Write(b []byte) (int, error) {
if _, params, minifier := w.m.Match(w.mediatype); minifier != nil {
pr, pw := io.Pipe()
z := &writer{pw, sync.WaitGroup{}, false, nil}
z.wg.Add(1)
go func() {
defer z.wg.Done()
z.wg.Go(func() {
defer pr.Close()
if err := minifier(w.m, w.ResponseWriter, pr, params); err != nil {
z.err = err
}
}()
})
w.z = z
} else {
w.z = w.ResponseWriter
Expand Down
4 changes: 2 additions & 2 deletions svg/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestAttributes(t *testing.T) {
l := xml.NewLexer(r)
tb := NewTokenBuffer(r, l)
tb.Shift()
for k := 0; k < 2; k++ { // run twice to ensure similar results
for range 2 { // run twice to ensure similar results
attrs := tb.Attributes(X, Y, Width, Height, Rx, Ry)
for i := 0; i < 6; i++ {
for i := range 6 {
test.That(t, attrs[i] != nil, "attr must not be nil")
val := string(attrs[i].AttrVal)
j, _ := strconv.ParseInt(val, 10, 32)
Expand Down
6 changes: 3 additions & 3 deletions svg/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,13 @@ func ToHash(s []byte) Hash {
// return HashMap[string(s)]
//}
h := uint32(_Hash_hash0)
for i := 0; i < len(s); i++ {
for i := range s {
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++ {
for i := range s {
if t[i] != s[i] {
goto NEXT
}
Expand All @@ -277,7 +277,7 @@ func ToHash(s []byte) Hash {
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++ {
for i := range s {
if t[i] != s[i] {
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (o *Minifier) Minify(m *minify.M, w io.Writer, r io.Reader, params map[stri
} else if attr == ViewBox {
j := 0
newVal := val[:0]
for i := 0; i < 4; i++ {
for i := range 4 {
if i != 0 {
if j >= len(val) || val[j] != ' ' && val[j] != ',' {
newVal = append(newVal, val[j:]...)
Expand Down
Loading
0