Skip to content

Commit 76b2678

Browse files
committed
Parse CDATA comments as data nodes
1 parent 55dfb8b commit 76b2678

File tree

1 file changed

+54
-13
lines changed

1 file changed

+54
-13
lines changed

native/meeseeks_html5ever_nif/src/flat_dom.rs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,24 @@ impl Parent {
3232
}
3333
}
3434

35+
#[derive(Debug)]
36+
enum ScriptOrStyle {
37+
Script,
38+
Style,
39+
Neither,
40+
}
41+
42+
#[derive(Debug)]
43+
enum DataType {
44+
Script,
45+
Style,
46+
Cdata,
47+
}
48+
3549
#[derive(Debug)]
3650
enum NodeEnum {
3751
Comment(StrTendril),
38-
Data(StrTendril),
52+
Data(DataType, StrTendril),
3953
Doctype(StrTendril, StrTendril, StrTendril),
4054
Document,
4155
Element(QualName, Vec<Attribute>),
@@ -44,23 +58,22 @@ enum NodeEnum {
4458
}
4559

4660
impl NodeEnum {
47-
fn script_or_style(&self) -> bool {
61+
fn script_or_style(&self) -> ScriptOrStyle {
4862
match *self {
4963
Element(ref name, ..) => {
5064
match name.expanded() {
51-
expanded_name!(html "script") |
52-
expanded_name!(html "style") =>
53-
true,
54-
_ => false,
65+
expanded_name!(html "script") => ScriptOrStyle::Script,
66+
expanded_name!(html "style") => ScriptOrStyle::Style,
67+
_ => ScriptOrStyle::Neither,
5568
}
5669
},
57-
_ => false,
70+
_ => ScriptOrStyle::Neither,
5871
}
5972
}
6073

6174
fn append_text(&mut self, text: &str) -> bool {
6275
match *self {
63-
Text(ref mut current) | Data(ref mut current) => {
76+
Text(ref mut current) | Data(_, ref mut current) => {
6477
current.push_slice(text);
6578
true
6679
},
@@ -128,9 +141,13 @@ impl FlatDom {
128141
_ => unreachable!(),
129142
};
130143
} else {
131-
let child = if self.node(parent).node.script_or_style() {
132-
self.add_node(Data(text))} else {
133-
self.add_node(Text(text))
144+
let child = match self.node(parent).node.script_or_style() {
145+
ScriptOrStyle::Script =>
146+
self.add_node(Data(DataType::Script, text)),
147+
ScriptOrStyle::Style =>
148+
self.add_node(Data(DataType::Style, text)),
149+
ScriptOrStyle::Neither =>
150+
self.add_node(Text(text)),
134151
};
135152
self.node_mut(child).parent = Parent::Some(parent);
136153
let parent_node = self.node_mut(parent);
@@ -208,7 +225,12 @@ impl TreeSink for FlatDom {
208225
}
209226

210227
fn create_comment(&mut self, text: StrTendril) -> Self::Handle {
211-
self.add_node(Comment(text))
228+
if text.starts_with("[CDATA[") && text.ends_with("]]") {
229+
let data = StrTendril::from_slice(&text[7..(text.len() - 2)]);
230+
self.add_node(Data(DataType::Cdata, data))
231+
} else {
232+
self.add_node(Comment(text))
233+
}
212234
}
213235

214236
fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Self::Handle {
@@ -316,6 +338,11 @@ mod atoms {
316338
atom target;
317339
atom data;
318340

341+
atom type_ = "type";
342+
atom script;
343+
atom style;
344+
atom cdata;
345+
319346
atom id_counter;
320347
atom roots;
321348
atom nodes;
@@ -369,6 +396,18 @@ impl NifEncoder for Parent {
369396
}
370397
}
371398

399+
// DataType
400+
401+
impl NifEncoder for DataType {
402+
fn encode<'a>(&self, env: NifEnv<'a>) -> NifTerm<'a> {
403+
match *self {
404+
DataType::Script => atoms::script().encode(env),
405+
DataType::Style => atoms::style().encode(env),
406+
DataType::Cdata => atoms::cdata().encode(env),
407+
}
408+
}
409+
}
410+
372411
// Node
373412

374413
fn split_ns_and_tag(ns_tag: &str) -> (&str, &str) {
@@ -411,11 +450,13 @@ impl NifEncoder for Node {
411450
.map_put(content_atom, STW(content).encode(env)).ok().unwrap()
412451
},
413452

414-
Data(ref content) => {
453+
Data(ref data_type, ref content) => {
454+
let type_atom = atoms::type_().encode(env);
415455
let content_atom = atoms::content().encode(env);
416456
make_ex_struct(env, "Elixir.Meeseeks.Document.Data").ok().unwrap()
417457
.map_put(parent_atom, self.parent.encode(env)).ok().unwrap()
418458
.map_put(id_atom, self.id.encode(env)).ok().unwrap()
459+
.map_put(type_atom, data_type.encode(env)).ok().unwrap()
419460
.map_put(content_atom, STW(content).encode(env)).ok().unwrap()
420461
},
421462

0 commit comments

Comments
 (0)