| 1 |
package css |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/tdewolff/parse/v2" |
| 10 |
"github.com/tdewolff/test" |
| 11 |
) |
| 12 |
|
| 13 |
//////////////////////////////////////////////////////////////// |
| 14 |
|
| 15 |
func TestParse(t *testing.T) { |
| 16 |
var parseTests = []struct { |
| 17 |
inline bool |
| 18 |
css string |
| 19 |
expected string |
| 20 |
}{ |
| 21 |
{true, " x : y ; ", "x:y;"}, |
| 22 |
{true, "color: red;", "color:red;"}, |
| 23 |
{true, "color : red;", "color:red;"}, |
| 24 |
{true, "color: red; border: 0;", "color:red;border:0;"}, |
| 25 |
{true, "color: red !important;", "color:red!important;"}, |
| 26 |
{true, "color: red ! important;", "color:red!important;"}, |
| 27 |
{true, "white-space: -moz-pre-wrap;", "white-space:-moz-pre-wrap;"}, |
| 28 |
{true, "display: -moz-inline-stack;", "display:-moz-inline-stack;"}, |
| 29 |
{true, "x: 10px / 1em;", "x:10px/1em;"}, |
| 30 |
{true, "x: 1em/1.5em \"Times New Roman\", Times, serif;", "x:1em/1.5em \"Times New Roman\",Times,serif;"}, |
| 31 |
{true, "x: hsla(100,50%, 75%, 0.5);", "x:hsla(100,50%,75%,0.5);"}, |
| 32 |
{true, "x: hsl(100,50%, 75%);", "x:hsl(100,50%,75%);"}, |
| 33 |
{true, "x: rgba(255, 238 , 221, 0.3);", "x:rgba(255,238,221,0.3);"}, |
| 34 |
{true, "x: 50vmax;", "x:50vmax;"}, |
| 35 |
{true, "color: linear-gradient(to right, black, white);", "color:linear-gradient(to right,black,white);"}, |
| 36 |
{true, "color: calc(100%/2 - 1em);", "color:calc(100%/2 - 1em);"}, |
| 37 |
{true, "color: calc(100%/2--1em);", "color:calc(100%/2--1em);"}, |
| 38 |
{false, "<!-- @charset; -->", "<!--@charset;-->"}, |
| 39 |
{false, "@layer base, typography, layout ;", "@layer base,typography,layout;"}, |
| 40 |
{false, "@layer base { }", "@layer base{}"}, |
| 41 |
{false, "@layer typography { :root { font-size: 1.618rem } }", "@layer typography{:root{font-size:1.618rem;}}"}, |
| 42 |
{false, "@layer { }", "@layer{}"}, |
| 43 |
{false, "@layer { @layer base { } }", "@layer{@layer base{}}"}, |
| 44 |
{false, "@layer { .foo { color: #fff } @layer base { } }", "@layer{.foo{color:#fff;}@layer base{}}"}, |
| 45 |
{false, "@layer { @layer base, typography ; .foo { color: #fff } @layer base { } }", "@layer{@layer base,typography;.foo{color:#fff;}@layer base{}}"}, |
| 46 |
{false, "@media print, screen { }", "@media print,screen{}"}, |
| 47 |
{false, "@media { @viewport ; }", "@media{@viewport;}"}, |
| 48 |
{false, "@keyframes 'diagonal-slide' { from { left: 0; top: 0; } to { left: 100px; top: 100px; } }", "@keyframes 'diagonal-slide'{from{left:0;top:0;}to{left:100px;top:100px;}}"}, |
| 49 |
{false, "@keyframes movingbox{0%{left:90%;}50%{left:10%;}100%{left:90%;}}", "@keyframes movingbox{0%{left:90%;}50%{left:10%;}100%{left:90%;}}"}, |
| 50 |
{false, ".foo { color: #fff;}", ".foo{color:#fff;}"}, |
| 51 |
{false, ".foo { ; _color: #fff;}", ".foo{_color:#fff;}"}, |
| 52 |
{false, "a { color: red; border: 0; }", "a{color:red;border:0;}"}, |
| 53 |
{false, "a { color: red; border: 0; } b { padding: 0; }", "a{color:red;border:0;}b{padding:0;}"}, |
| 54 |
{false, "/* comment */", "/* comment */"}, |
| 55 |
{false, "a,b,c{color:red;}", "a,b,c{color:red;}"}, |
| 56 |
|
| 57 |
// extraordinary |
| 58 |
{true, "color: red;;", "color:red;"}, |
| 59 |
{true, "margin: 10px/*comment*/50px;", "margin:10px 50px;"}, |
| 60 |
{true, "color:#c0c0c0", "color:#c0c0c0;"}, |
| 61 |
{true, "background:URL(x.png);", "background:URL(x.png);"}, |
| 62 |
{true, "filter: progid : DXImageTransform.Microsoft.BasicImage(rotation=1);", "filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=1);"}, |
| 63 |
{true, "/*a*/\n/*c*/\nkey: value;", "key:value;"}, |
| 64 |
{true, "@-moz-charset;", "@-moz-charset;"}, |
| 65 |
{true, "--custom-variable: (0;) ;", "--custom-variable: (0;) ;"}, |
| 66 |
{false, "@import;@import;", "@import;@import;"}, |
| 67 |
{false, ".a .b#c, .d<.e { x:y; }", ".a .b#c,.d<.e{x:y;}"}, |
| 68 |
{false, ".a[b~=c]d { x:y; }", ".a[b~=c]d{x:y;}"}, |
| 69 |
// {false, "{x:y;}", "{x:y;}"}, |
| 70 |
{false, "a{}", "a{}"}, |
| 71 |
{false, "a,.b/*comment*/ {x:y;}", "a,.b{x:y;}"}, |
| 72 |
{false, "a,.b/*comment*/.c {x:y;}", "a,.b.c{x:y;}"}, |
| 73 |
{false, "a{x:; z:q;}", "a{x:;z:q;}"}, |
| 74 |
{false, "@font-face { x:y; }", "@font-face{x:y;}"}, |
| 75 |
{false, "a:not([controls]){x:y;}", "a:not([controls]){x:y;}"}, |
| 76 |
{false, "@document regexp('https:.*') { p { color: red; } }", "@document regexp('https:.*'){p{color:red;}}"}, |
| 77 |
{false, "@media all and ( max-width:400px ) { }", "@media all and (max-width:400px){}"}, |
| 78 |
{false, "@media (max-width:400px) { }", "@media(max-width:400px){}"}, |
| 79 |
{false, "@media (max-width:400px)", "@media(max-width:400px);"}, |
| 80 |
{false, "@font-face { ; font:x; }", "@font-face{font:x;}"}, |
| 81 |
{false, "@-moz-font-face { ; font:x; }", "@-moz-font-face{font:x;}"}, |
| 82 |
{false, "@unknown abc { {} lala }", "@unknown abc{{} lala }"}, |
| 83 |
{false, "a[x={}]{x:y;}", "a[x={}]{x:y;}"}, |
| 84 |
{false, "a[x=,]{x:y;}", "a[x=,]{x:y;}"}, |
| 85 |
{false, "a[x=+]{x:y;}", "a[x=+]{x:y;}"}, |
| 86 |
{false, ".cla .ss > #id { x:y; }", ".cla .ss>#id{x:y;}"}, |
| 87 |
{false, ".cla /*a*/ /*b*/ .ss{}", ".cla .ss{}"}, |
| 88 |
{false, "a{x:f(a(),b);}", "a{x:f(a(),b);}"}, |
| 89 |
{false, "a{x:y!z;}", "a{x:y!z;}"}, |
| 90 |
{false, "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}", "[class*=\"column\"]+[class*=\"column\"]:last-child{a:b;}"}, |
| 91 |
{false, "@media { @viewport }", "@media{@viewport;}"}, |
| 92 |
{false, "table { @unknown }", "table{@unknown;}"}, |
| 93 |
{false, "a{@media{width:70%;} b{width:60%;}}", "a{@media{ERROR(unexpected ending in qualified rule)b{width:60%;}}"}, |
| 94 |
{false, "a{& :is(b) { }}", "a{& :is(b){}}"}, |
| 95 |
{false, "a{&:is(b) :is(c) { }}", "a{&:is(b) :is(c){}}"}, |
| 96 |
{false, "a{b: 2 ,, = 3}", "a{b:2,,=3;}"}, |
| 97 |
|
| 98 |
// early endings |
| 99 |
{false, "selector{", "selector{"}, |
| 100 |
{false, "@media{selector{", "@media{selector{"}, |
| 101 |
|
| 102 |
// bad grammar |
| 103 |
{false, "}", "ERROR(unexpected ending in qualified rule)"}, |
| 104 |
{true, "}", "ERROR(unexpected token '}' in declaration)"}, |
| 105 |
{true, "~color:red", "ERROR(expected colon in declaration)"}, |
| 106 |
{true, "(color;red)", "ERROR(unexpected token '(' in declaration)"}, |
| 107 |
{true, "color(;red)", "ERROR(unexpected token 'color(' in declaration)"}, |
| 108 |
{false, ".foo { *color: #fff;}", ".foo{*color:#fff;}"}, |
| 109 |
{true, "*color: red; font-size: 12pt;", "*color:red;font-size:12pt;"}, |
| 110 |
{true, "*--custom: red;", "*--custom: red;"}, |
| 111 |
{true, "_color: red; font-size: 12pt;", "_color:red;font-size:12pt;"}, |
| 112 |
{false, ".foo { baddecl } .bar { color:red; }", ".foo{ERROR(expected colon in declaration)}.bar{color:red;}"}, |
| 113 |
{false, ".foo { baddecl baddecl baddecl; height:100px } .bar { color:red; }", ".foo{ERROR(expected colon in declaration)height:100px;}.bar{color:red;}"}, |
| 114 |
{false, ".foo { visibility: hidden;” } .bar { color:red; }", ".foo{visibility:hidden;ERROR(expected colon in declaration)}.bar{color:red;}"}, |
| 115 |
{false, ".foo { baddecl (; color:red; }", ".foo{ERROR(expected colon in declaration)"}, |
| 116 |
{true, "color=blue;", "ERROR(expected colon in declaration)"}, |
| 117 |
|
| 118 |
// issues |
| 119 |
{false, "@media print {.class{width:5px;}}", "@media print{.class{width:5px;}}"}, // #6 |
| 120 |
{false, ".class{width:calc((50% + 2em)/2 + 14px);}", ".class{width:calc((50% + 2em)/2 + 14px);}"}, // #7 |
| 121 |
{false, ".class [c=y]{}", ".class [c=y]{}"}, // tdewolff/minify#16 |
| 122 |
{false, "table{font-family:Verdana}", "table{font-family:Verdana;}"}, // tdewolff/minify#22 |
| 123 |
|
| 124 |
// go-fuzz |
| 125 |
{false, "@-webkit-", "@-webkit-;"}, |
| 126 |
} |
| 127 |
for _, tt := range parseTests { |
| 128 |
t.Run(tt.css, func(t *testing.T) { |
| 129 |
var output strings.Builder |
| 130 |
p := NewParser(parse.NewInputString(tt.css), tt.inline) |
| 131 |
for { |
| 132 |
grammar, _, data := p.Next() |
| 133 |
data = parse.Copy(data) |
| 134 |
if grammar == ErrorGrammar { |
| 135 |
if err := p.Err(); err != io.EOF { |
| 136 |
data = []byte(fmt.Sprintf("ERROR(%v)", p.err)) |
| 137 |
} else { |
| 138 |
break |
| 139 |
} |
| 140 |
} else if grammar == AtRuleGrammar || grammar == BeginAtRuleGrammar || grammar == QualifiedRuleGrammar || grammar == BeginRulesetGrammar || grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { |
| 141 |
if grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { |
| 142 |
data = append(data, ":"...) |
| 143 |
} |
| 144 |
for _, val := range p.Values() { |
| 145 |
data = append(data, val.Data...) |
| 146 |
} |
| 147 |
if grammar == BeginAtRuleGrammar || grammar == BeginRulesetGrammar { |
| 148 |
data = append(data, "{"...) |
| 149 |
} else if grammar == AtRuleGrammar || grammar == DeclarationGrammar || grammar == CustomPropertyGrammar { |
| 150 |
data = append(data, ";"...) |
| 151 |
} else if grammar == QualifiedRuleGrammar { |
| 152 |
data = append(data, ","...) |
| 153 |
} |
| 154 |
} |
| 155 |
output.WriteString(string(data)) |
| 156 |
} |
| 157 |
test.String(t, output.String(), tt.expected) |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
// coverage |
| 162 |
for i := 0; ; i++ { |
| 163 |
if GrammarType(i).String() == fmt.Sprintf("Invalid(%d)", i) { |
| 164 |
break |
| 165 |
} |
| 166 |
} |
| 167 |
test.T(t, Token{IdentToken, []byte("data")}.String(), "Ident('data')") |
| 168 |
} |
| 169 |
|
| 170 |
func TestParseError(t *testing.T) { |
| 171 |
var parseErrorTests = []struct { |
| 172 |
inline bool |
| 173 |
css string |
| 174 |
col int |
| 175 |
}{ |
| 176 |
{false, "}", 2}, |
| 177 |
{true, "}", 1}, |
| 178 |
{false, "selector", 9}, |
| 179 |
{true, "color 0", 7}, |
| 180 |
{true, "--color 0", 9}, |
| 181 |
{true, "--custom-variable:0", 0}, |
| 182 |
} |
| 183 |
for _, tt := range parseErrorTests { |
| 184 |
t.Run(tt.css, func(t *testing.T) { |
| 185 |
p := NewParser(parse.NewInputString(tt.css), tt.inline) |
| 186 |
for { |
| 187 |
grammar, _, _ := p.Next() |
| 188 |
if grammar == ErrorGrammar { |
| 189 |
if tt.col == 0 { |
| 190 |
test.T(t, p.Err(), io.EOF) |
| 191 |
} else if perr, ok := p.Err().(*parse.Error); ok { |
| 192 |
test.That(t, p.HasParseError()) |
| 193 |
_, col, _ := perr.Position() |
| 194 |
test.T(t, col, tt.col) |
| 195 |
} else { |
| 196 |
test.Fail(t, "bad error:", p.Err()) |
| 197 |
} |
| 198 |
break |
| 199 |
} |
| 200 |
} |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func TestParseOffset(t *testing.T) { |
| 206 |
z := parse.NewInputString(`div{background:url(link);}`) |
| 207 |
p := NewParser(z, false) |
| 208 |
test.T(t, z.Offset(), 0) |
| 209 |
_, _, _ = p.Next() |
| 210 |
test.T(t, z.Offset(), 4) // div{ |
| 211 |
_, _, _ = p.Next() |
| 212 |
test.T(t, z.Offset(), 25) // background:url(link); |
| 213 |
_, _, _ = p.Next() |
| 214 |
test.T(t, z.Offset(), 26) // } |
| 215 |
} |
| 216 |
|
| 217 |
//////////////////////////////////////////////////////////////// |
| 218 |
|
| 219 |
type Obj struct{} |
| 220 |
|
| 221 |
func (*Obj) F() {} |
| 222 |
|
| 223 |
var f1 func(*Obj) |
| 224 |
|
| 225 |
func BenchmarkFuncPtr(b *testing.B) { |
| 226 |
for i := 0; i < b.N; i++ { |
| 227 |
f1 = (*Obj).F |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
var f2 func() |
| 232 |
|
| 233 |
func BenchmarkMemFuncPtr(b *testing.B) { |
| 234 |
obj := &Obj{} |
| 235 |
for i := 0; i < b.N; i++ { |
| 236 |
f2 = obj.F |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
func ExampleNewParser() { |
| 241 |
p := NewParser(parse.NewInputString("color: red;"), true) // false because this is the content of an inline style attribute |
| 242 |
var out strings.Builder |
| 243 |
for { |
| 244 |
gt, _, data := p.Next() |
| 245 |
if gt == ErrorGrammar { |
| 246 |
break |
| 247 |
} else if gt == AtRuleGrammar || gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar || gt == DeclarationGrammar { |
| 248 |
out.WriteString(string(data)) |
| 249 |
if gt == DeclarationGrammar { |
| 250 |
out.WriteString(":") |
| 251 |
} |
| 252 |
for _, val := range p.Values() { |
| 253 |
out.WriteString(string(val.Data)) |
| 254 |
} |
| 255 |
if gt == BeginAtRuleGrammar || gt == BeginRulesetGrammar { |
| 256 |
out.WriteString("{") |
| 257 |
} else if gt == AtRuleGrammar || gt == DeclarationGrammar { |
| 258 |
out.WriteString(";") |
| 259 |
} |
| 260 |
} else { |
| 261 |
out.WriteString(string(data)) |
| 262 |
} |
| 263 |
} |
| 264 |
fmt.Println(out.String()) |
| 265 |
// Output: color:red; |
| 266 |
} |