| 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 |
type TTs []TokenType |
| 14 |
|
| 15 |
func TestTokens(t *testing.T) { |
| 16 |
var tokenTests = []struct { |
| 17 |
css string |
| 18 |
ttypes []TokenType |
| 19 |
lexemes []string |
| 20 |
}{ |
| 21 |
{" ", TTs{}, []string{}}, |
| 22 |
{"5.2 .4", TTs{NumberToken, NumberToken}, []string{"5.2", ".4"}}, |
| 23 |
{"color: red;", TTs{IdentToken, ColonToken, IdentToken, SemicolonToken}, []string{"color", ":", "red", ";"}}, |
| 24 |
{"background: url(\"http://x\");", TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{"background", ":", `url("http://x")`, ";"}}, |
| 25 |
{"background: URL(x.png);", TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{"background", ":", "URL(x.png)", ";"}}, |
| 26 |
{"color: rgb(4, 0%, 5em);", TTs{IdentToken, ColonToken, FunctionToken, NumberToken, CommaToken, PercentageToken, CommaToken, DimensionToken, RightParenthesisToken, SemicolonToken}, []string{"color", ":", "rgb(", "4", ",", "0%", ",", "5em", ")", ";"}}, |
| 27 |
{"body { \"string\" }", TTs{IdentToken, LeftBraceToken, StringToken, RightBraceToken}, []string{"body", "{", `"string"`, "}"}}, |
| 28 |
{"body { \"str\\\"ing\" }", TTs{IdentToken, LeftBraceToken, StringToken, RightBraceToken}, []string{"body", "{", `"str\"ing"`, "}"}}, |
| 29 |
{".class { }", TTs{DelimToken, IdentToken, LeftBraceToken, RightBraceToken}, []string{".", "class", "{", "}"}}, |
| 30 |
{"#class { }", TTs{HashToken, LeftBraceToken, RightBraceToken}, []string{"#class", "{", "}"}}, |
| 31 |
{"#class\\#withhash { }", TTs{HashToken, LeftBraceToken, RightBraceToken}, []string{`#class\#withhash`, "{", "}"}}, |
| 32 |
{"@media print { }", TTs{AtKeywordToken, IdentToken, LeftBraceToken, RightBraceToken}, []string{"@media", "print", "{", "}"}}, |
| 33 |
{"/*comment*/", TTs{CommentToken}, []string{"/*comment*/"}}, |
| 34 |
{"/*com* /ment*/", TTs{CommentToken}, []string{"/*com* /ment*/"}}, |
| 35 |
{"~= |= ^= $= *=", TTs{IncludeMatchToken, DashMatchToken, PrefixMatchToken, SuffixMatchToken, SubstringMatchToken}, []string{"~=", "|=", "^=", "$=", "*="}}, |
| 36 |
{"||", TTs{ColumnToken}, []string{"||"}}, |
| 37 |
{"<!-- -->", TTs{CDOToken, CDCToken}, []string{"<!--", "-->"}}, |
| 38 |
{"U+1234", TTs{UnicodeRangeToken}, []string{"U+1234"}}, |
| 39 |
{"5.2 .4 4e-22", TTs{NumberToken, NumberToken, NumberToken}, []string{"5.2", ".4", "4e-22"}}, |
| 40 |
{"--custom-variable", TTs{CustomPropertyNameToken}, []string{"--custom-variable"}}, |
| 41 |
{"--0", TTs{CustomPropertyNameToken}, []string{"--0"}}, |
| 42 |
|
| 43 |
// unexpected ending |
| 44 |
{"ident", TTs{IdentToken}, []string{"ident"}}, |
| 45 |
{"123.", TTs{NumberToken, DelimToken}, []string{"123", "."}}, |
| 46 |
{"\"string", TTs{StringToken}, []string{`"string`}}, |
| 47 |
{"123/*comment", TTs{NumberToken, CommentToken}, []string{"123", "/*comment"}}, |
| 48 |
{"U+1-", TTs{IdentToken, NumberToken, DelimToken}, []string{"U", "+1", "-"}}, |
| 49 |
|
| 50 |
// unicode |
| 51 |
{"fooδbar", TTs{IdentToken}, []string{"fooδbar"}}, |
| 52 |
{"foo\\æ\\†", TTs{IdentToken}, []string{"foo\\æ\\†"}}, |
| 53 |
{"'foo\u554abar'", TTs{StringToken}, []string{"'foo\u554abar'"}}, |
| 54 |
{"\\000026B", TTs{IdentToken}, []string{"\\000026B"}}, |
| 55 |
{"\\26 B", TTs{IdentToken}, []string{"\\26 B"}}, |
| 56 |
|
| 57 |
// hacks |
| 58 |
{`\-\mo\z\-b\i\nd\in\g:\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss);`, TTs{IdentToken, ColonToken, URLToken, SemicolonToken}, []string{`\-\mo\z\-b\i\nd\in\g`, ":", `\url(//business\i\nfo.co.uk\/labs\/xbl\/xbl\.xml\#xss)`, ";"}}, |
| 59 |
{"width/**/:/**/ 40em;", TTs{IdentToken, CommentToken, ColonToken, CommentToken, DimensionToken, SemicolonToken}, []string{"width", "/**/", ":", "/**/", "40em", ";"}}, |
| 60 |
{":root *> #quince", TTs{ColonToken, IdentToken, DelimToken, DelimToken, HashToken}, []string{":", "root", "*", ">", "#quince"}}, |
| 61 |
{"html[xmlns*=\"\"]:root", TTs{IdentToken, LeftBracketToken, IdentToken, SubstringMatchToken, StringToken, RightBracketToken, ColonToken, IdentToken}, []string{"html", "[", "xmlns", "*=", `""`, "]", ":", "root"}}, |
| 62 |
{"body:nth-of-type(1)", TTs{IdentToken, ColonToken, FunctionToken, NumberToken, RightParenthesisToken}, []string{"body", ":", "nth-of-type(", "1", ")"}}, |
| 63 |
{"color/*\\**/: blue\\9;", TTs{IdentToken, CommentToken, ColonToken, IdentToken, SemicolonToken}, []string{"color", `/*\**/`, ":", `blue\9`, ";"}}, |
| 64 |
{"color: blue !ie;", TTs{IdentToken, ColonToken, IdentToken, DelimToken, IdentToken, SemicolonToken}, []string{"color", ":", "blue", "!", "ie", ";"}}, |
| 65 |
|
| 66 |
// escapes, null and replacement character |
| 67 |
{"c\\\x00olor: white;", TTs{IdentToken, ColonToken, IdentToken, SemicolonToken}, []string{"c\\\x00olor", ":", "white", ";"}}, |
| 68 |
{"null\\0", TTs{IdentToken}, []string{`null\0`}}, |
| 69 |
{"\\", TTs{DelimToken}, []string{"\\"}}, |
| 70 |
{"abc\\", TTs{IdentToken, DelimToken}, []string{"abc", "\\"}}, |
| 71 |
{"#\\", TTs{DelimToken, DelimToken}, []string{"#", "\\"}}, |
| 72 |
{"#abc\\", TTs{HashToken, DelimToken}, []string{"#abc", "\\"}}, |
| 73 |
{"\"abc\\", TTs{StringToken}, []string{"\"abc\\"}}, // should officially not include backslash, but no biggie |
| 74 |
{"url(abc\\", TTs{BadURLToken}, []string{"url(abc\\"}}, |
| 75 |
{"\"a\x00b\"", TTs{StringToken}, []string{"\"a\x00b\""}}, |
| 76 |
{"a\\\x00b", TTs{IdentToken}, []string{"a\\\x00b"}}, |
| 77 |
{"url(a\x00b)", TTs{BadURLToken}, []string{"url(a\x00b)"}}, // null character cannot be unquoted |
| 78 |
{"/*a\x00b*/", TTs{CommentToken}, []string{"/*a\x00b*/"}}, |
| 79 |
|
| 80 |
// coverage |
| 81 |
{" \n\r\n\r\"\\\r\n\\\r\"", TTs{StringToken}, []string{"\"\\\r\n\\\r\""}}, |
| 82 |
{"U+?????? U+ABCD?? U+ABC-DEF", TTs{UnicodeRangeToken, UnicodeRangeToken, UnicodeRangeToken}, []string{"U+??????", "U+ABCD??", "U+ABC-DEF"}}, |
| 83 |
{"U+? U+A?", TTs{UnicodeRangeToken, UnicodeRangeToken}, []string{"U+?", "U+A?"}}, |
| 84 |
{"U+ U+ABCDEF?", TTs{IdentToken, DelimToken, IdentToken, DelimToken, IdentToken, DelimToken}, []string{"U", "+", "U", "+", "ABCDEF", "?"}}, |
| 85 |
{"-5.23 -moz", TTs{NumberToken, IdentToken}, []string{"-5.23", "-moz"}}, |
| 86 |
{"()", TTs{LeftParenthesisToken, RightParenthesisToken}, []string{"(", ")"}}, |
| 87 |
{"url( //url\n )", TTs{URLToken}, []string{"url( //url\n )"}}, |
| 88 |
{"url( ", TTs{URLToken}, []string{"url( "}}, |
| 89 |
{"url( //url ", TTs{URLToken}, []string{"url( //url "}}, |
| 90 |
{"url(\")a", TTs{URLToken}, []string{"url(\")a"}}, |
| 91 |
{"url(a'\\\n)a", TTs{BadURLToken, IdentToken}, []string{"url(a'\\\n)", "a"}}, |
| 92 |
{"url(\"\n)a", TTs{BadURLToken, IdentToken}, []string{"url(\"\n)", "a"}}, |
| 93 |
{"url(a h)a", TTs{BadURLToken, IdentToken}, []string{"url(a h)", "a"}}, |
| 94 |
{"<!- | @4 ## /2", TTs{DelimToken, DelimToken, DelimToken, DelimToken, DelimToken, NumberToken, DelimToken, DelimToken, DelimToken, NumberToken}, []string{"<", "!", "-", "|", "@", "4", "#", "#", "/", "2"}}, |
| 95 |
{"\"s\\\n\"", TTs{StringToken}, []string{"\"s\\\n\""}}, |
| 96 |
{"\"a\\\"b\"", TTs{StringToken}, []string{"\"a\\\"b\""}}, |
| 97 |
{"\"s\n", TTs{BadStringToken}, []string{"\"s\n"}}, |
| 98 |
|
| 99 |
// small |
| 100 |
{"\"abcd", TTs{StringToken}, []string{"\"abcd"}}, |
| 101 |
{"/*comment", TTs{CommentToken}, []string{"/*comment"}}, |
| 102 |
{"U+A-B", TTs{UnicodeRangeToken}, []string{"U+A-B"}}, |
| 103 |
{"url((", TTs{BadURLToken}, []string{"url(("}}, |
| 104 |
{"id\u554a", TTs{IdentToken}, []string{"id\u554a"}}, |
| 105 |
} |
| 106 |
for _, tt := range tokenTests { |
| 107 |
t.Run(tt.css, func(t *testing.T) { |
| 108 |
l := NewLexer(parse.NewInputString(tt.css)) |
| 109 |
i := 0 |
| 110 |
tokens := []TokenType{} |
| 111 |
lexemes := []string{} |
| 112 |
for { |
| 113 |
token, lexeme := l.Next() |
| 114 |
if token == ErrorToken { |
| 115 |
test.T(t, l.Err(), io.EOF) |
| 116 |
break |
| 117 |
} else if token == WhitespaceToken { |
| 118 |
continue |
| 119 |
} |
| 120 |
tokens = append(tokens, token) |
| 121 |
lexemes = append(lexemes, string(lexeme)) |
| 122 |
i++ |
| 123 |
} |
| 124 |
test.T(t, tokens, tt.ttypes, "token types must match") |
| 125 |
test.T(t, lexemes, tt.lexemes, "token data must match") |
| 126 |
}) |
| 127 |
} |
| 128 |
|
| 129 |
// coverage |
| 130 |
for i := 0; ; i++ { |
| 131 |
if TokenType(i).String() == fmt.Sprintf("Invalid(%d)", i) { |
| 132 |
break |
| 133 |
} |
| 134 |
} |
| 135 |
test.T(t, NewLexer(parse.NewInputString("x")).consumeBracket(), ErrorToken, "consumeBracket on 'x' must return error") |
| 136 |
} |
| 137 |
|
| 138 |
func TestOffset(t *testing.T) { |
| 139 |
z := parse.NewInputString(`div{background:url(link);}`) |
| 140 |
l := NewLexer(z) |
| 141 |
test.T(t, z.Offset(), 0) |
| 142 |
_, _ = l.Next() |
| 143 |
test.T(t, z.Offset(), 3) // div |
| 144 |
_, _ = l.Next() |
| 145 |
test.T(t, z.Offset(), 4) // { |
| 146 |
_, _ = l.Next() |
| 147 |
test.T(t, z.Offset(), 14) // background |
| 148 |
_, _ = l.Next() |
| 149 |
test.T(t, z.Offset(), 15) // : |
| 150 |
_, _ = l.Next() |
| 151 |
test.T(t, z.Offset(), 24) // url(link) |
| 152 |
_, _ = l.Next() |
| 153 |
test.T(t, z.Offset(), 25) // ; |
| 154 |
_, _ = l.Next() |
| 155 |
test.T(t, z.Offset(), 26) // } |
| 156 |
} |
| 157 |
|
| 158 |
//////////////////////////////////////////////////////////////// |
| 159 |
|
| 160 |
func ExampleNewLexer() { |
| 161 |
l := NewLexer(parse.NewInputString("color: red;")) |
| 162 |
var out strings.Builder |
| 163 |
for { |
| 164 |
tt, data := l.Next() |
| 165 |
if tt == ErrorToken { |
| 166 |
break |
| 167 |
} else if tt == WhitespaceToken || tt == CommentToken { |
| 168 |
continue |
| 169 |
} |
| 170 |
out.WriteString(string(data)) |
| 171 |
} |
| 172 |
fmt.Println(out.String()) |
| 173 |
// Output: color:red; |
| 174 |
} |