8000 [pull] master from tdewolff:master by pull[bot] · Pull Request #211 · 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
8000
4 changes: 2 additions & 2 deletions svg/pathdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (p *PathData) copyInstruction(b []byte, cmd byte) int {
func (p *PathData) shortenCurPosInstruction(cmd byte, coords [][]byte) PathDataState {
state := p.state
p.curBuffer = p.curBuffer[:0]
if (cmd != state.cmd || cmd == 'M' || cmd == 'm') && !(state.cmd == 0 && cmd == 'M' || state.cmd == 'M' && cmd == 'L' || state.cmd == 'm' && cmd == 'l') {
if (cmd != state.cmd || cmd == 'M' || cmd == 'm') && !(state.cmd == 'M' && cmd == 'L' || state.cmd == 'm' && cmd == 'l') {
p.curBuffer = append(p.curBuffer, cmd)
state.prevDigit = false
state.prevDigitIsInt = false
Expand All @@ -378,7 +378,7 @@ func (p *PathData) shortenCurPosInstruction(cmd byte, coords [][]byte) PathDataS
func (p *PathData) shortenAltPosInstruction(cmd byte, coordFloats []float64, x, y float64) PathDataState {
state := p.state
p.altBuffer = p.altBuffer[:0]
if (cmd != state.cmd || cmd == 'M' || cmd == 'm') && !(state.cmd == 0 && cmd == 'M' || state.cmd == 'M' && cmd == 'L' || state.cmd == 'm' && cmd == 'l') {
if (cmd != state.cmd || cmd == 'M' || cmd == 'm') && !(state.cmd == 'M' && cmd == 'L' || state.cmd == 'm' && cmd == 'l') {
p.altBuffer = append(p.altBuffer, cmd)
state.prevDigit = false
state.prevDigitIsInt = false
Expand Down
94 changes: 47 additions & 47 deletions svg/pathdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,67 @@ func TestPathData(t *testing.T) {
pathData string
expected string
}{
{"M10 10 10 10", "10 10"},
{"M10 10 20 10", "10 10H20"},
{"M10 10 10 20", "10 10V20"},
{"M10 10L20 20", "10 10 20 20"},
{"M10 10l20 20", "10 10l20 20"},
{"M50 50 100 100", "50 50l50 50"},
{"m50 50 40 40m50 50", "50 50l40 40m50 50"},
{"M10 10zM15 15", "10 10zm5 5"},
{"M50 50H55V55", "50 50h5v5"},
{"M10 10L11 10 11 11", "10 10h1v1"},
{"M10 10l1 0 0 1", "10 10h1v1"},
{"M10 10v1", "10 10v1"},
{"M10 10L11 11 0 0", "10 10l1 1L0 0"},
{"M246.614 51.028L246.614-5.665 189.922-5.665", "246.614 51.028V-5.665H189.922"},
{"M100,200 C100,100 250,100 250,200 S400,300 400,200", "1e2 2e2c0-1e2 150-1e2 150 0s150 1e2 150 0"},
{"M200,300 Q400,50 600,300 T1000,300", "2e2 3e2q2e2-250 4e2.0t4e2.0"},
{"M300,200 h-150 a150,150 0 1,0 150,-150 z", "3e2 2e2H150A150 150 0 103e2 50z"},
{"M10 10 10 10", "10 10"},
{"M10 10 10 10", "M10 10"},
{"M10 10 20 10", "M10 10H20"},
{"M10 10 10 20", "M10 10V20"},
{"M10 10L20 20", "M10 10 20 20"},
{"M10 10l20 20", "M10 10l20 20"},
{"M50 50 100 100", "M50 50l50 50"},
{"m50 50 40 40m50 50", "m50 50 40 40m50 50"},
{"M10 10zM15 15", "M10 10zm5 5"},
{"M50 50H55V55", "M50 50h5v5"},
{"M10 10L11 10 11 11", "M10 10h1v1"},
{"M10 10l1 0 0 1", "M10 10h1v1"},
{"M10 10v1", "M10 10v1"},
{"M10 10L11 11 0 0", "M10 10l1 1L0 0"},
{"M246.614 51.028L246.614-5.665 189.922-5.665", "M246.614 51.028V-5.665H189.922"},
{"M100,200 C100,100 250,100 250,200 S400,300 400,200", "M1e2 2e2c0-1e2 150-1e2 150 0s150 1e2 150 0"},
{"M200,300 Q400,50 600,300 T1000,300", "M2e2 3e2q2e2-250 4e2.0t4e2.0"},
{"M300,200 h-150 a150,150 0 1,0 150,-150 z", "M3e2 2e2H150A150 150 0 103e2 50z"},
{"M10 10 10 10", "M10 10"},
{"x5 5L10 10", "x5 5L10 10"},

{"M.0.1", "0 .1"},
{"M200.0.1", "2e2.1"},
{"M0 0a3.28 3.28 0 0 0 3.279 3.28", "0 0a3.28 3.28.0 003.279 3.28"}, // #114
{"A1.1.0.0.0.0.2.3", "A1.1.0.0.0.0.2.3"}, // bad input (sweep and large-arc are not booleans) gives bad output
{"M.0.1", "M0 .1"},
{"M200.0.1", "M2e2.1"},
{"M0 0a3.28 3.28 0 0 0 3.279 3.28", "M0 0a3.28 3.28.0 003.279 3.28"}, // #114
{"A1.1.0.0.0.0.2.3", "A1.1.0.0.0.0.2.3"}, // bad input (sweep and large-arc are not booleans) gives bad output
{"A.0.0.4 0 0.0.3", "A0 0 .4 000 .3"},
{"A1.1.0.0 00.2.3L10 0", "A1.1.0.0 00.2.3L10 0"},
{"A5 5 0 010 0", "A5 5 0 010 0"}, // different flag syntax
{
"q6.55 0 10.56-2.93a9.36 9.36 0 004-8 10 10 0 00-3.37-7.83q-3.37-3-9.9-4.79A25.38 25.38 0 0137.76 44",
"q6.55.0 10.56-2.93a9.36 9.36.0 004-8 10 10 0 00-3.37-7.83q-3.37-3-9.9-4.79A25.38 25.38.0 0137.76 44"}, // #275
{
"-3.5498-0.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389-0+4.9389-5.3027z",
"-3.5498-.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389.0 4.9389-5.3027z"}, // #284
"M-3.5498-0.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389-0+4.9389-5.3027z",
"M-3.5498-.0882q0-5.1924-4.5861-5.1924h-1.819v10.495h1.4662q4.9389.0 4.9389-5.3027z"}, // #284
{"C10 0 0 10 10 10", "C10 0 0 10 10 10"},

// change/remove commands
{"M10 10L10 10L20 10z", "10 10H20z"},
{"M10 10t10 0", "10 10h10"},
{"M10 10T20 10", "10 10H20"},
{"M10 10T20 10T20 20", "10 10t10 0 0 10"},
{"M10 10q0 0 10 0", "10 10h10"},
{"M10 10Q10 10 20 10", "10 10H20"},
{"M10 10Q20 10 20 10", "10 10H20"},
{"M10 10Q15 20 20 10Q25 0 30 10", "10 10q5 10 10 0t10 0"},
{"M10 10s0 0 10 0", "10 10h10"},
{"M10 10S10 10 20 10", "10 10H20"},
{"M10 10S20 10 20 10", "10 10H20"},
{"M10 10c0 0 0 0 10 0", "10 10h10"},
{"M10 10C10 10 10 10 20 10", "10 10H20"},
{"M10 10C10 10 20 10 20 10", "10 10H20"},
{"M10 10C20 10 10 10 20 10", "10 10H20"},
{"M10 10C20 10 20 10 20 10", "10 10H20"},
{"M10 10C10 20 20 20 20 10C20 0 30 0 30 10", "10 10c0 10 10 10 10 0S30 0 30 10"},
{"M10 10L10 10L20 10z", "M10 10H20z"},
{"M10 10t10 0", "M10 10h10"},
{"M10 10T20 10", "M10 10H20"},
{"M10 10T20 10T20 20", "M10 10t10 0 0 10"},
{"M10 10q0 0 10 0", "M10 10h10"},
{"M10 10Q10 10 20 10", "M10 10H20"},
{"M10 10Q20 10 20 10", "M10 10H20"},
{"M10 10Q15 20 20 10Q25 0 30 10", "M10 10q5 10 10 0t10 0"},
{"M10 10s0 0 10 0", "M10 10h10"},
{"M10 10S10 10 20 10", "M10 10H20"},
{"M10 10S20 10 20 10", "M10 10H20"},
{"M10 10c0 0 0 0 10 0", "M10 10h10"},
{"M10 10C10 10 10 10 20 10", "M10 10H20"},
{"M10 10C10 10 20 10 20 10", "M10 10H20"},
{"M10 10C20 10 10 10 20 10", "M10 10H20"},
{"M10 10C20 10 20 10 20 10", "M10 10H20"},
{"M10 10C10 20 20 20 20 10C20 0 30 0 30 10", "M10 10c0 10 10 10 10 0S30 0 30 10"},
// does not have C/S -> Q/T since subsequent commands could rely on the control points, and both are incompatible for that

// fuzz
{"", ""},
{"ML", ""},
{".8.00c0", ".8.0"},
{".1.04h0e6.0e6.0e0.0", ".1.04h0 0 0 0"},
{"M.1.0.0.2Z", ".1.0.0.2z"},
{".8.00c0", "M.8.0"},
{".1.04h0e6.0e6.0e0.0", "M.1.04h0 0 0 0"},
{"M.1.0.0.2Z", "M.1.0.0.2z"},
{"A.0.0.0.0.3.2e3.7.0.0.0.0.0.1.3.0.0.0.0.2.3.2.0.0.0.0.20.2e-10.0.0.0.0.0.0.0.0", "A.0.0.0.0.3.2e3.7.0.0.0.0.0.1.3.0.0.0.0.2.3.2.0.0.0.0.20.2e-10.0.0.0.0.0.0.0.0"}, // bad input (sweep and large-arc are not booleans) gives bad output
{
"A.0.0.4.0.0.0.3.0.0.0.0.0.4.2.0.0.0.0.2.0.4.0.0.0.4.2.8.2.0.0.0.2.9.28.0.0.0.0.0.2.3.0.0.0.0.0.0.2.3.2.09e-03.0.0.0.0.8.0.0.0.0.0.0.0",
Expand All @@ -81,7 +81,7 @@ func TestPathData(t *testing.T) {
{"0a0z", "z"},
{"t.3.3.3.3z", "t.3.3.3.3z"},

{`m26 33.1-.5 5.3L23 38l-.2 2.7-5.3-.5M15 37.5l-2.6-.2-.5 5.3 5.3.4.4-5.3zl.4-5.3-2.6-.2.4-5.3 5.3.5-.2 2.6 5.2.4.3-2.6 5.2.4-.4 5.3-5.3-.4.2-2.7`, `26 33.1l-.5 5.3L23 38l-.2 2.7-5.3-.5M15 37.5l-2.6-.2-.5 5.3 5.3.4.4-5.3zl.4-5.3-2.6-.2.4-5.3 5.3.5-.2 2.6 5.2.4.3-2.6 5.2.4-.4 5.3-5.3-.4.2-2.7`}, // #1023
{`m26 33.1-.5 5.3L23 38l-.2 2.7-5.3-.5M15 37.5l-2.6-.2-.5 5.3 5.3.4.4-5.3zl.4-5.3-2.6-.2.4-5.3 5.3.5-.2 2.6 5.2.4.3-2.6 5.2.4-.4 5.3-5.3-.4.2-2.7`, `m26 33.1-.5 5.3L23 38l-.2 2.7-5.3-.5M15 37.5l-2.6-.2-.5 5.3 5.3.4.4-5.3zl.4-5.3-2.6-.2.4-5.3 5.3.5-.2 2.6 5.2.4.3-2.6 5.2.4-.4 5.3-5.3-.4.2-2.7`}, // #1023
}

p := NewPathData(&Minifier{})
Expand All @@ -98,8 +98,8 @@ func TestPathDataTruncated(t *testing.T) {
pathData string
expected string
}{
{"m100 0 50 50zM100 0z", "1e2.0l50 50zm0 0z"},
{"M194.4 16.4C194.4 7.4 187 0 177.9 0 168.8 0 161.5 7.4 161.5 16.4", "194 16.4C194 7.4 187 0 178 0S162 7.4 162 16.4"}, // #233
{"m100 0 50 50zM100 0z", "m1e2.0 50 50zm0 0z"},
{"M194.4 16.4C194.4 7.4 187 0 177.9 0 168.8 0 161.5 7.4 161.5 16.4", "M194 16.4C194 7.4 187 0 178 0S162 7.4 162 16.4"}, // #233
//{"M100 100L200.4 200.4l100.2 100.2", "M100 100L200 200L301 301"}, // remember rounding error
//{"M100 100L200.4 200.4L300.6 300.6", "M100 100L200 200L301 301"},
}
Expand Down
12 changes: 6 additions & 6 deletions svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ func TestSVG(t *testing.T) {
{`<path x="5.0px" y="0%"/>`, `<path x="5" y="0"/>`},
{`<svg viewBox="5.0px 5px 240IN px"><path/></svg>`, `<svg viewBox="5 5 240in px"><path/></svg>`},
{`<svg viewBox="5.0!5px"><path/></svg>`, `<svg viewBox="5!5px"><path/></svg>`},
{`<path d="M 100 100 L 300 100 L 200 100 z"/>`, `<path d="1e2 1e2H3e2 2e2z"/>`},
{`<path d="M100 -100M200 300z"/>`, `<path d="1e2-1e2M2e2 3e2z"/>`},
{`<path d="M0.5 0.6 M -100 0.5z"/>`, `<path d=".5.6M-1e2.5z"/>`},
{`<path d="M01.0 0.6 z"/>`, `<path d="1 .6z"/>`},
{`<path d="M20 20l-10-10z"/>`, `<path d="20 20 10 10z"/>`},
{`<path d="M 100 100 L 300 100 L 200 100 z"/>`, `<path d="M1e2 1e2H3e2 2e2z"/>`},
{`<path d="M100 -100M200 300z"/>`, `<path d="M1e2-1e2M2e2 3e2z"/>`},
{`<path d="M0.5 0.6 M -100 0.5z"/>`, `<path d="M.5.6M-1e2.5z"/>`},
{`<path d="M01.0 0.6 z"/>`, `<path d="M1 .6z"/>`},
{`<path d="M20 20l-10-10z"/>`, `<path d="M20 20 10 10z"/>`},
{`<?xml version="1.0" encoding="utf-8"?>`, ``},
{`<svg viewbox="0 0 16 16"><path/></svg>`, `<svg viewbox="0 0 16 16"><path/></svg>`},
{`<g></g>`, `<g/>`},
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestSVG(t *testing.T) {
d="m3.5 2c-.82843 0-1.5.67157-1.5 1.5 0 .6558404.4135873 1.2024109 1 1.40625v6.1875c-.5864127.203839-1 .75041-1 1.40625 0 .82843.67157 1.5 1.5 1.5s1.5-.67157 1.5-1.5c0-.65584-.4135873-1.202411-1-1.40625v-6.1875c.0771596-.0268209.1479033-.0552639.21875-.09375l2.875 2.15625c-.0599582.1621025-.09375.348305-.09375.53125 0 .82843.67157 1.5 1.5 1.5s1.5-.67157 1.5-1.5-.67157-1.5-1.5-1.5c-.2847728 0-.5544546.0809461-.78125.21875l-2.84375-2.125c.0759039-.1794577.125-.3866425.125-.59375 0-.82843-.67157-1.5-1.5-1.5zm0 1c.27614 0 .5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm5 4c.27614 0 .5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm2.5.78125v4.3125l-2-2.03125v1.4375l2.28125 2.28125.21875.21875.21875-.21875 2.28125-2.28125v-1.4375l-2 2.03125v-4.3125zm-7.5 4.21875c.27614 0 .5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5z"
fill="#eff0f1"
id="path1" />
</svg>`, `<svg viewBox="0 0 16 16" id="svg1" xmlns="http://www.w3.org/2000/svg"><path d="3.5 2C2.67157 2 2 2.67157 2 3.5c0 .6558404.4135873 1.2024109 1 1.40625v6.1875c-.5864127.203839-1 .75041-1 1.40625.0.82843.67157 1.5 1.5 1.5S5 13.32843 5 12.5c0-.65584-.4135873-1.202411-1-1.40625v-6.1875c.0771596-.0268209.1479033-.0552639.21875-.09375l2.875 2.15625C7.0337918 7.1308525 7 7.317055 7 7.5 7 8.32843 7.67157 9 8.5 9S10 8.32843 10 7.5 9.32843 6 8.5 6c-.2847728.0-.5544546.0809461-.78125.21875L4.875 4.09375C4.9509039 3.9142923 5 3.7071075 5 3.5 5 2.67157 4.32843 2 3.5 2zm0 1c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm5 4c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm2.5.78125v4.3125L9 10.0625V11.5l2.28125 2.28125L11.5 14l.21875-.21875L14 11.5v-1.4375l-2 2.03125v-4.3125z3.5 12c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5z" fill="#eff0f1" id="path1"/></svg>`},
</svg>`, `<svg viewBox="0 0 16 16" id="svg1" xmlns="http://www.w3.org/2000/svg"><path d="m3.5 2C2.67157 2 2 2.67157 2 3.5c0 .6558404.4135873 1.2024109 1 1.40625v6.1875c-.5864127.203839-1 .75041-1 1.40625.0.82843.67157 1.5 1.5 1.5S5 13.32843 5 12.5c0-.65584-.4135873-1.202411-1-1.40625v-6.1875c.0771596-.0268209.1479033-.0552639.21875-.09375l2.875 2.15625C7.0337918 7.1308525 7 7.317055 7 7.5 7 8.32843 7.67157 9 8.5 9S10 8.32843 10 7.5 9.32843 6 8.5 6c-.2847728.0-.5544546.0809461-.78125.21875L4.875 4.09375C4.9509039 3.9142923 5 3.7071075 5 3.5 5 2.67157 4.32843 2 3.5 2zm0 1c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm5 4c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5zm2.5.78125v4.3125L9 10.0625V11.5l2.28125 2.28125L11.5 14l.21875-.21875L14 11.5v-1.4375l-2 2.03125v-4.3125zM3.5 12c.27614.0.5.22386.5.5s-.22386.5-.5.5-.5-.22386-.5-.5.22386-.5.5-.5z" fill="#eff0f1" id="path1"/></svg>`},
}

m := minify.New()
Expand Down
Loading
0