Repository navigation
Expand file tree
/
Copy pathcssParser.go
More file actions
129 lines (116 loc) · 2.52 KB
/
Copy pathcssParser.go
File metadata and controls
129 lines (116 loc) · 2.52 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"errors"
"fmt"
)
type cssParser struct {
string string
i int
}
func newCssParser(string string) *cssParser {
return &cssParser{
string: string,
i: 0,
}
}
func (p *cssParser) whitespace() {
for p.i < len(p.string) && p.string[p.i] == ' ' {
p.i++
}
}
func (p *cssParser) word() (string, error) {
start := p.i
for p.i < len(p.string) {
if checkAlphaNum(p.string[p.i]) || arrayContains([]string{"#", "-", ".", "%"}, string(p.string[p.i])) {
p.i++
} else {
break
}
}
if !(p.i > start) {
//panic("[CSSParser] Error parsing word")
return "", errors.New("[CSSParser] Error parsing word")
}
return p.string[start:p.i], nil
}
func (p *cssParser) literal(literal uint8) error {
if !(p.i < len(p.string) && p.string[p.i] == literal) { //FIXME: i should use rune here instead of uint8
//panic("[CSSParser] Error parsing literal")
return errors.New("[CSSParser] Error parsing literal")
}
p.i++
return nil
}
func (p *cssParser) pair() (string, string, error) {
var err error
property, err := p.word()
if err != nil {
return "", "", err
}
p.whitespace()
err = p.literal(':')
if err != nil {
return "", "", err
}
p.whitespace()
value, err := p.word()
if err != nil {
return "", "", err
}
return property, value, nil
}
func (p *cssParser) body() map[string]string {
pairs := map[string]string{}
for p.i < len(p.string) {
property, value, err := p.pair()
if err != nil {
fmt.Printf("[CSSParser] Ignoring error: %s\n", err)
why := p.ignoreUntil([]string{";"})
if why == ';' {
_ = p.literal(';') //Why tf would there be an error here???
p.whitespace()
} else {
break
}
}
pairs[property] = value
p.whitespace()
err = p.literal(';')
if err != nil {
//panic("[CSSParser] While parsing body couldn't find ';'")
println("[CSSParser] While parsing body couldn't find ';'")
}
p.whitespace()
}
return pairs
}
func (p *cssParser) ignoreUntil(chars []string) uint8 {
for p.i < len(p.string) {
if arrayContains(chars, string(p.string[p.i])) {
return p.string[p.i]
} else {
p.i++
}
}
return 0
}
func checkAlphaNum(char uint8) bool {
if (char < 'a' || char > 'z') && (char < 'A' || char > 'Z') && (char < '0' || char > '9') {
return false
}
return true
}
func style(node node) {
if node.isText() {
return
}
styleMap := node.Style()
*styleMap = map[string]string{}
value, ok := node.Attributes()["style"]
if ok {
*styleMap = newCssParser(value).body()
}
for _, child := range node.getChildren() {
style(child)
}
}