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