forked from gorilla/css
-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
375 lines (342 loc) · 8.64 KB
/
token.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright 2012 The Gorilla Authors, Copyright 2015 Barracuda Networks.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scanner
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"io"
"strings"
"unicode/utf8"
)
// Type is an integer that identifies the type of the token. Only the types
// defined as variables in the package may be used.
type Type struct {
t int
}
// String returns a string representation of the token type.
func (t Type) String() string {
return tokenNames[t]
}
// GoString returns a string representation of the token type.
func (t Type) GoString() string {
return tokenNames[t]
}
// Token represents a token and the corresponding string.
type Token struct {
Type Type
Value string
Line int
Column int
}
// String returns a string representation of the token.
func (t *Token) String() string {
if len(t.Value) > 10 {
return fmt.Sprintf("%s (line: %d, column: %d): %.10q...",
t.Type, t.Line, t.Column, t.Value)
}
return fmt.Sprintf("%s (line: %d, column: %d): %q",
t.Type, t.Line, t.Column, t.Value)
}
// For those types of tokens that need to have their representation
// normalized to contain the semantic contents of the token, rather than
// the literal contents of the token, this performs that act.
func (t *Token) normalize() {
switch t.Type {
case Ident:
t.Value = unbackslash(t.Value, false)
case AtKeyword:
t.Value = unbackslash(t.Value[1:], false)
case String:
t.Value = unbackslash(t.Value[1:len(t.Value)-1], true)
case Hash:
t.Value = unbackslash(t.Value[1:], false)
case Percentage:
t.Value = t.Value[0 : len(t.Value)-1]
case Dimension:
t.Value = unbackslash(t.Value, false)
case CDO:
t.Value = ""
case CDC:
t.Value = ""
case URI:
// this is a strict parser; only u, r, l, followed by a paren with
// no whitespace, is accepted.
trimmed := strings.TrimSpace(t.Value[4 : len(t.Value)-1])
if trimmed == "" {
t.Value = ""
return
}
if len(trimmed) >= 2 {
lastIdx := len(trimmed) - 1
if trimmed[0] == '\'' && trimmed[lastIdx] == '\'' {
trimmed = trimmed[1:lastIdx]
} else if trimmed[0] == '"' && trimmed[lastIdx] == '"' {
trimmed = trimmed[1:lastIdx]
}
}
t.Value = unbackslash(trimmed, false)
case Comment:
t.Value = t.Value[2 : len(t.Value)-2]
case Function:
t.Value = unbackslash(t.Value[0:len(t.Value)-1], false)
case Includes:
t.Value = ""
case DashMatch:
t.Value = ""
case PrefixMatch:
t.Value = ""
case SuffixMatch:
t.Value = ""
case SubstringMatch:
t.Value = ""
}
}
func wr(w io.Writer, strs ...string) (err error) {
for _, str := range strs {
_, err = w.Write([]byte(str))
if err != nil {
return
}
}
return
}
// Emit will write a string representation of the given token to the target
// io.Writer. An error will be returned if you either try to emit Error or
// EOF, or if the Writer returns an error.
//
// Emit will make many small writes to the io.Writer.
//
// Emit assumes you have not set the token's .Value to an invalid value for
// many of these; for instance, if you manually take a Number token and set
// its .Value to "sometext", you will emit something that is not a number.
func (t *Token) Emit(w io.Writer) (err error) {
switch t.Type {
case Error:
return errors.New("can not emit an error token")
case EOF:
return errors.New("can not emit an EOF")
case Ident:
err = wr(w, backslashifyIdent(t.Value))
case AtKeyword:
err = wr(w, "@", backslashifyIdent(t.Value))
case String:
err = wr(w, "\"", backslashifyString(t.Value), "\"")
case Hash:
err = wr(w, "#", backslashifyIdent(t.Value))
case Number:
err = wr(w, t.Value)
case Percentage:
err = wr(w, t.Value, "%")
case Dimension:
err = wr(w, t.Value)
case URI:
err = wr(w, "url('", backslashifyString(t.Value), "')")
case UnicodeRange:
err = wr(w, t.Value)
case CDO:
err = wr(w, "<!--")
case CDC:
err = wr(w, "-->")
case S:
err = wr(w, t.Value)
case Comment:
err = wr(w, "/*", t.Value, "*/")
case Function:
err = wr(w, backslashifyIdent(t.Value), "(")
case Includes:
err = wr(w, "~=")
case DashMatch:
err = wr(w, "|=")
case PrefixMatch:
err = wr(w, "^=")
case SuffixMatch:
err = wr(w, "$=")
case SubstringMatch:
err = wr(w, "*=")
case Delim:
err = wr(w, t.Value)
case BOM:
err = wr(w, "\ufeff")
}
return
}
func unbackslash(s string, isString bool) string {
// in general, strings are short, and do not contain backslashes; if
// that is the case, just bail out with no additional allocation.
if !strings.Contains(s, "\\") {
return s
}
in := bytes.NewBufferString(s)
var out bytes.Buffer
out.Grow(len(s))
hexChars := make([]byte, 6, 6)
for {
c, err := in.ReadByte()
if err == io.EOF {
break
}
if c != '\\' {
_ = out.WriteByte(c)
continue
}
// c is now the first byte after the backslash
c, err = in.ReadByte()
if err == io.EOF {
_ = out.WriteByte('\\')
break
}
// CSS 4.1.3 third bullet point: Rules for decoding backslashes.
// We won't process comments, so we skip that for now.
// First, special string rules:
if isString {
// If this is a string token, and the next thing is a newline
// (LF or CRLF), then the whole thing didn't happen.
if c == '\n' {
continue
}
if c == '\r' {
c, err = in.ReadByte()
if err == io.EOF {
_ = out.WriteByte('\\')
break
}
if c == '\n' {
continue
} else {
// standard does not say what to do with backslash-CR
// that is not followed by a LF. Go ahead and eat the
// CR and return to normal processing.
_ = in.UnreadByte()
continue
}
}
}
// Second, any non-hex digit, CR, LF, or FF gets replaced by the
// literal character. CR, LF, or FF, if left unescaped, presumably
// didn't make it this far to be decoded. So that just leaves the
// hex digits and the not-hex-digits.
switch {
case isHexChar(c):
// A hex specification is either 0-5 digits followed by
// optional whitespace which will be eaten, or exactly six
// digits.
hexChars = hexChars[:0]
hexChars = append(hexChars, c)
HEXLOOP:
for len(hexChars) < 6 {
nextChar, err := in.ReadByte()
if err == io.EOF {
break HEXLOOP
}
switch {
case isHexChar(nextChar):
hexChars = append(hexChars, nextChar)
case isWhitespace(nextChar):
// this ends up eating the whitespace char
break HEXLOOP
default:
// Non-space chars do not get eaten
_ = in.UnreadByte()
break HEXLOOP
}
}
// The rune this represents:
r := decodeHex(hexChars)
_, _ = out.WriteRune(r)
default:
_ = out.WriteByte(c)
}
}
return out.String()
}
func backslashifyString(s string) string {
res := bytes.NewBuffer(make([]byte, 0, len(s)+32))
b := []byte(s)
for {
r, size := utf8.DecodeRune(b)
if size == 0 {
break
}
b = b[size:]
switch {
case r == '"':
_, _ = res.WriteRune('\\')
_, _ = res.WriteRune(r)
case r >= '#':
_, _ = res.WriteRune(r)
case r == '\t' || r == '!':
_, _ = res.WriteRune(r)
default:
_, _ = res.WriteRune('\\')
_, _ = res.WriteRune(r)
}
}
return res.String()
}
func backslashifyIdent(s string) string {
res := bytes.NewBuffer(make([]byte, 0, len(s)+32))
b := []byte(s)
for {
r, size := utf8.DecodeRune(b)
if size == 0 {
break
}
b = b[size:]
if !(r >= 'a' && r <= 'z') &&
!(r >= 'A' && r <= 'Z') &&
r != '_' && r != '-' &&
r <= 255 {
_, _ = res.WriteRune('\\')
// we just asserted in the if that this is <= 255, so it fits
// in a byte
_ = cssEncodeHex(res, byte(r))
} else {
_, _ = res.WriteRune(r)
}
}
return res.String()
}
func isWhitespace(c byte) bool {
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\f'
}
func isHexChar(c byte) bool {
return c >= '0' && c <= '9' ||
c >= 'a' && c <= 'f' ||
c >= 'A' && c <= 'F'
}
// fromHexChar copied from encoding/hex/hex.go, except this is guaranteed
// to only be called on hex chars, so no success flag.
func fromHexChar(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
// satisfies compiler that there is a return.
return 0
}
// as mentioned in fromHexChar, by construction, we know this is being
// called only with hex values, and only in quantities that fit into the
// rune type. C&P at your own peril. :)
func decodeHex(in []byte) rune {
val := rune(0)
for _, c := range in {
val = val << 4
val = val + rune(fromHexChar(c))
}
return val
}
func cssEncodeHex(w io.Writer, b byte) error {
h := make([]byte, 2)
hex.Encode(h, []byte{b})
_, _ = w.Write(h)
_, err := w.Write([]byte(" "))
return err
}