Skip to content

Commit

Permalink
Merge pull request #16 from alin04/preprocess
Browse files Browse the repository at this point in the history
Perform all preprocessing as per the spec.
  • Loading branch information
kisielk authored Dec 10, 2018
2 parents 398b0b0 + 776dbfb commit 9c69421
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ func init() {
// New returns a new CSS scanner for the given input.
func New(input string) *Scanner {
// Normalize newlines.
// https://www.w3.org/TR/css-syntax-3/#input-preprocessing
input = strings.Replace(input, "\r\n", "\n", -1)
input = strings.Replace(input, "\r", "\n", -1)
input = strings.Replace(input, "\f", "\n", -1)
input = strings.Replace(input, "\u0000", "\ufffd", -1)
return &Scanner{
input: input,
row: 1,
Expand Down
36 changes: 36 additions & 0 deletions scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,39 @@ func TestMatchers(t *testing.T) {
checkMatch("\uFEFF", TokenBOM, "\uFEFF")
checkMatch(`╯︵┻━┻"stuff"`, TokenIdent, "╯︵┻━┻", TokenString, `"stuff"`)
}

func TestPreprocess(t *testing.T) {
tcs := []struct{ desc, input, expected string }{
{
"CR",
".a{ \r color:red}",
".a{ \n color:red}",
},
{
"FF",
".a{ \f color:red}",
".a{ \n color:red}",
},
{
"CRLF",
".a{ \r\n color:red}",
".a{ \n color:red}",
},
{
"NULL",
".a{ \u0000 color:red}",
".a{ \ufffd color:red}",
},
{
"mixture",
".a{ \r\r\n\u0000\f color:red}",
".a{ \n\n\ufffd\n color:red}",
},
}
for _, tc := range tcs {
s := New(tc.input)
if s.input != tc.expected {
t.Errorf("%s: got=%q, want=%q", tc.desc, s.input, tc.expected)
}
}
}

0 comments on commit 9c69421

Please sign in to comment.