Skip to content

Commit f4048cb

Browse files
author
bors-servo
authored
Auto merge of #300 - cynicaldevil:remove-dep-on-sync-methods, r=nox
TreeBuilder no longer relies on `same_tree` and `has_parent_node` The use of `same_tree` and `has_parent_node` meant that the TreeBuilder needed access to the DOM state to carry out its tasks.
2 parents 84a8ea8 + 307f14c commit f4048cb

File tree

6 files changed

+71
-19
lines changed

6 files changed

+71
-19
lines changed

html5ever/examples/noop-tree-builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ impl TreeSink for Sink {
8686
_sibling: &usize,
8787
_new_node: NodeOrText<usize>) { }
8888

89+
fn append_based_on_parent_node(&mut self,
90+
_element: &usize,
91+
_prev_element: &usize,
92+
_new_node: NodeOrText<usize>) { }
93+
8994
fn parse_error(&mut self, _msg: Cow<'static, str>) { }
9095
fn set_quirks_mode(&mut self, _mode: QuirksMode) { }
9196
fn append(&mut self, _parent: &usize, _child: NodeOrText<usize>) { }

html5ever/examples/print-tree-actions.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ impl TreeSink for Sink {
109109
}
110110
}
111111

112+
fn append_based_on_parent_node(&mut self,
113+
element: &Self::Handle,
114+
prev_element: &Self::Handle,
115+
child: NodeOrText<Self::Handle>) {
116+
117+
if self.has_parent_node(element) {
118+
self.append_before_sibling(element, child);
119+
} else {
120+
self.append(prev_element, child);
121+
}
122+
}
123+
112124
fn append_doctype_to_document(&mut self,
113125
name: StrTendril,
114126
public_id: StrTendril,
@@ -124,7 +136,7 @@ impl TreeSink for Sink {
124136
}
125137
}
126138

127-
fn associate_with_form(&mut self, _target: &usize, _form: &usize) {
139+
fn associate_with_form(&mut self, _target: &usize, _form: &usize, _nodes: (&usize, Option<&usize>)) {
128140
// No form owner support. Since same_tree always returns
129141
// true we cannot be sure that this associate_with_form call is
130142
// valid

html5ever/src/tree_builder/mod.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -386,15 +386,10 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
386386
let contents = self.sink.get_template_contents(&elem);
387387
return LastChild(contents);
388388
} else if self.html_elem_named(&elem, local_name!("table")) {
389-
// Try inserting "inside last table's parent node, immediately before last table"
390-
if self.sink.has_parent_node(&elem) {
391-
return BeforeSibling(elem.clone());
392-
} else {
393-
// If elem has no parent, we regain ownership of the child.
394-
// Insert "inside previous element, after its last child (if any)"
395-
let previous_element = (*iter.peek().unwrap()).clone();
396-
return LastChild(previous_element);
397-
}
389+
return TableFosterParenting {
390+
element: elem.clone(),
391+
prev_element: (*iter.peek().unwrap()).clone(),
392+
};
398393
}
399394
}
400395
let html_elem = self.html_elem();
@@ -404,7 +399,11 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
404399
fn insert_at(&mut self, insertion_point: InsertionPoint<Handle>, child: NodeOrText<Handle>) {
405400
match insertion_point {
406401
LastChild(parent) => self.sink.append(&parent, child),
407-
BeforeSibling(sibling) => self.sink.append_before_sibling(&sibling, child)
402+
BeforeSibling(sibling) => self.sink.append_before_sibling(&sibling, child),
403+
TableFosterParenting { element, prev_element } => self.sink.append_based_on_parent_node(
404+
&element,
405+
&prev_element,
406+
child),
408407
}
409408
}
410409
}
@@ -1178,9 +1177,10 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
11781177
let elem = create_element(&mut self.sink, qname.clone(), attrs.clone());
11791178

11801179
let insertion_point = self.appropriate_place_for_insertion(None);
1181-
let tree_node = match insertion_point {
1180+
let (node1, node2) = match insertion_point {
11821181
LastChild(ref p) |
1183-
BeforeSibling(ref p) => p.clone()
1182+
BeforeSibling(ref p) => (p.clone(), None),
1183+
TableFosterParenting { ref element, ref prev_element } => (element.clone(), Some(prev_element.clone())),
11841184
};
11851185

11861186
// Step 12.
@@ -1190,10 +1190,12 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
11901190
!(listed(qname.expanded()) &&
11911191
attrs.iter().any(|a| a.name.expanded() == expanded_name!("", "form"))) {
11921192

1193-
let form = self.form_elem.as_ref().unwrap().clone();
1194-
if self.sink.same_tree(&tree_node, &form) {
1195-
self.sink.associate_with_form(&elem, &form)
1196-
}
1193+
let form = self.form_elem.as_ref().unwrap().clone();
1194+
let node2 = match node2 {
1195+
Some(ref n) => Some(n),
1196+
None => None,
1197+
};
1198+
self.sink.associate_with_form(&elem, &form, (&node1, node2));
11971199
}
11981200

11991201
self.insert_at(insertion_point, AppendNode(elem.clone()));
@@ -1644,6 +1646,14 @@ mod test {
16441646
self.rcdom.append_before_sibling(sibling, child)
16451647
}
16461648

1649+
fn append_based_on_parent_node(
1650+
&mut self,
1651+
element: &Handle,
1652+
prev_element: &Handle,
1653+
child: NodeOrText<Handle>) {
1654+
self.rcdom.append_based_on_parent_node(element, prev_element, child)
1655+
}
1656+
16471657
fn append_doctype_to_document(&mut self,
16481658
name: StrTendril,
16491659
public_id: StrTendril,

html5ever/src/tree_builder/types.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,7 @@ pub enum InsertionPoint<Handle> {
8686
/// Insert as last child in this parent.
8787
LastChild(Handle),
8888
/// Insert before this following sibling.
89-
BeforeSibling(Handle)
89+
BeforeSibling(Handle),
90+
/// Insertion point is decided based on existence of element's parent node.
91+
TableFosterParenting { element: Handle, prev_element: Handle },
9092
}

markup5ever/interface/tree_builder.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ pub trait TreeSink {
140140
/// The child node will not already have a parent.
141141
fn append(&mut self, parent: &Self::Handle, child: NodeOrText<Self::Handle>);
142142

143+
/// When the insertion point is decided by the existence of a parent node of the
144+
/// element, we consider both possibilities and send the element which will be used
145+
/// if a parent node exists, along with the element to be used if there isn't one.
146+
fn append_based_on_parent_node(&mut self,
147+
element: &Self::Handle,
148+
prev_element: &Self::Handle,
149+
child: NodeOrText<Self::Handle>);
150+
143151
/// Append a `DOCTYPE` element to the `Document` node.
144152
fn append_doctype_to_document(&mut self,
145153
name: StrTendril,
@@ -188,7 +196,10 @@ pub trait TreeSink {
188196
fn add_attrs_if_missing(&mut self, target: &Self::Handle, attrs: Vec<Attribute>);
189197

190198
/// Associate the given form-associatable element with the form element
191-
fn associate_with_form(&mut self, _target: &Self::Handle, _form: &Self::Handle) {}
199+
fn associate_with_form(&mut self,
200+
_target: &Self::Handle,
201+
_form: &Self::Handle,
202+
_nodes: (&Self::Handle, Option<&Self::Handle>)) {}
192203

193204
/// Detach the given node from its parent.
194205
fn remove_from_parent(&mut self, target: &Self::Handle);

markup5ever/rcdom.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,18 @@ impl TreeSink for RcDom {
265265
parent.children.borrow_mut().insert(i, child);
266266
}
267267

268+
fn append_based_on_parent_node(&mut self,
269+
element: &Self::Handle,
270+
prev_element: &Self::Handle,
271+
child: NodeOrText<Self::Handle>) {
272+
273+
if self.has_parent_node(element) {
274+
self.append_before_sibling(element, child);
275+
} else {
276+
self.append(prev_element, child);
277+
}
278+
}
279+
268280
fn append_doctype_to_document(&mut self,
269281
name: StrTendril,
270282
public_id: StrTendril,

0 commit comments

Comments
 (0)