lobzique 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/Cargo.lock +520 -0
- data/Cargo.toml +11 -0
- data/LICENSE-NOKOGIRI.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/crates/lobzique-core/Cargo.toml +14 -0
- data/crates/lobzique-core/src/css/mod.rs +127 -0
- data/crates/lobzique-core/src/css/parser.rs +805 -0
- data/crates/lobzique-core/src/css/tables.rs +282 -0
- data/crates/lobzique-core/src/css/tests.rs +476 -0
- data/crates/lobzique-core/src/css/tokenizer.rs +215 -0
- data/crates/lobzique-core/src/css/translate.rs +806 -0
- data/crates/lobzique-core/src/css/write.rs +370 -0
- data/crates/lobzique-core/src/dom/attribute.rs +144 -0
- data/crates/lobzique-core/src/dom/check.rs +160 -0
- data/crates/lobzique-core/src/dom/copy.rs +229 -0
- data/crates/lobzique-core/src/dom/mod.rs +298 -0
- data/crates/lobzique-core/src/dom/namespace.rs +134 -0
- data/crates/lobzique-core/src/dom/node.rs +129 -0
- data/crates/lobzique-core/src/dom/path.rs +116 -0
- data/crates/lobzique-core/src/dom/reparent.rs +308 -0
- data/crates/lobzique-core/src/dom/tests.rs +1057 -0
- data/crates/lobzique-core/src/dom/text.rs +159 -0
- data/crates/lobzique-core/src/dom/tree.rs +446 -0
- data/crates/lobzique-core/src/html5/mod.rs +28 -0
- data/crates/lobzique-core/src/html5/parse.rs +838 -0
- data/crates/lobzique-core/src/html5/serialize.rs +181 -0
- data/crates/lobzique-core/src/html5/tests.rs +548 -0
- data/crates/lobzique-core/src/lib.rs +12 -0
- data/crates/lobzique-core/src/work.rs +26 -0
- data/crates/lobzique-core/src/xml/mod.rs +8 -0
- data/crates/lobzique-core/src/xml/serialize.rs +681 -0
- data/crates/lobzique-core/src/xml/tests.rs +174 -0
- data/crates/lobzique-core/src/xpath/ast.rs +415 -0
- data/crates/lobzique-core/src/xpath/chars.rs +139 -0
- data/crates/lobzique-core/src/xpath/eval.rs +1305 -0
- data/crates/lobzique-core/src/xpath/mod.rs +118 -0
- data/crates/lobzique-core/src/xpath/parse.rs +729 -0
- data/crates/lobzique-core/src/xpath/tests.rs +921 -0
- data/ext/lobzique/Cargo.toml +22 -0
- data/ext/lobzique/build.rs +5 -0
- data/ext/lobzique/clippy.toml +15 -0
- data/ext/lobzique/extconf.rb +6 -0
- data/ext/lobzique/src/css/mod.rs +9 -0
- data/ext/lobzique/src/css/parser.rs +44 -0
- data/ext/lobzique/src/css/selector.rs +101 -0
- data/ext/lobzique/src/html4/document.rs +31 -0
- data/ext/lobzique/src/html4/mod.rs +7 -0
- data/ext/lobzique/src/html5/mod.rs +16 -0
- data/ext/lobzique/src/html5ever.rs +278 -0
- data/ext/lobzique/src/lib.rs +45 -0
- data/ext/lobzique/src/support.rs +156 -0
- data/ext/lobzique/src/xml/attr.rs +27 -0
- data/ext/lobzique/src/xml/cdata.rs +33 -0
- data/ext/lobzique/src/xml/comment.rs +34 -0
- data/ext/lobzique/src/xml/document.rs +199 -0
- data/ext/lobzique/src/xml/document_fragment.rs +27 -0
- data/ext/lobzique/src/xml/dtd.rs +37 -0
- data/ext/lobzique/src/xml/mod.rs +44 -0
- data/ext/lobzique/src/xml/namespace.rs +56 -0
- data/ext/lobzique/src/xml/node.rs +944 -0
- data/ext/lobzique/src/xml/node_set.rs +20 -0
- data/ext/lobzique/src/xml/syntax_error.rs +33 -0
- data/ext/lobzique/src/xml/text.rs +44 -0
- data/ext/lobzique/src/xml/xpath_context.rs +305 -0
- data/lib/lobzique/action_text.rb +29 -0
- data/lib/lobzique/class_resolver.rb +33 -0
- data/lib/lobzique/css/selector_cache.rb +36 -0
- data/lib/lobzique/css/syntax_error.rb +12 -0
- data/lib/lobzique/css/xpath_visitor.rb +60 -0
- data/lib/lobzique/css.rb +57 -0
- data/lib/lobzique/extension.rb +31 -0
- data/lib/lobzique/html.rb +14 -0
- data/lib/lobzique/html4/document.rb +235 -0
- data/lib/lobzique/html4/document_fragment.rb +75 -0
- data/lib/lobzique/html4/encoding_reader.rb +374 -0
- data/lib/lobzique/html4.rb +35 -0
- data/lib/lobzique/html5/document.rb +106 -0
- data/lib/lobzique/html5/document_fragment.rb +113 -0
- data/lib/lobzique/html5/node.rb +66 -0
- data/lib/lobzique/html5.rb +92 -0
- data/lib/lobzique/html5ever.rb +14 -0
- data/lib/lobzique/rails_dom_testing.rb +95 -0
- data/lib/lobzique/sanitizer/concerns.rb +110 -0
- data/lib/lobzique/sanitizer/document.rb +14 -0
- data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
- data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
- data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
- data/lib/lobzique/sanitizer.rb +96 -0
- data/lib/lobzique/syntax_error.rb +8 -0
- data/lib/lobzique/version.rb +6 -0
- data/lib/lobzique/xml/attr.rb +23 -0
- data/lib/lobzique/xml/cdata.rb +13 -0
- data/lib/lobzique/xml/character_data.rb +11 -0
- data/lib/lobzique/xml/document.rb +149 -0
- data/lib/lobzique/xml/document_fragment.rb +109 -0
- data/lib/lobzique/xml/dtd.rb +28 -0
- data/lib/lobzique/xml/namespace.rb +19 -0
- data/lib/lobzique/xml/node/save_options.rb +52 -0
- data/lib/lobzique/xml/node.rb +629 -0
- data/lib/lobzique/xml/node_set.rb +329 -0
- data/lib/lobzique/xml/parse_options.rb +89 -0
- data/lib/lobzique/xml/pp/character_data.rb +22 -0
- data/lib/lobzique/xml/pp/node.rb +73 -0
- data/lib/lobzique/xml/pp.rb +6 -0
- data/lib/lobzique/xml/searchable.rb +123 -0
- data/lib/lobzique/xml/syntax_error.rb +77 -0
- data/lib/lobzique/xml/text.rb +14 -0
- data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
- data/lib/lobzique/xml/xpath.rb +16 -0
- data/lib/lobzique/xml/xpath_context.rb +27 -0
- data/lib/lobzique/xml.rb +22 -0
- data/lib/lobzique.rb +15 -0
- metadata +177 -0
|
@@ -0,0 +1,805 @@
|
|
|
1
|
+
//! Nokogiri's CSS parser: the racc parse loop over tables generated
|
|
2
|
+
//! from Nokogiri's grammar (`tables.rs`). The grammar actions (parser.y) build
|
|
3
|
+
//! a typed tree, not `Nokogiri::CSS::Node`s. Chains such as `a b c`
|
|
4
|
+
//! and `.a.b.c` stay flat, although the grammar makes them right-recursive.
|
|
5
|
+
//! Long selectors then use no extra stack.
|
|
6
|
+
|
|
7
|
+
use super::tables::{self as t, N};
|
|
8
|
+
use super::tokenizer::{Token, Tokenizer};
|
|
9
|
+
use super::SyntaxError;
|
|
10
|
+
|
|
11
|
+
/// Maximum nesting of parentheses. It limits the stack that translation,
|
|
12
|
+
/// evaluation and drop of the tree use: about 4 KB a level in a release build.
|
|
13
|
+
/// Nokogiri has no limit.
|
|
14
|
+
pub const MAX_NESTING: usize = 32;
|
|
15
|
+
|
|
16
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
17
|
+
pub enum Combinator {
|
|
18
|
+
Child,
|
|
19
|
+
Descendant,
|
|
20
|
+
DirectAdjacent,
|
|
21
|
+
Following,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
#[derive(Debug)]
|
|
25
|
+
pub struct Selector {
|
|
26
|
+
/// A combinator with nothing before it: `> p`.
|
|
27
|
+
pub leading: Option<Combinator>,
|
|
28
|
+
pub first: Simple,
|
|
29
|
+
pub rest: Vec<(Combinator, Simple)>,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#[derive(Debug)]
|
|
33
|
+
pub enum Simple {
|
|
34
|
+
/// `p.a`, or `.a` with an implied `*`.
|
|
35
|
+
Element {
|
|
36
|
+
name: ElementName,
|
|
37
|
+
conditions: Vec<Condition>,
|
|
38
|
+
},
|
|
39
|
+
/// `text()`, `comment()`, `has(…)`. Has at most one condition.
|
|
40
|
+
Function {
|
|
41
|
+
function: Function,
|
|
42
|
+
condition: Option<Condition>,
|
|
43
|
+
},
|
|
44
|
+
Attribute(AttributeName),
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#[derive(Debug)]
|
|
48
|
+
pub struct ElementName {
|
|
49
|
+
/// `None` for `name`, `Some(None)` for `|name`, `Some(Some(ns))` for
|
|
50
|
+
/// `ns|name`. Unescaped.
|
|
51
|
+
pub prefix: Option<Option<String>>,
|
|
52
|
+
/// `*` for the universal selector. Unescaped, so `*` means only that.
|
|
53
|
+
pub local: String,
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// Unescaped. `[|name]` has no prefix.
|
|
57
|
+
#[derive(Debug)]
|
|
58
|
+
pub struct AttributeName {
|
|
59
|
+
pub prefix: Option<String>,
|
|
60
|
+
pub local: String,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#[derive(Debug)]
|
|
64
|
+
pub enum Condition {
|
|
65
|
+
/// The `#id` token, unescaped, with the `#`.
|
|
66
|
+
Id(String),
|
|
67
|
+
Class(String),
|
|
68
|
+
Attribute {
|
|
69
|
+
subject: AttributeSubject,
|
|
70
|
+
test: Option<(AttributeOperator, String)>,
|
|
71
|
+
},
|
|
72
|
+
Pseudo(Pseudo),
|
|
73
|
+
Not(Negation),
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#[derive(Debug)]
|
|
77
|
+
pub enum AttributeSubject {
|
|
78
|
+
Name(AttributeName),
|
|
79
|
+
Function(Function),
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
83
|
+
pub enum AttributeOperator {
|
|
84
|
+
Equal,
|
|
85
|
+
PrefixMatch,
|
|
86
|
+
SuffixMatch,
|
|
87
|
+
SubstringMatch,
|
|
88
|
+
NotEqual,
|
|
89
|
+
Includes,
|
|
90
|
+
DashMatch,
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
#[derive(Debug)]
|
|
94
|
+
pub enum Pseudo {
|
|
95
|
+
Ident(String),
|
|
96
|
+
Function(Function),
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/// The grammar drops the conditions after an element name: `:not(a.b)`
|
|
100
|
+
/// is `:not(a)`.
|
|
101
|
+
#[derive(Debug)]
|
|
102
|
+
pub enum Negation {
|
|
103
|
+
Element(ElementName),
|
|
104
|
+
Conditions(Vec<Condition>),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
#[derive(Debug)]
|
|
108
|
+
pub struct Function {
|
|
109
|
+
/// The name with its `(`, as the visitor matches it, for example
|
|
110
|
+
/// `nth-child(`.
|
|
111
|
+
pub name: String,
|
|
112
|
+
pub arguments: Vec<Argument>,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#[derive(Debug)]
|
|
116
|
+
pub enum Argument {
|
|
117
|
+
/// Tokens as written, with escapes and quotes. The visitor pastes them
|
|
118
|
+
/// into XPath, or quotes the unescaped text of a string.
|
|
119
|
+
Number(String),
|
|
120
|
+
String(String),
|
|
121
|
+
Ident(String),
|
|
122
|
+
Nth(Nth),
|
|
123
|
+
Attribute(AttributeName),
|
|
124
|
+
Selector(Selector),
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/// `an+b` as the four strings of Nokogiri's `NTH` node: `a`, `n`, the operator
|
|
128
|
+
/// and `b`.
|
|
129
|
+
#[derive(Debug)]
|
|
130
|
+
pub struct Nth {
|
|
131
|
+
pub a: String,
|
|
132
|
+
pub operator: String,
|
|
133
|
+
pub b: String,
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
enum Value<'a> {
|
|
137
|
+
Nil,
|
|
138
|
+
Token(&'a str),
|
|
139
|
+
Combinator(Combinator),
|
|
140
|
+
Selectors(Vec<Selector>),
|
|
141
|
+
/// `simple_selector_1toN`. The rest is in reverse order.
|
|
142
|
+
Chain(Simple, Vec<(Combinator, Simple)>),
|
|
143
|
+
Simple(Simple),
|
|
144
|
+
ElementName(ElementName),
|
|
145
|
+
AttributeName(AttributeName),
|
|
146
|
+
Function(Function),
|
|
147
|
+
/// `hcap_1toN`, in reverse order.
|
|
148
|
+
Conditions(Vec<Condition>),
|
|
149
|
+
Condition(Condition),
|
|
150
|
+
AttributeTest(AttributeOperator, String),
|
|
151
|
+
Operator(AttributeOperator),
|
|
152
|
+
Arguments(Vec<Argument>),
|
|
153
|
+
Nth(Nth),
|
|
154
|
+
Negation(Negation),
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
pub fn parse(input: &str) -> Result<Vec<Selector>, SyntaxError> {
|
|
158
|
+
let mut tokens = Tokenizer::new(input);
|
|
159
|
+
let mut states: Vec<i16> = vec![0];
|
|
160
|
+
let mut values: Vec<(Value<'_>, &str)> = Vec::new();
|
|
161
|
+
let mut lookahead: Option<Token<'_>> = None;
|
|
162
|
+
let mut read_next = true;
|
|
163
|
+
let mut at_end = false;
|
|
164
|
+
let mut nesting = 0;
|
|
165
|
+
loop {
|
|
166
|
+
let state = *states.last().expect("racc never pops the start state");
|
|
167
|
+
let pointer = t::ACTION_POINTER[index(state)];
|
|
168
|
+
let action = if pointer == N {
|
|
169
|
+
t::ACTION_DEFAULT[index(state)]
|
|
170
|
+
} else {
|
|
171
|
+
if read_next && !at_end {
|
|
172
|
+
lookahead = tokens.next();
|
|
173
|
+
at_end = lookahead.is_none();
|
|
174
|
+
read_next = false;
|
|
175
|
+
match lookahead.map(|token| token.kind) {
|
|
176
|
+
Some(t::FUNCTION | t::HAS | t::NOT) => nesting += 1,
|
|
177
|
+
Some(t::RPAREN) => nesting = usize::saturating_sub(nesting, 1),
|
|
178
|
+
_ => {}
|
|
179
|
+
}
|
|
180
|
+
if nesting > MAX_NESTING {
|
|
181
|
+
return Err(SyntaxError(format!(
|
|
182
|
+
"selector nests more than {MAX_NESTING} parentheses deep"
|
|
183
|
+
)));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
let kind = lookahead.map_or(t::END, |token| token.kind);
|
|
187
|
+
lookup(&t::ACTION_TABLE, &t::ACTION_CHECK, pointer + kind, state)
|
|
188
|
+
.unwrap_or(t::ACTION_DEFAULT[index(state)])
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
if action > 0 && action < t::SHIFT_N {
|
|
192
|
+
let text = lookahead.map_or("$", |token| token.text);
|
|
193
|
+
values.push((Value::Token(text), text));
|
|
194
|
+
states.push(action);
|
|
195
|
+
read_next = true;
|
|
196
|
+
} else if action < 0 && action > -t::REDUCE_N {
|
|
197
|
+
let rule = index(-action);
|
|
198
|
+
let (length, lhs) = t::RULES[rule];
|
|
199
|
+
let start = values.len() - usize::from(length);
|
|
200
|
+
let popped: Vec<_> = values.drain(start..).collect();
|
|
201
|
+
states.truncate(states.len() - usize::from(length));
|
|
202
|
+
let span = span(input, popped.iter().map(|(_, text)| *text));
|
|
203
|
+
let value = reduce(rule, popped.into_iter().map(|(value, _)| value).collect())?;
|
|
204
|
+
values.push((value, span));
|
|
205
|
+
let top = *states.last().expect("racc never pops the start state");
|
|
206
|
+
let nonterminal = lhs - t::NT_BASE;
|
|
207
|
+
let pointer = t::GOTO_POINTER[index(nonterminal)];
|
|
208
|
+
let next = (pointer != N)
|
|
209
|
+
.then(|| lookup(&t::GOTO_TABLE, &t::GOTO_CHECK, pointer + top, nonterminal))
|
|
210
|
+
.flatten()
|
|
211
|
+
.unwrap_or(t::GOTO_DEFAULT[index(nonterminal)]);
|
|
212
|
+
states.push(next);
|
|
213
|
+
} else if action == t::SHIFT_N {
|
|
214
|
+
return match values.into_iter().next() {
|
|
215
|
+
Some((Value::Selectors(selectors), _)) => Ok(selectors),
|
|
216
|
+
_ => unreachable!("the start rule reduces to a selector list"),
|
|
217
|
+
};
|
|
218
|
+
} else {
|
|
219
|
+
// Same message as Nokogiri's on_error.
|
|
220
|
+
let unexpected = lookahead.map_or("$", |token| token.text);
|
|
221
|
+
let after = values
|
|
222
|
+
.iter()
|
|
223
|
+
.rev()
|
|
224
|
+
.find_map(|(value, text)| match value {
|
|
225
|
+
Value::Nil => None,
|
|
226
|
+
Value::Combinator(combinator) => Some(combinator.symbol()),
|
|
227
|
+
_ => Some(*text),
|
|
228
|
+
})
|
|
229
|
+
.unwrap_or("");
|
|
230
|
+
return Err(SyntaxError(format!(
|
|
231
|
+
"unexpected '{unexpected}' after '{after}'"
|
|
232
|
+
)));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
fn index(value: i16) -> usize {
|
|
238
|
+
usize::try_from(value).expect("racc indexes are positive")
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
fn lookup(table: &[i16], check: &[i16], i: i16, owner: i16) -> Option<i16> {
|
|
242
|
+
let i = usize::try_from(i).ok()?;
|
|
243
|
+
let value = *table.get(i)?;
|
|
244
|
+
(value != N && check.get(i) == Some(&owner)).then_some(value)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
fn span<'a>(input: &'a str, mut parts: impl Iterator<Item = &'a str>) -> &'a str {
|
|
248
|
+
let offset = |part: &str| part.as_ptr() as usize - input.as_ptr() as usize;
|
|
249
|
+
let parts: Vec<&str> = parts
|
|
250
|
+
.by_ref()
|
|
251
|
+
.filter(|part| !part.is_empty() && *part != "$")
|
|
252
|
+
.collect();
|
|
253
|
+
match (parts.first(), parts.last()) {
|
|
254
|
+
(Some(first), Some(last)) => &input[offset(first)..offset(last) + last.len()],
|
|
255
|
+
_ => "",
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
impl Combinator {
|
|
260
|
+
fn symbol(self) -> &'static str {
|
|
261
|
+
match self {
|
|
262
|
+
Combinator::Child => "CHILD_SELECTOR",
|
|
263
|
+
Combinator::Descendant => "DESCENDANT_SELECTOR",
|
|
264
|
+
Combinator::DirectAdjacent => "DIRECT_ADJACENT_SELECTOR",
|
|
265
|
+
Combinator::Following => "FOLLOWING_SELECTOR",
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/// Numbers of the rules that `reduce` handles. They are the racc numbers
|
|
271
|
+
/// of parser.y. A test checks them against `tables::GRAMMAR`.
|
|
272
|
+
#[cfg(test)]
|
|
273
|
+
pub const REDUCED_RULES: [&str; 82] = [
|
|
274
|
+
"",
|
|
275
|
+
"selector: selector COMMA simple_selector_1toN",
|
|
276
|
+
"selector: prefixless_combinator_selector",
|
|
277
|
+
"selector: optional_S simple_selector_1toN",
|
|
278
|
+
"combinator: PLUS",
|
|
279
|
+
"combinator: GREATER",
|
|
280
|
+
"combinator: TILDE",
|
|
281
|
+
"combinator: DOUBLESLASH",
|
|
282
|
+
"combinator: SLASH",
|
|
283
|
+
r#"xpath_attribute_name: "@" IDENT"#,
|
|
284
|
+
"xpath_attribute: xpath_attribute_name",
|
|
285
|
+
"simple_selector: element_name hcap_0toN",
|
|
286
|
+
"simple_selector: function",
|
|
287
|
+
"simple_selector: function pseudo",
|
|
288
|
+
"simple_selector: function attrib",
|
|
289
|
+
"simple_selector: hcap_1toN",
|
|
290
|
+
"simple_selector: xpath_attribute",
|
|
291
|
+
"prefixless_combinator_selector: combinator simple_selector_1toN",
|
|
292
|
+
"simple_selector_1toN: simple_selector combinator simple_selector_1toN",
|
|
293
|
+
"simple_selector_1toN: simple_selector S simple_selector_1toN",
|
|
294
|
+
"simple_selector_1toN: simple_selector",
|
|
295
|
+
r#"class: "." IDENT"#,
|
|
296
|
+
"element_name: namespaced_ident",
|
|
297
|
+
r#"element_name: "*""#,
|
|
298
|
+
r#"namespaced_ident: namespace "|" IDENT"#,
|
|
299
|
+
"namespaced_ident: IDENT",
|
|
300
|
+
"namespace: IDENT",
|
|
301
|
+
"namespace:",
|
|
302
|
+
"attrib: LSQUARE attrib_name attrib_val_0or1 RSQUARE",
|
|
303
|
+
"attrib: LSQUARE function attrib_val_0or1 RSQUARE",
|
|
304
|
+
"attrib: LSQUARE NUMBER RSQUARE",
|
|
305
|
+
r#"attrib_name: namespace "|" IDENT"#,
|
|
306
|
+
"attrib_name: IDENT",
|
|
307
|
+
"attrib_name: xpath_attribute",
|
|
308
|
+
"function: FUNCTION RPAREN",
|
|
309
|
+
"function: FUNCTION expr RPAREN",
|
|
310
|
+
"function: FUNCTION nth RPAREN",
|
|
311
|
+
"function: NOT expr RPAREN",
|
|
312
|
+
"function: HAS selector RPAREN",
|
|
313
|
+
"expr: NUMBER COMMA expr",
|
|
314
|
+
"expr: STRING COMMA expr",
|
|
315
|
+
"expr: IDENT COMMA expr",
|
|
316
|
+
"expr: xpath_attribute COMMA expr",
|
|
317
|
+
"expr: NUMBER",
|
|
318
|
+
"expr: STRING",
|
|
319
|
+
"expr: IDENT",
|
|
320
|
+
"expr: xpath_attribute",
|
|
321
|
+
"nth: NUMBER IDENT PLUS NUMBER",
|
|
322
|
+
"nth: IDENT PLUS NUMBER",
|
|
323
|
+
"nth: NUMBER IDENT",
|
|
324
|
+
r#"pseudo: ":" function"#,
|
|
325
|
+
r#"pseudo: ":" IDENT"#,
|
|
326
|
+
"hcap_0toN: hcap_1toN",
|
|
327
|
+
"hcap_0toN:",
|
|
328
|
+
"hcap_1toN: attribute_id hcap_1toN",
|
|
329
|
+
"hcap_1toN: class hcap_1toN",
|
|
330
|
+
"hcap_1toN: attrib hcap_1toN",
|
|
331
|
+
"hcap_1toN: pseudo hcap_1toN",
|
|
332
|
+
"hcap_1toN: negation hcap_1toN",
|
|
333
|
+
"hcap_1toN: attribute_id",
|
|
334
|
+
"hcap_1toN: class",
|
|
335
|
+
"hcap_1toN: attrib",
|
|
336
|
+
"hcap_1toN: pseudo",
|
|
337
|
+
"hcap_1toN: negation",
|
|
338
|
+
"attribute_id: HASH",
|
|
339
|
+
"attrib_val_0or1: eql_incl_dash IDENT",
|
|
340
|
+
"attrib_val_0or1: eql_incl_dash STRING",
|
|
341
|
+
"attrib_val_0or1: eql_incl_dash NUMBER",
|
|
342
|
+
"attrib_val_0or1:",
|
|
343
|
+
"eql_incl_dash: EQUAL",
|
|
344
|
+
"eql_incl_dash: PREFIXMATCH",
|
|
345
|
+
"eql_incl_dash: SUFFIXMATCH",
|
|
346
|
+
"eql_incl_dash: SUBSTRINGMATCH",
|
|
347
|
+
"eql_incl_dash: NOT_EQUAL",
|
|
348
|
+
"eql_incl_dash: INCLUDES",
|
|
349
|
+
"eql_incl_dash: DASHMATCH",
|
|
350
|
+
"negation: NOT negation_arg RPAREN",
|
|
351
|
+
"negation_arg: element_name",
|
|
352
|
+
"negation_arg: element_name hcap_1toN",
|
|
353
|
+
"negation_arg: hcap_1toN",
|
|
354
|
+
"optional_S: S",
|
|
355
|
+
"optional_S:",
|
|
356
|
+
];
|
|
357
|
+
|
|
358
|
+
/// Runs the action of grammar rule `rule` (numbered as in parser.y) on the
|
|
359
|
+
/// values of its right-hand side.
|
|
360
|
+
fn reduce(rule: usize, values: Vec<Value<'_>>) -> Result<Value<'_>, SyntaxError> {
|
|
361
|
+
use Value as V;
|
|
362
|
+
let mut values = values.into_iter();
|
|
363
|
+
let mut next = || values.next().expect("racc pops the rule's length");
|
|
364
|
+
Ok(match rule {
|
|
365
|
+
// selector: selector COMMA simple_selector_1toN
|
|
366
|
+
1 => {
|
|
367
|
+
let (V::Selectors(mut selectors), _, chain) = (next(), next(), next()) else {
|
|
368
|
+
unreachable!()
|
|
369
|
+
};
|
|
370
|
+
selectors.push(selector(None, chain));
|
|
371
|
+
V::Selectors(selectors)
|
|
372
|
+
}
|
|
373
|
+
// | prefixless_combinator_selector
|
|
374
|
+
2 => match next() {
|
|
375
|
+
V::Selectors(selectors) => V::Selectors(selectors),
|
|
376
|
+
_ => unreachable!(),
|
|
377
|
+
},
|
|
378
|
+
// | optional_S simple_selector_1toN
|
|
379
|
+
3 => {
|
|
380
|
+
let (_, chain) = (next(), next());
|
|
381
|
+
V::Selectors(vec![selector(None, chain)])
|
|
382
|
+
}
|
|
383
|
+
// combinator: PLUS | GREATER | TILDE | DOUBLESLASH | SLASH
|
|
384
|
+
4 => V::Combinator(Combinator::DirectAdjacent),
|
|
385
|
+
5 | 8 => V::Combinator(Combinator::Child),
|
|
386
|
+
6 => V::Combinator(Combinator::Following),
|
|
387
|
+
7 => V::Combinator(Combinator::Descendant),
|
|
388
|
+
// xpath_attribute_name: '@' IDENT
|
|
389
|
+
9 => {
|
|
390
|
+
next();
|
|
391
|
+
next()
|
|
392
|
+
}
|
|
393
|
+
// xpath_attribute: xpath_attribute_name
|
|
394
|
+
10 => V::AttributeName(AttributeName {
|
|
395
|
+
prefix: None,
|
|
396
|
+
local: unescape_identifier(token(next())),
|
|
397
|
+
}),
|
|
398
|
+
// simple_selector: element_name hcap_0toN
|
|
399
|
+
11 => {
|
|
400
|
+
let (V::ElementName(name), conditions) = (next(), next()) else {
|
|
401
|
+
unreachable!()
|
|
402
|
+
};
|
|
403
|
+
let conditions = match conditions {
|
|
404
|
+
V::Conditions(reversed) => reversed.into_iter().rev().collect(),
|
|
405
|
+
_ => Vec::new(),
|
|
406
|
+
};
|
|
407
|
+
V::Simple(Simple::Element { name, conditions })
|
|
408
|
+
}
|
|
409
|
+
// | function
|
|
410
|
+
12 => V::Simple(Simple::Function {
|
|
411
|
+
function: function(next()),
|
|
412
|
+
condition: None,
|
|
413
|
+
}),
|
|
414
|
+
// | function pseudo | function attrib
|
|
415
|
+
13 | 14 => {
|
|
416
|
+
let function = function(next());
|
|
417
|
+
let V::Condition(condition) = next() else {
|
|
418
|
+
unreachable!()
|
|
419
|
+
};
|
|
420
|
+
V::Simple(Simple::Function {
|
|
421
|
+
function,
|
|
422
|
+
condition: Some(condition),
|
|
423
|
+
})
|
|
424
|
+
}
|
|
425
|
+
// | hcap_1toN
|
|
426
|
+
15 => V::Simple(Simple::Element {
|
|
427
|
+
name: ElementName {
|
|
428
|
+
prefix: None,
|
|
429
|
+
local: "*".into(),
|
|
430
|
+
},
|
|
431
|
+
conditions: conditions(next()),
|
|
432
|
+
}),
|
|
433
|
+
// | xpath_attribute
|
|
434
|
+
16 => match next() {
|
|
435
|
+
V::AttributeName(name) => V::Simple(Simple::Attribute(name)),
|
|
436
|
+
_ => unreachable!(),
|
|
437
|
+
},
|
|
438
|
+
// prefixless_combinator_selector: combinator simple_selector_1toN
|
|
439
|
+
17 => {
|
|
440
|
+
let (V::Combinator(combinator), chain) = (next(), next()) else {
|
|
441
|
+
unreachable!()
|
|
442
|
+
};
|
|
443
|
+
V::Selectors(vec![selector(Some(combinator), chain)])
|
|
444
|
+
}
|
|
445
|
+
// simple_selector_1toN: simple_selector combinator simple_selector_1toN
|
|
446
|
+
// | simple_selector S simple_selector_1toN
|
|
447
|
+
18 | 19 => {
|
|
448
|
+
let (V::Simple(simple), combinator, chain) = (next(), next(), next()) else {
|
|
449
|
+
unreachable!()
|
|
450
|
+
};
|
|
451
|
+
let combinator = match combinator {
|
|
452
|
+
V::Combinator(combinator) => combinator,
|
|
453
|
+
_ => Combinator::Descendant,
|
|
454
|
+
};
|
|
455
|
+
let V::Chain(head, mut rest) = chain else {
|
|
456
|
+
unreachable!()
|
|
457
|
+
};
|
|
458
|
+
rest.push((combinator, head));
|
|
459
|
+
V::Chain(simple, rest)
|
|
460
|
+
}
|
|
461
|
+
// | simple_selector
|
|
462
|
+
20 => match next() {
|
|
463
|
+
V::Simple(simple) => V::Chain(simple, Vec::new()),
|
|
464
|
+
_ => unreachable!(),
|
|
465
|
+
},
|
|
466
|
+
// class: '.' IDENT
|
|
467
|
+
21 => {
|
|
468
|
+
next();
|
|
469
|
+
V::Condition(Condition::Class(unescape_identifier(token(next()))))
|
|
470
|
+
}
|
|
471
|
+
// element_name: namespaced_ident | '*'
|
|
472
|
+
22 => next(),
|
|
473
|
+
23 => V::ElementName(ElementName {
|
|
474
|
+
prefix: None,
|
|
475
|
+
local: "*".into(),
|
|
476
|
+
}),
|
|
477
|
+
// namespaced_ident: namespace '|' IDENT
|
|
478
|
+
24 => {
|
|
479
|
+
let (namespace, _, local) = (next(), next(), next());
|
|
480
|
+
let prefix = optional_token(namespace)
|
|
481
|
+
.map(|prefix| unescape_element_name(&prefix))
|
|
482
|
+
.transpose()?;
|
|
483
|
+
V::ElementName(ElementName {
|
|
484
|
+
prefix: Some(prefix),
|
|
485
|
+
local: unescape_element_name(token(local))?,
|
|
486
|
+
})
|
|
487
|
+
}
|
|
488
|
+
// | IDENT
|
|
489
|
+
25 => V::ElementName(ElementName {
|
|
490
|
+
prefix: None,
|
|
491
|
+
local: unescape_element_name(token(next()))?,
|
|
492
|
+
}),
|
|
493
|
+
// namespace: IDENT | (empty)
|
|
494
|
+
26 => next(),
|
|
495
|
+
27 => V::Nil,
|
|
496
|
+
// attrib: LSQUARE attrib_name attrib_val_0or1 RSQUARE
|
|
497
|
+
// | LSQUARE function attrib_val_0or1 RSQUARE
|
|
498
|
+
28 | 29 => {
|
|
499
|
+
let (_, subject, test, _) = (next(), next(), next(), next());
|
|
500
|
+
let subject = match subject {
|
|
501
|
+
V::AttributeName(name) => AttributeSubject::Name(name),
|
|
502
|
+
function => AttributeSubject::Function(self::function(function)),
|
|
503
|
+
};
|
|
504
|
+
let test = match test {
|
|
505
|
+
V::AttributeTest(operator, value) => Some((operator, value)),
|
|
506
|
+
_ => None,
|
|
507
|
+
};
|
|
508
|
+
V::Condition(Condition::Attribute { subject, test })
|
|
509
|
+
}
|
|
510
|
+
// | LSQUARE NUMBER RSQUARE
|
|
511
|
+
30 => {
|
|
512
|
+
let (_, number, _) = (next(), next(), next());
|
|
513
|
+
V::Condition(Condition::Pseudo(Pseudo::Function(Function {
|
|
514
|
+
name: "nth-child(".into(),
|
|
515
|
+
arguments: vec![Argument::Number(token(number).into())],
|
|
516
|
+
})))
|
|
517
|
+
}
|
|
518
|
+
// attrib_name: namespace '|' IDENT
|
|
519
|
+
31 => {
|
|
520
|
+
let (namespace, _, local) = (next(), next(), next());
|
|
521
|
+
V::AttributeName(AttributeName {
|
|
522
|
+
prefix: optional_token(namespace).map(|prefix| unescape_identifier(&prefix)),
|
|
523
|
+
local: unescape_identifier(token(local)),
|
|
524
|
+
})
|
|
525
|
+
}
|
|
526
|
+
// | IDENT
|
|
527
|
+
32 => V::AttributeName(AttributeName {
|
|
528
|
+
prefix: None,
|
|
529
|
+
local: unescape_identifier(token(next())),
|
|
530
|
+
}),
|
|
531
|
+
// | xpath_attribute
|
|
532
|
+
33 => next(),
|
|
533
|
+
// function: FUNCTION RPAREN
|
|
534
|
+
34 => V::Function(Function {
|
|
535
|
+
name: ruby_strip(token(next())).into(),
|
|
536
|
+
arguments: Vec::new(),
|
|
537
|
+
}),
|
|
538
|
+
// | FUNCTION expr RPAREN | FUNCTION nth RPAREN | NOT expr RPAREN
|
|
539
|
+
// | HAS selector RPAREN
|
|
540
|
+
35..=38 => {
|
|
541
|
+
let (name, arguments) = (next(), next());
|
|
542
|
+
let arguments = match arguments {
|
|
543
|
+
V::Arguments(arguments) => arguments,
|
|
544
|
+
V::Nth(nth) => vec![Argument::Nth(nth)],
|
|
545
|
+
V::Selectors(selectors) => selectors.into_iter().map(Argument::Selector).collect(),
|
|
546
|
+
_ => unreachable!(),
|
|
547
|
+
};
|
|
548
|
+
V::Function(Function {
|
|
549
|
+
name: ruby_strip(token(name)).into(),
|
|
550
|
+
arguments,
|
|
551
|
+
})
|
|
552
|
+
}
|
|
553
|
+
// expr: NUMBER COMMA expr | STRING COMMA expr | IDENT COMMA expr
|
|
554
|
+
// | xpath_attribute COMMA expr
|
|
555
|
+
39..=42 => {
|
|
556
|
+
let (first, _, rest) = (next(), next(), next());
|
|
557
|
+
let mut arguments = vec![match (rule, first) {
|
|
558
|
+
(39, first) => Argument::Number(token(first).into()),
|
|
559
|
+
(40, first) => Argument::String(token(first).into()),
|
|
560
|
+
(41, first) => Argument::Ident(token(first).into()),
|
|
561
|
+
(_, V::AttributeName(name)) => Argument::Attribute(name),
|
|
562
|
+
_ => unreachable!(),
|
|
563
|
+
}];
|
|
564
|
+
match rest {
|
|
565
|
+
V::Arguments(rest) => arguments.extend(rest),
|
|
566
|
+
V::Nth(nth) => arguments.push(Argument::Nth(nth)),
|
|
567
|
+
_ => unreachable!(),
|
|
568
|
+
}
|
|
569
|
+
V::Arguments(arguments)
|
|
570
|
+
}
|
|
571
|
+
// | NUMBER | STRING
|
|
572
|
+
43 => V::Arguments(vec![Argument::Number(token(next()).into())]),
|
|
573
|
+
44 => V::Arguments(vec![Argument::String(token(next()).into())]),
|
|
574
|
+
// | IDENT
|
|
575
|
+
45 => {
|
|
576
|
+
let ident = token(next());
|
|
577
|
+
match ident {
|
|
578
|
+
"even" => nth("2", "+", "0"),
|
|
579
|
+
"odd" => nth("2", "+", "1"),
|
|
580
|
+
"n" => nth("1", "+", "0"),
|
|
581
|
+
_ => V::Arguments(vec![Argument::Ident(ident.into())]),
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
// | xpath_attribute
|
|
585
|
+
46 => match next() {
|
|
586
|
+
V::AttributeName(name) => V::Arguments(vec![Argument::Attribute(name)]),
|
|
587
|
+
_ => unreachable!(),
|
|
588
|
+
},
|
|
589
|
+
// nth: NUMBER IDENT PLUS NUMBER
|
|
590
|
+
47 => {
|
|
591
|
+
let (a, n, plus, b) = (token(next()), token(next()), token(next()), token(next()));
|
|
592
|
+
if n != "n" {
|
|
593
|
+
return Err(parse_error(n));
|
|
594
|
+
}
|
|
595
|
+
nth(a, plus, b)
|
|
596
|
+
}
|
|
597
|
+
// | IDENT PLUS NUMBER
|
|
598
|
+
48 => {
|
|
599
|
+
let (n, plus, b) = (token(next()), token(next()), token(next()));
|
|
600
|
+
match n {
|
|
601
|
+
"n" => nth("1", plus, b),
|
|
602
|
+
"-n" => nth("-1", plus, b),
|
|
603
|
+
// Nokogiri reports the operator, not the identifier.
|
|
604
|
+
_ => return Err(parse_error(plus)),
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
// | NUMBER IDENT
|
|
608
|
+
49 => {
|
|
609
|
+
let (a, n) = (token(next()), token(next()));
|
|
610
|
+
match n.strip_prefix("n-") {
|
|
611
|
+
Some(b) => nth(a, "-", b),
|
|
612
|
+
None if n == "n" => nth(a, "+", "0"),
|
|
613
|
+
None => return Err(parse_error(n)),
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
// pseudo: ':' function | ':' IDENT
|
|
617
|
+
50 => {
|
|
618
|
+
next();
|
|
619
|
+
V::Condition(Condition::Pseudo(Pseudo::Function(function(next()))))
|
|
620
|
+
}
|
|
621
|
+
51 => {
|
|
622
|
+
next();
|
|
623
|
+
V::Condition(Condition::Pseudo(Pseudo::Ident(token(next()).into())))
|
|
624
|
+
}
|
|
625
|
+
// hcap_0toN: hcap_1toN | (empty)
|
|
626
|
+
52 => next(),
|
|
627
|
+
53 => V::Nil,
|
|
628
|
+
// hcap_1toN: (attribute_id | class | attrib | pseudo | negation) hcap_1toN
|
|
629
|
+
54..=58 => {
|
|
630
|
+
let (V::Condition(condition), V::Conditions(mut reversed)) = (next(), next()) else {
|
|
631
|
+
unreachable!()
|
|
632
|
+
};
|
|
633
|
+
reversed.push(condition);
|
|
634
|
+
V::Conditions(reversed)
|
|
635
|
+
}
|
|
636
|
+
// | attribute_id | class | attrib | pseudo | negation
|
|
637
|
+
59..=63 => match next() {
|
|
638
|
+
V::Condition(condition) => V::Conditions(vec![condition]),
|
|
639
|
+
_ => unreachable!(),
|
|
640
|
+
},
|
|
641
|
+
// attribute_id: HASH
|
|
642
|
+
64 => V::Condition(Condition::Id(unescape_identifier(token(next())))),
|
|
643
|
+
// attrib_val_0or1: eql_incl_dash (IDENT | STRING | NUMBER) | (empty)
|
|
644
|
+
65..=67 => {
|
|
645
|
+
let (V::Operator(operator), value) = (next(), token(next())) else {
|
|
646
|
+
unreachable!()
|
|
647
|
+
};
|
|
648
|
+
let value = match rule {
|
|
649
|
+
65 => unescape_identifier(value),
|
|
650
|
+
66 => unescape_string(&value[1..value.len() - 1]),
|
|
651
|
+
_ => value.into(),
|
|
652
|
+
};
|
|
653
|
+
V::AttributeTest(operator, value)
|
|
654
|
+
}
|
|
655
|
+
68 => V::Nil,
|
|
656
|
+
// eql_incl_dash
|
|
657
|
+
69 => V::Operator(AttributeOperator::Equal),
|
|
658
|
+
70 => V::Operator(AttributeOperator::PrefixMatch),
|
|
659
|
+
71 => V::Operator(AttributeOperator::SuffixMatch),
|
|
660
|
+
72 => V::Operator(AttributeOperator::SubstringMatch),
|
|
661
|
+
73 => V::Operator(AttributeOperator::NotEqual),
|
|
662
|
+
74 => V::Operator(AttributeOperator::Includes),
|
|
663
|
+
75 => V::Operator(AttributeOperator::DashMatch),
|
|
664
|
+
// negation: NOT negation_arg RPAREN
|
|
665
|
+
76 => {
|
|
666
|
+
let (_, V::Negation(negation), _) = (next(), next(), next()) else {
|
|
667
|
+
unreachable!()
|
|
668
|
+
};
|
|
669
|
+
V::Condition(Condition::Not(negation))
|
|
670
|
+
}
|
|
671
|
+
// negation_arg: element_name | element_name hcap_1toN | hcap_1toN
|
|
672
|
+
77 | 78 => match next() {
|
|
673
|
+
V::ElementName(name) => V::Negation(Negation::Element(name)),
|
|
674
|
+
_ => unreachable!(),
|
|
675
|
+
},
|
|
676
|
+
79 => V::Negation(Negation::Conditions(conditions(next()))),
|
|
677
|
+
// optional_S: S | (empty)
|
|
678
|
+
80 => next(),
|
|
679
|
+
81 => V::Nil,
|
|
680
|
+
_ => unreachable!("parser.y has 81 rules"),
|
|
681
|
+
})
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
fn token<'a>(value: Value<'a>) -> &'a str {
|
|
685
|
+
match value {
|
|
686
|
+
Value::Token(text) => text,
|
|
687
|
+
_ => unreachable!("expected a token"),
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
fn optional_token(value: Value<'_>) -> Option<String> {
|
|
692
|
+
match value {
|
|
693
|
+
Value::Token(text) => Some(text.into()),
|
|
694
|
+
_ => None,
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
fn function(value: Value<'_>) -> Function {
|
|
699
|
+
match value {
|
|
700
|
+
Value::Function(function) => function,
|
|
701
|
+
_ => unreachable!("expected a function"),
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
fn conditions(value: Value<'_>) -> Vec<Condition> {
|
|
706
|
+
match value {
|
|
707
|
+
Value::Conditions(reversed) => reversed.into_iter().rev().collect(),
|
|
708
|
+
_ => unreachable!("expected conditions"),
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
fn selector(leading: Option<Combinator>, chain: Value<'_>) -> Selector {
|
|
713
|
+
let Value::Chain(first, mut rest) = chain else {
|
|
714
|
+
unreachable!("expected a chain")
|
|
715
|
+
};
|
|
716
|
+
rest.reverse();
|
|
717
|
+
Selector {
|
|
718
|
+
leading,
|
|
719
|
+
first,
|
|
720
|
+
rest,
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
fn nth<'a>(a: &str, operator: &str, b: &str) -> Value<'a> {
|
|
725
|
+
Value::Nth(Nth {
|
|
726
|
+
a: a.into(),
|
|
727
|
+
operator: operator.into(),
|
|
728
|
+
b: b.into(),
|
|
729
|
+
})
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
/// Nokogiri raises Racc::ParseError here. It is not a CSS::SyntaxError.
|
|
733
|
+
fn parse_error(text: &str) -> SyntaxError {
|
|
734
|
+
SyntaxError(format!("parse error on IDENT '{text}'"))
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
fn ruby_strip(text: &str) -> &str {
|
|
738
|
+
text.trim_matches(|c: char| c == '\0' || c.is_ascii_whitespace() || c == '\x0b')
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
pub fn unescape_identifier(text: &str) -> String {
|
|
742
|
+
unescape(text, false)
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/// Input is a string without its quotes.
|
|
746
|
+
pub fn unescape_string(text: &str) -> String {
|
|
747
|
+
unescape(text, true)
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
/// A name that decodes to `*` would become the universal selector.
|
|
751
|
+
fn unescape_element_name(text: &str) -> Result<String, SyntaxError> {
|
|
752
|
+
let name = unescape_identifier(text);
|
|
753
|
+
if name == "*" {
|
|
754
|
+
return Err(SyntaxError(format!("Invalid element name '{text}'")));
|
|
755
|
+
}
|
|
756
|
+
Ok(name)
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/// Port of Nokogiri's `gsub(/\\(?:([^0-9a-fA-F])|([0-9a-fA-F]{1,6})(?:\r\n|[
|
|
760
|
+
/// \t\n\r\f])?)/)`. A hex escape has up to six digits and one optional
|
|
761
|
+
/// whitespace. Other escapes give the escaped character. In strings, an escaped
|
|
762
|
+
/// newline is dropped.
|
|
763
|
+
fn unescape(text: &str, in_string: bool) -> String {
|
|
764
|
+
let mut out = String::with_capacity(text.len());
|
|
765
|
+
let mut rest = text;
|
|
766
|
+
while let Some(backslash) = rest.find('\\') {
|
|
767
|
+
out.push_str(&rest[..backslash]);
|
|
768
|
+
let after = &rest[backslash + 1..];
|
|
769
|
+
let Some(c) = after.chars().next() else {
|
|
770
|
+
out.push('\\');
|
|
771
|
+
rest = after;
|
|
772
|
+
break;
|
|
773
|
+
};
|
|
774
|
+
if c.is_ascii_hexdigit() {
|
|
775
|
+
let digits = after
|
|
776
|
+
.bytes()
|
|
777
|
+
.take(6)
|
|
778
|
+
.take_while(u8::is_ascii_hexdigit)
|
|
779
|
+
.count();
|
|
780
|
+
let code = u32::from_str_radix(&after[..digits], 16).expect("hex digits");
|
|
781
|
+
let c = char::from_u32(code)
|
|
782
|
+
.filter(|&c| c != '\0')
|
|
783
|
+
.unwrap_or('\u{FFFD}');
|
|
784
|
+
out.push(c);
|
|
785
|
+
let tail = &after[digits..];
|
|
786
|
+
let space = if tail.starts_with("\r\n") {
|
|
787
|
+
2
|
|
788
|
+
} else if tail.starts_with([' ', '\t', '\n', '\r', '\x0c']) {
|
|
789
|
+
1
|
|
790
|
+
} else {
|
|
791
|
+
0
|
|
792
|
+
};
|
|
793
|
+
rest = &tail[space..];
|
|
794
|
+
} else if in_string && after.starts_with("\r\n") {
|
|
795
|
+
rest = &after[2..];
|
|
796
|
+
} else {
|
|
797
|
+
if !(in_string && matches!(c, '\n' | '\r' | '\x0c')) {
|
|
798
|
+
out.push(c);
|
|
799
|
+
}
|
|
800
|
+
rest = &after[c.len_utf8()..];
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
out.push_str(rest);
|
|
804
|
+
out
|
|
805
|
+
}
|