-
Notifications
You must be signed in to change notification settings - Fork 0
/
css_parser.py
157 lines (131 loc) · 4.44 KB
/
css_parser.py
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
from html_parser import Element
INHERITED_PROPERTIES = {
"font-size": "16px",
"font-style": "normal",
"font-weight": "normal",
"color": "black",
}
class CSSParser:
def __init__(self, s):
self.s = s
self.i = 0
def whitespace(self):
while self.i < len(self.s) and self.s[self.i].isspace():
self.i += 1
def word(self):
start = self.i
while self.i < len(self.s):
if self.s[self.i].isalnum() or self.s[self.i] in '#-.%':
self.i += 1
else:
break
assert self.i > start
return self.s[start:self.i]
def literal(self, literal):
assert self.i < len(self.s) and self.s[self.i] == literal
self.i += 1
def pair(self):
prop = self.word()
self.whitespace()
self.literal(':')
self.whitespace()
value = self.word()
return prop.lower(), value
# parse the body of a rule into key value pairs
def body(self):
pairs = {}
while self.i < len(self.s) and self.s[self.i] != '}':
try:
prop, val = self.pair()
pairs[prop.lower()] = val
self.whitespace()
self.literal(';')
self.whitespace()
except AssertionError:
why = self.ignore_until([";", "}"])
if why == ";":
self.literal(";")
self.whitespace()
else:
break
return pairs
def ignore_until(self, chars):
while self.i < len(self.s):
if self.s[self.i] in chars:
return self.s[self.i]
else:
self.i += 1
def selector(self):
out = TagSelector(self.word().lower())
self.whitespace()
while self.i < len(self.s) and self.s[self.i] != '{':
tag = self.word()
descendant = TagSelector(tag.lower())
out = DescendantSelector(out, descendant)
self.whitespace()
return out
def parse(self):
rules = []
while self.i < len(self.s):
try:
self.whitespace()
selector = self.selector()
self.literal('{')
self.whitespace()
body = self.body()
self.literal('}')
rules.append((selector, body))
except AssertionError:
why = self.ignore_until(["}"])
if why == "}":
self.literal("}")
self.whitespace()
else:
break
return rules
class TagSelector:
def __init__(self, tag):
self.tag = tag
self.priority = 1
def matches(self, node):
return isinstance(node, Element) and self.tag == node.tag
class DescendantSelector:
def __init__(self, ancestor, descendant):
self.ancestor = ancestor
self.descendant = descendant
self.priority = ancestor.priority + descendant.priority
def matches(self, node):
if not self.descendant.matches(node): return False
while node.parent:
if self.ancestor.matches(node.parent): return True
node = node.parent
return False
def style(node, rules):
node.style = {}
for prop, default_value in INHERITED_PROPERTIES.items():
if node.parent:
node.style[prop] = node.parent.style[prop]
else:
node.style[prop] = default_value
for selector, body in rules:
if not selector.matches(node): continue
for prop, value in body.items():
node.style[prop] = value
if isinstance(node, Element) and 'style' in node.attributes:
pairs = CSSParser(node.attributes['style']).body()
for prop, val in pairs.items():
node.style[prop] = val
# normalize percentage fonts to pixels
if node.style["font-size"].endswith("%"):
if node.parent:
parent_font_size = node.parent.style["font-size"]
else:
parent_font_size = INHERITED_PROPERTIES["font-size"]
node_pct = float(node.style['font-size'][:-1]) / 100
parent_px = float(parent_font_size[:-2])
node.style['font-size'] = str(node_pct * parent_px) + 'px'
for child in node.children:
style(child, rules)
def cascade_priority(rule):
selector, body = rule
return selector.priority