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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use simplecss;
use svgparser::{
Stream,
StrSpan,
};
use error::Result;
use {
AttributeId,
AttributeValue,
Document,
ElementId,
ErrorKind,
ErrorPos,
Node,
ParseOptions,
};
use super::parser::{
Entities,
Links,
NodeSpanData,
PostData,
};
pub fn resolve_css<'a>(
doc: &Document,
post_data: &mut PostData<'a>,
opt: &ParseOptions,
) -> Result<()> {
use simplecss::Token as CssToken;
#[derive(Clone,Copy,Debug)]
enum CssSelector<'a> {
Universal,
Type(&'a str),
Id(&'a str),
Class(&'a str),
}
fn gen_err_pos(frame: StrSpan, pos: usize) -> ErrorPos {
let mut s = Stream::from_str(frame.full_str());
s.gen_error_pos_from(pos)
}
let mut selectors: Vec<CssSelector> = Vec::new();
let mut values: Vec<(&str,&str)> = Vec::with_capacity(16);
// remember all resolved classes
let mut resolved_classes: Vec<&str> = Vec::with_capacity(16);
// we have to make a copy to allow passing mutable 'post_data'
// it's cheap, because in a 99.9% cases we have only one style
let styles = post_data.css_list.clone();
for style in &styles {
let mut tokenizer = {
let mut s = Stream::from_span(*style);
// check for a empty string
s.skip_spaces();
if s.at_end() {
// ignore such CSS
continue;
}
let text = style.full_str();
// we use 'new_bound' method to get absolute error positions
simplecss::Tokenizer::new_bound(text, style.start() + s.pos(), style.end())
};
'root: loop {
selectors.clear();
values.clear();
// get list of selectors
loop {
// remember position before next token
let last_pos = tokenizer.pos();
let token = tokenizer.parse_next()?;
match token {
CssToken::EndOfStream => {
// parsing finished
break 'root;
}
CssToken::BlockStart => {
// stop selectors parsing
break;
}
CssToken::Comma => {
// we ignore 'comma' token
continue;
}
_ => {}
}
// currently we support only simple selectors
let selector = match token {
CssToken::UniversalSelector => CssSelector::Universal,
CssToken::TypeSelector(name) => CssSelector::Type(name),
CssToken::IdSelector(name) => CssSelector::Id(name),
CssToken::ClassSelector(name) => CssSelector::Class(name),
CssToken::AttributeSelector(_)
| CssToken::PseudoClass(_)
| CssToken::LangPseudoClass(_)
| CssToken::Combinator(_) => {
return Err(ErrorKind::UnsupportedCSS(gen_err_pos(*style, last_pos)).into());
}
_ => {
return Err(ErrorKind::InvalidCSS(gen_err_pos(*style, last_pos)).into());
}
};
selectors.push(selector);
}
// get list of declarations
loop {
// remember position before next token
let last_pos = tokenizer.pos();
match tokenizer.parse_next()? {
CssToken::Declaration(name, value) => values.push((name, value)),
CssToken::BlockEnd => break,
CssToken::EndOfStream => break 'root,
_ => {
return Err(ErrorKind::InvalidCSS(gen_err_pos(*style, last_pos)).into());
}
}
}
// process selectors
for selector in &selectors {
match *selector {
CssSelector::Universal => {
for (_, mut node) in doc.descendants().svg() {
apply_css_attributes(&values, &mut node, &mut post_data.links,
&post_data.entitis, opt)?;
}
}
CssSelector::Type(name) => {
if let Some(eid) = ElementId::from_name(name) {
for (id, mut node) in doc.descendants().svg() {
if id == eid {
apply_css_attributes(&values, &mut node, &mut post_data.links,
&post_data.entitis, opt)?;
}
}
} else {
warn!("CSS styles for a non-SVG element ('{}') are ignored.", name);
}
}
CssSelector::Id(name) => {
if let Some(mut node) = doc.descendants().find(|n| *n.id() == name) {
apply_css_attributes(&values, &mut node, &mut post_data.links,
&post_data.entitis, opt)?;
}
}
CssSelector::Class(name) => {
// we use already collected list of 'class' attributes
for d in post_data.class_attrs.iter_mut().filter(|n| n.span.to_str() == name) {
apply_css_attributes(&values, &mut d.node, &mut post_data.links,
&post_data.entitis, opt)?;
resolved_classes.push(name);
}
}
}
}
}
}
postprocess_class_selector(&resolved_classes, &mut post_data.class_attrs, opt);
Ok(())
}
fn postprocess_class_selector<'a>(
resolved_classes: &[&str],
class_attrs: &mut Vec<NodeSpanData<'a>>,
opt: &ParseOptions,
) {
// remove resolved classes
class_attrs.retain(|n| !resolved_classes.contains(&n.span.to_str()));
if opt.skip_unresolved_classes {
for d in class_attrs {
warn!("Could not resolve an unknown class: {}.", d.span);
}
} else {
// create 'class' attributes with unresolved classes
for d in class_attrs {
if d.node.has_attribute(AttributeId::Class) {
let mut attrs = d.node.attributes_mut();
let class_val = attrs.get_value_mut(AttributeId::Class);
if let Some(&mut AttributeValue::String(ref mut text)) = class_val {
text.push(' ');
text.push_str(d.span.to_str());
}
} else {
d.node.set_attribute((AttributeId::Class, d.span.to_str()));
}
}
}
}
fn apply_css_attributes<'a>(
values: &[(&str, &'a str)],
node: &mut Node,
links: &mut Links<'a>,
entitis: &Entities<'a>,
opt: &ParseOptions,
) -> Result<()> {
for &(aname, avalue) in values {
match AttributeId::from_name(aname) {
Some(aid) => {
let mut parse_attr = |aid: AttributeId| {
// TODO: to a proper stream
super::parser::parse_svg_attribute_value(
node, aid, StrSpan::from_str(avalue),
links, entitis, opt
)
};
if aid == AttributeId::Marker {
// The SVG specification defines three properties to reference markers:
// `marker-start`, `marker-mid`, `marker-end`.
// It also provides a shorthand property, marker.
// Using the marker property from a style sheet
// is equivalent to using all three (start, mid, end).
// However, shorthand properties cannot be used as presentation attributes.
// So we have to convert it to presentation attributes.
parse_attr(AttributeId::MarkerStart)?;
parse_attr(AttributeId::MarkerMid)?;
parse_attr(AttributeId::MarkerEnd)?;
} else {
parse_attr(aid)?;
}
}
None => {
if opt.parse_unknown_attributes {
node.set_attribute((aname, avalue));
}
}
}
}
Ok(())
}