-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathstylesheet_test.go
More file actions
105 lines (102 loc) · 2.74 KB
/
stylesheet_test.go
File metadata and controls
105 lines (102 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2017 The Go Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
package safehtml
import (
"fmt"
"testing"
)
func TestCSSRule(t *testing.T) {
for _, test := range [...]struct {
selector string
style Style
want, err string
}{
{
`#id`, StyleFromConstant(`top:0;left:0;`),
`#id{top:0;left:0;}`, ``,
},
{
`.class`, StyleFromConstant(`margin-left:5px;`),
`.class{margin-left:5px;}`, ``,
},
{
`tag #id, .class`, StyleFromConstant(`color:black !important;`),
`tag #id, .class{color:black !important;}`, ``,
},
{
`[title='son\'s']`, Style{},
`[title='son\'s']{}`, ``,
},
{
`[title="{"]`, Style{},
`[title="{"]{}`, ``,
},
{
`:nth-child(1)`, Style{},
`:nth-child(1){}`, ``,
},
{
`tag{color:black;}`, Style{},
``, `selector "tag{color:black;}" contains "{", which is disallowed outside of CSS strings`,
},
{
`]`, Style{},
``, `selector "]" contains unbalanced () or [] brackets`,
},
{
`[title`, Style{},
``, `selector "[title" contains unbalanced () or [] brackets`,
},
{
`[foo)bar]`, Style{},
``, `selector "[foo)bar]" contains unbalanced () or [] brackets`,
},
{
`[foo[bar]`, Style{},
``, `selector "[foo[bar]" contains unbalanced () or [] brackets`,
},
{
`foo(bar(baz)`, Style{},
``, `selector "foo(bar(baz)" contains unbalanced () or [] brackets`,
},
{
`:nth-child(1`, Style{},
``, `selector ":nth-child(1" contains unbalanced () or [] brackets`,
},
{
`[type="a]`, Style{},
``, `selector "[type=\"a]" contains "\"", which is disallowed outside of CSS strings`,
},
{
`[type=\'a]`, Style{},
``, `selector "[type=\\'a]" contains "\\", which is disallowed outside of CSS strings`,
},
{
`<`, Style{},
``, `selector "<" contains '<'`,
},
{
`@import "foo";#id`, Style{},
``, `selector "@import \"foo\";#id" contains "@", which is disallowed outside of CSS strings`,
},
{
`/* `, Style{},
``, `selector "/* " contains "/", which is disallowed outside of CSS strings`,
},
} {
errPrefix := fmt.Sprintf("CSSRule(%q, %#v)", test.selector, test.style)
ss, err := CSSRule(test.selector, test.style)
if test.want != "" && err != nil {
t.Errorf("%s returned unexpected error: %s", errPrefix, err)
} else if test.want != "" && ss.String() != test.want {
t.Errorf("%s = %q, want: %q", errPrefix, ss.String(), test.want)
} else if test.want == "" && err == nil {
t.Errorf("%s expected error", errPrefix)
} else if test.want == "" && err.Error() != test.err {
t.Errorf("%s returned error:\n\t%s,\nwant:\n\t%q", errPrefix, err, test.want)
}
}
}