Skip to content

Commit deeb26f

Browse files
committed
Update to html5ever v0.16.0 (#3)
- Replace html5ever_atoms with markup5ever - Add ProcessingInstruction to NodeEnum and nif encoding - Reconcile changes to TreeSink interface - Reconcile misc changes
1 parent eebaa1e commit deeb26f

File tree

3 files changed

+55
-36
lines changed

3 files changed

+55
-36
lines changed

native/meeseeks_html5ever_nif/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ crate-type = ["dylib"]
1111
[dependencies]
1212
rustler = "^0.14"
1313

14-
html5ever = "0.15.0"
15-
html5ever-atoms = "0.3.0"
14+
html5ever = "0.16.0"
15+
markup5ever = "0.1"
1616
tendril = "0.2.3"
17-
lazy_static = "*"
17+
lazy_static = "0.2"
1818
scoped-pool = "1.0.0"

native/meeseeks_html5ever_nif/src/flat_dom.rs

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ use std::borrow::Cow;
22
use std::collections::HashSet;
33
use std::default::Default;
44

5-
use html5ever::{ QualName };
6-
use html5ever::tokenizer::{Attribute};
7-
use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, AppendNode, AppendText};
5+
use html5ever::{ QualName, Attribute };
6+
use html5ever::tree_builder::{TreeSink, QuirksMode, NodeOrText, ElementFlags};
7+
use markup5ever::ExpandedName;
8+
89
use tendril::StrTendril;
910

1011
use rustler::{NifEnv, NifTerm, NifEncoder};
1112
use rustler::types::elixir_struct::{ make_ex_struct};
1213
use rustler::types::map::{ map_new };
1314

14-
use self::NodeEnum::{Comment, Data, Doctype, Document, Element, Text};
15+
use self::NodeEnum::{Comment, Data, Doctype, Document, Element, ProcessingInstruction, Text};
1516

1617
#[derive(Copy, Clone, PartialEq, Debug)]
1718
pub struct Id(usize);
@@ -38,15 +39,17 @@ enum NodeEnum {
3839
Doctype(StrTendril, StrTendril, StrTendril),
3940
Document,
4041
Element(QualName, Vec<Attribute>),
42+
ProcessingInstruction(StrTendril, StrTendril),
4143
Text(StrTendril),
4244
}
4345

4446
impl NodeEnum {
4547
fn script_or_style(&self) -> bool {
4648
match *self {
47-
Element(ref name, _) => {
48-
match *name {
49-
qualname!(html, "script") | qualname!(html, "style") =>
49+
Element(ref name, ..) => {
50+
match name.expanded() {
51+
expanded_name!(html "script") |
52+
expanded_name!(html "style") =>
5053
true,
5154
_ => false,
5255
}
@@ -181,56 +184,60 @@ impl TreeSink for FlatDom {
181184
}
182185

183186
// Not supported
184-
fn get_template_contents(&mut self, _target: Self::Handle) -> Self::Handle {
187+
fn get_template_contents(&mut self, _target: &Self::Handle) -> Self::Handle {
185188
panic!("Templates not supported");
186189
}
187190

188191
// Not supported
189192
fn set_quirks_mode(&mut self, _mode: QuirksMode) {}
190193

191-
fn same_node(&self, x: Self::Handle, y: Self::Handle) -> bool {
194+
fn same_node(&self, x: &Self::Handle, y: &Self::Handle) -> bool {
192195
x == y
193196
}
194197

195-
fn elem_name(&self, target: Self::Handle) -> QualName {
196-
if let Element(ref name, _) = self.node(target).node {
197-
name.clone()
198+
fn elem_name(&self, target: &Self::Handle) -> ExpandedName {
199+
if let Element(ref name, ..) = self.node(*target).node {
200+
name.expanded()
198201
} else {
199202
panic!("not an element!")
200203
}
201204
}
202205

203-
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>) -> Self::Handle {
206+
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>, _flags: ElementFlags) -> Self::Handle {
204207
self.add_node(Element(name, attrs))
205208
}
206209

207210
fn create_comment(&mut self, text: StrTendril) -> Self::Handle {
208211
self.add_node(Comment(text))
209212
}
210213

211-
fn has_parent_node(&self, node: Self::Handle) -> bool {
212-
match self.node(node).parent {
214+
fn create_pi(&mut self, target: StrTendril, data: StrTendril) -> Self::Handle {
215+
self.add_node(ProcessingInstruction(target, data))
216+
}
217+
218+
fn has_parent_node(&self, node: &Self::Handle) -> bool {
219+
match self.node(*node).parent {
213220
Parent::None => false,
214221
_ => true,
215222
}
216223
}
217224

218-
fn append(&mut self, parent: Self::Handle, child: NodeOrText<Self::Handle>) {
225+
fn append(&mut self, parent: &Self::Handle, child: NodeOrText<Self::Handle>) {
219226
match child {
220-
AppendNode(node) => self.append_node(parent, node),
221-
AppendText(text) => self.append_text(parent, text),
227+
NodeOrText::AppendNode(node) => self.append_node(*parent, node),
228+
NodeOrText::AppendText(text) => self.append_text(*parent, text),
222229
};
223230
}
224231

225-
fn append_before_sibling(&mut self, sibling: Self::Handle, child: NodeOrText<Self::Handle>) {
226-
let (parent, i) = self.get_parent_and_index(sibling);
232+
fn append_before_sibling(&mut self, sibling: &Self::Handle, child: NodeOrText<Self::Handle>) {
233+
let (parent, i) = self.get_parent_and_index(*sibling);
227234

228235
let child = match (child, i) {
229236
// No previous node
230-
(AppendText(text), 0) => self.add_node(Text(text)),
237+
(NodeOrText::AppendText(text), 0) => self.add_node(Text(text)),
231238

232239
// Check for text node before insertion point, append if there is
233-
(AppendText(text), i) => {
240+
(NodeOrText::AppendText(text), i) => {
234241
let prev = self.node(parent).children[i-1];
235242
if self.node_mut(prev).node.append_text(&text) {
236243
return;
@@ -241,7 +248,7 @@ impl TreeSink for FlatDom {
241248
// Tree builder promises no text no *after* insertion point
242249

243250
// Any other kind of node
244-
(AppendNode(node), _) => node,
251+
(NodeOrText::AppendNode(node), _) => node,
245252
};
246253

247254
if self.node(child).parent.is_some() {
@@ -257,9 +264,9 @@ impl TreeSink for FlatDom {
257264
self.append_node(Id(0), doctype);
258265
}
259266

260-
fn add_attrs_if_missing(&mut self, target: Self::Handle, attrs: Vec<Attribute>) {
261-
let target_node = self.node_mut(target);
262-
let target_attrs = if let Element(_, ref mut attrs) = target_node.node {
267+
fn add_attrs_if_missing(&mut self, target: &Self::Handle, attrs: Vec<Attribute>) {
268+
let target_node = self.node_mut(*target);
269+
let target_attrs = if let Element(_, ref mut attrs, ..) = target_node.node {
263270
attrs
264271
} else {
265272
panic!("not an element")
@@ -272,20 +279,20 @@ impl TreeSink for FlatDom {
272279
}));
273280
}
274281

275-
fn remove_from_parent(&mut self, target: Self::Handle) {
276-
self.remove_from_parent(target);
282+
fn remove_from_parent(&mut self, target: &Self::Handle) {
283+
self.remove_from_parent(*target);
277284
}
278285

279-
fn reparent_children(&mut self, node: Self::Handle, new_parent: Self::Handle) {
280-
let children = self.node(node).children.clone();
286+
fn reparent_children(&mut self, node: &Self::Handle, new_parent: &Self::Handle) {
287+
let children = self.node(*node).children.clone();
281288
for child in &children {
282289
self.remove_from_parent(*child);
283-
self.append_node(new_parent, *child);
290+
self.append_node(*new_parent, *child);
284291
}
285292
}
286293

287294
// Not supported
288-
fn mark_script_already_started(&mut self, _target: Self::Handle) {
295+
fn mark_script_already_started(&mut self, _target: &Self::Handle) {
289296
panic!("not supported")
290297
}
291298
}
@@ -306,6 +313,8 @@ mod atoms {
306313
atom tag;
307314
atom attributes;
308315
atom children;
316+
atom target;
317+
atom data;
309318

310319
atom id_counter;
311320
atom roots;
@@ -426,6 +435,16 @@ impl NifEncoder for Node {
426435
.map_put(children_atom, self.children.encode(env)).ok().unwrap()
427436
},
428437

438+
ProcessingInstruction(ref target, ref data) => {
439+
let target_atom = atoms::target().encode(env);
440+
let data_atom = atoms::data().encode(env);
441+
make_ex_struct(env, "Elixir.Meeseeks.Document.ProcessingInstruction").ok().unwrap()
442+
.map_put(parent_atom, self.parent.encode(env)).ok().unwrap()
443+
.map_put(id_atom, self.id.encode(env)).ok().unwrap()
444+
.map_put(target_atom, STW(target).encode(env)).ok().unwrap()
445+
.map_put(data_atom, STW(data).encode(env)).ok().unwrap()
446+
},
447+
429448
Text(ref content) => {
430449
let content_atom = atoms::content().encode(env);
431450
make_ex_struct(env, "Elixir.Meeseeks.Document.Text").ok().unwrap()

native/meeseeks_html5ever_nif/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ extern crate rustler;
44
extern crate lazy_static;
55
extern crate html5ever;
66
#[macro_use]
7-
extern crate html5ever_atoms;
7+
extern crate markup5ever;
88
extern crate tendril;
99
extern crate scoped_pool;
1010

0 commit comments

Comments
 (0)