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
use crate::svgtree::{self, AId};
use crate::{converter, Node, OptionsRef, Tree};
static FEATURES: &[&str] = &[
"http://www.w3.org/TR/SVG11/feature#SVGDOM-static",
"http://www.w3.org/TR/SVG11/feature#SVG-static",
"http://www.w3.org/TR/SVG11/feature#CoreAttribute",
"http://www.w3.org/TR/SVG11/feature#Structure",
"http://www.w3.org/TR/SVG11/feature#BasicStructure",
"http://www.w3.org/TR/SVG11/feature#ContainerAttribute",
"http://www.w3.org/TR/SVG11/feature#ConditionalProcessing",
"http://www.w3.org/TR/SVG11/feature#Image",
"http://www.w3.org/TR/SVG11/feature#Style",
"http://www.w3.org/TR/SVG11/feature#Shape",
"http://www.w3.org/TR/SVG11/feature#Text",
"http://www.w3.org/TR/SVG11/feature#BasicText",
"http://www.w3.org/TR/SVG11/feature#PaintAttribute",
"http://www.w3.org/TR/SVG11/feature#BasicPaintAttribute",
"http://www.w3.org/TR/SVG11/feature#OpacityAttribute",
"http://www.w3.org/TR/SVG11/feature#GraphicsAttribute",
"http://www.w3.org/TR/SVG11/feature#BasicGraphicsAttribute",
"http://www.w3.org/TR/SVG11/feature#Marker",
"http://www.w3.org/TR/SVG11/feature#Gradient",
"http://www.w3.org/TR/SVG11/feature#Pattern",
"http://www.w3.org/TR/SVG11/feature#Clip",
"http://www.w3.org/TR/SVG11/feature#BasicClip",
"http://www.w3.org/TR/SVG11/feature#Mask",
"http://www.w3.org/TR/SVG11/feature#Filter",
"http://www.w3.org/TR/SVG11/feature#BasicFilter",
"http://www.w3.org/TR/SVG11/feature#XlinkAttribute",
];
pub(crate) fn convert(
node: svgtree::Node,
state: &converter::State,
id_generator: &mut converter::NodeIdGenerator,
parent: &mut Node,
tree: &mut Tree,
) -> Option<()> {
let child = node.children().find(|n| is_condition_passed(*n, state.opt))?;
match converter::convert_group(node, state, false, id_generator, parent, tree) {
converter::GroupKind::Create(ref mut g) => {
converter::convert_element(child, state, id_generator, g, tree);
}
converter::GroupKind::Skip => {
converter::convert_element(child, state, id_generator, parent, tree);
}
converter::GroupKind::Ignore => {}
}
Some(())
}
pub(crate) fn is_condition_passed(
node: svgtree::Node,
opt: &OptionsRef,
) -> bool {
if !node.is_element() {
return false;
}
if node.has_attribute(AId::RequiredExtensions) {
return false;
}
if let Some(features) = node.attribute::<&str>(AId::RequiredFeatures) {
for feature in features.split(' ') {
if !FEATURES.contains(&feature) {
return false;
}
}
}
if !is_valid_sys_lang(node, opt) {
return false;
}
true
}
fn is_valid_sys_lang(
node: svgtree::Node,
opt: &OptionsRef,
) -> bool {
if let Some(langs) = node.attribute::<&str>(AId::SystemLanguage) {
let mut has_match = false;
for lang in langs.split(',') {
let lang = lang.trim();
if opt.languages.iter().any(|v| v == lang) {
has_match = true;
break;
}
if let Some(idx) = lang.bytes().position(|c| c == b'-') {
let lang_prefix = &lang[..idx];
if opt.languages.iter().any(|v| v == lang_prefix) {
has_match = true;
break;
}
}
}
has_match
} else {
true
}
}