1
2
3
4
5 package template
6
7 import (
8 "bytes"
9 "fmt"
10 "unicode"
11 "unicode/utf8"
12 )
13
14
15
16 func endsWithCSSKeyword(b []byte, kw string) bool {
17 i := len(b) - len(kw)
18 if i < 0 {
19
20 return false
21 }
22 if i != 0 {
23 r, _ := utf8.DecodeLastRune(b[:i])
24 if isCSSNmchar(r) {
25
26 return false
27 }
28 }
29
30
31
32
33
34 return string(bytes.ToLower(b[i:])) == kw
35 }
36
37
38 func isCSSNmchar(r rune) bool {
39
40
41
42 return 'a' <= r && r <= 'z' ||
43 'A' <= r && r <= 'Z' ||
44 '0' <= r && r <= '9' ||
45 r == '-' ||
46 r == '_' ||
47
48 0x80 <= r && r <= 0xd7ff ||
49 0xe000 <= r && r <= 0xfffd ||
50 0x10000 <= r && r <= 0x10ffff
51 }
52
53
54
55
56
57 func decodeCSS(s []byte) []byte {
58 i := bytes.IndexByte(s, '\\')
59 if i == -1 {
60 return s
61 }
62
63
64
65 b := make([]byte, 0, len(s))
66 for len(s) != 0 {
67 i := bytes.IndexByte(s, '\\')
68 if i == -1 {
69 i = len(s)
70 }
71 b, s = append(b, s[:i]...), s[i:]
72 if len(s) < 2 {
73 break
74 }
75
76
77 if isHex(s[1]) {
78
79
80 j := 2
81 for j < len(s) && j < 7 && isHex(s[j]) {
82 j++
83 }
84 r := hexDecode(s[1:j])
85 if r > unicode.MaxRune {
86 r, j = r/16, j-1
87 }
88 n := utf8.EncodeRune(b[len(b):cap(b)], r)
89
90
91
92 b, s = b[:len(b)+n], skipCSSSpace(s[j:])
93 } else {
94
95 _, n := utf8.DecodeRune(s[1:])
96 b, s = append(b, s[1:1+n]...), s[1+n:]
97 }
98 }
99 return b
100 }
101
102
103 func isHex(c byte) bool {
104 return '0' <= c && c <= '9' || 'a' <= c && c <= 'f' || 'A' <= c && c <= 'F'
105 }
106
107
108 func hexDecode(s []byte) rune {
109 n := '\x00'
110 for _, c := range s {
111 n <<= 4
112 switch {
113 case '0' <= c && c <= '9':
114 n |= rune(c - '0')
115 case 'a' <= c && c <= 'f':
116 n |= rune(c-'a') + 10
117 case 'A' <= c && c <= 'F':
118 n |= rune(c-'A') + 10
119 default:
120 panic(fmt.Sprintf("Bad hex digit in %q", s))
121 }
122 }
123 return n
124 }
125
126
127 func skipCSSSpace(c []byte) []byte {
128 if len(c) == 0 {
129 return c
130 }
131
132 switch c[0] {
133 case '\t', '\n', '\f', ' ':
134 return c[1:]
135 case '\r':
136
137
138
139 if len(c) >= 2 && c[1] == '\n' {
140 return c[2:]
141 }
142 return c[1:]
143 }
144 return c
145 }
146
147
148 func isCSSSpace(b byte) bool {
149 switch b {
150 case '\t', '\n', '\f', '\r', ' ':
151 return true
152 }
153 return false
154 }
155
156
157 func cssEscaper(args ...interface{}) string {
158 s, _ := stringify(args...)
159 var b bytes.Buffer
160 r, w, written := rune(0), 0, 0
161 for i := 0; i < len(s); i += w {
162
163 r, w = utf8.DecodeRuneInString(s[i:])
164 var repl string
165 switch {
166 case int(r) < len(cssReplacementTable) && cssReplacementTable[r] != "":
167 repl = cssReplacementTable[r]
168 default:
169 continue
170 }
171 b.WriteString(s[written:i])
172 b.WriteString(repl)
173 written = i + w
174 if repl != `\\` && (written == len(s) || isHex(s[written]) || isCSSSpace(s[written])) {
175 b.WriteByte(' ')
176 }
177 }
178 if written == 0 {
179 return s
180 }
181 b.WriteString(s[written:])
182 return b.String()
183 }
184
185 var cssReplacementTable = []string{
186 0: `\0`,
187 '\t': `\9`,
188 '\n': `\a`,
189 '\f': `\c`,
190 '\r': `\d`,
191
192
193 '"': `\22`,
194 '&': `\26`,
195 '\'': `\27`,
196 '(': `\28`,
197 ')': `\29`,
198 '+': `\2b`,
199 '/': `\2f`,
200 ':': `\3a`,
201 ';': `\3b`,
202 '<': `\3c`,
203 '>': `\3e`,
204 '\\': `\\`,
205 '{': `\7b`,
206 '}': `\7d`,
207 }
208
209 var expressionBytes = []byte("expression")
210 var mozBindingBytes = []byte("mozbinding")
211
212
213
214
215
216
217 func cssValueFilter(args ...interface{}) string {
218 s, t := stringify(args...)
219 if t == contentTypeCSS {
220 return s
221 }
222 b, id := decodeCSS([]byte(s)), make([]byte, 0, 64)
223
224
225
226
227
228
229
230
231
232
233
234
235 for i, c := range b {
236 switch c {
237 case 0, '"', '\'', '(', ')', '/', ';', '@', '[', '\\', ']', '`', '{', '}':
238 return filterFailsafe
239 case '-':
240
241
242 if i != 0 && b[i-1] == '-' {
243 return filterFailsafe
244 }
245 default:
246 if c < utf8.RuneSelf && isCSSNmchar(rune(c)) {
247 id = append(id, c)
248 }
249 }
250 }
251 id = bytes.ToLower(id)
252 if bytes.Contains(id, expressionBytes) || bytes.Contains(id, mozBindingBytes) {
253 return filterFailsafe
254 }
255 return string(b)
256 }
257
View as plain text