-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvalue.go
76 lines (68 loc) · 1.76 KB
/
value.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
package css
import (
"strings"
"github.com/gorilla/css/scanner"
)
type CSSValue struct {
Tokens []*scanner.Token
}
func NewCSSValue(csstext string) *CSSValue {
sc := scanner.New(csstext)
val := CSSValue{Tokens: make([]*scanner.Token, 0)}
Loop:
for {
token := sc.Next()
switch token.Type {
case scanner.TokenError, scanner.TokenEOF:
break Loop
default:
val.Tokens = append(val.Tokens, token)
}
}
return &val
}
func NewCSSValueString(data string) *CSSValue {
data = strings.ReplaceAll(data, `\`, `\\`)
data = strings.ReplaceAll(data, `"`, `\"`)
data = `"` + data + `"`
token := scanner.Token{scanner.TokenString, data, 0, 0}
return &CSSValue{Tokens: []*scanner.Token{&token}}
}
func (v *CSSValue) SplitOnToken(split *scanner.Token) []*CSSValue {
res := make([]*CSSValue, 0)
current := make([]*scanner.Token, 0)
for _, tok := range v.Tokens {
if tok.Type == split.Type && tok.Value == split.Value {
res = append(res, &CSSValue{Tokens: current})
current = make([]*scanner.Token, 0)
} else {
current = append(current, tok)
}
}
res = append(res, &CSSValue{Tokens: current})
return res
}
func (v *CSSValue) Text() string {
var b strings.Builder
for _, t := range v.Tokens {
b.WriteString(t.Value)
}
return strings.TrimSpace(b.String())
}
func (v *CSSValue) ParsedText() string {
var b strings.Builder
for _, t := range v.Tokens {
switch t.Type {
case scanner.TokenString:
val := t.Value[1 : len(t.Value)-1] // remove trailing / leading quotes
val = strings.ReplaceAll(val, `\"`, `"`)
val = strings.ReplaceAll(val, `\'`, `'`)
val = strings.ReplaceAll(val, `\\`, `\`)
// \A9 should be replaced by the corresponding rune
b.WriteString(val)
default:
b.WriteString(t.Value)
}
}
return strings.TrimSpace(b.String())
}