Skip to content

Commit 2cd6255

Browse files
author
bors-servo
authored
Auto merge of #252 - jdm:revert-form-owner, r=jdm
Revert "Implement support for form owner" This reverts commit 69f7069 (#249). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/html5ever/252) <!-- Reviewable:end -->
2 parents dfeb635 + d043fd8 commit 2cd6255

File tree

7 files changed

+54
-145
lines changed

7 files changed

+54
-145
lines changed

examples/noop-tree-builder.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,6 @@ impl TreeSink for Sink {
5757
x == y
5858
}
5959

60-
fn same_tree(&self, _x: usize, _y: usize) -> bool {
61-
true
62-
}
63-
6460
fn elem_name(&self, target: usize) -> QualName {
6561
self.names.get(&target).expect("not an element").clone()
6662
}
@@ -75,15 +71,13 @@ impl TreeSink for Sink {
7571
self.get_id()
7672
}
7773

78-
fn has_parent_node(&self, _node: usize) -> bool {
79-
// `node` will have a parent unless a script moved it, and we're
80-
// not running scripts. Therefore we can aways return true.
81-
true
82-
}
83-
8474
fn append_before_sibling(&mut self,
8575
_sibling: usize,
86-
_new_node: NodeOrText<usize>) { }
76+
_new_node: NodeOrText<usize>) -> Result<(), NodeOrText<usize>> {
77+
// `sibling` will have a parent unless a script moved it, and we're
78+
// not running scripts. Therefore we can aways return `Ok(())`.
79+
Ok(())
80+
}
8781

8882
fn parse_error(&mut self, _msg: Cow<'static, str>) { }
8983
fn set_quirks_mode(&mut self, _mode: QuirksMode) { }

examples/print-tree-actions.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,6 @@ impl TreeSink for Sink {
8282
id
8383
}
8484

85-
fn has_parent_node(&self, _node: usize) -> bool {
86-
// `node` will have a parent unless a script moved it, and we're
87-
// not running scripts. Therefore we can aways return true
88-
true
89-
}
90-
9185
fn append(&mut self, parent: usize, child: NodeOrText<usize>) {
9286
match child {
9387
AppendNode(n)
@@ -99,13 +93,17 @@ impl TreeSink for Sink {
9993

10094
fn append_before_sibling(&mut self,
10195
sibling: usize,
102-
new_node: NodeOrText<usize>) {
96+
new_node: NodeOrText<usize>) -> Result<(), NodeOrText<usize>> {
10397
match new_node {
10498
AppendNode(n)
10599
=> println!("Append node {} before {}", n, sibling),
106100
AppendText(t)
107101
=> println!("Append text before {}: \"{}\"", sibling, escape_default(&t)),
108102
}
103+
104+
// `sibling` will have a parent unless a script moved it, and we're
105+
// not running scripts. Therefore we can aways return `Ok(())`.
106+
Ok(())
109107
}
110108

111109
fn append_doctype_to_document(&mut self,
@@ -123,12 +121,6 @@ impl TreeSink for Sink {
123121
}
124122
}
125123

126-
fn associate_with_form(&mut self, _target: usize, _form: usize) {
127-
// No form owner support. Since same_tree always returns
128-
// true we cannot be sure that this associate_with_form call is
129-
// valid
130-
}
131-
132124
fn remove_from_parent(&mut self, target: usize) {
133125
println!("Remove {} from parent", target);
134126
}

src/rcdom.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,6 @@ impl TreeSink for RcDom {
223223
new_node(Comment(text))
224224
}
225225

226-
fn has_parent_node(&self, node: Handle) -> bool {
227-
let node = node.borrow();
228-
node.parent.is_some()
229-
}
230-
231226
fn append(&mut self, parent: Handle, child: NodeOrText<Handle>) {
232227
// Append to an existing Text node if we have one.
233228
match child {
@@ -246,9 +241,8 @@ impl TreeSink for RcDom {
246241

247242
fn append_before_sibling(&mut self,
248243
sibling: Handle,
249-
child: NodeOrText<Handle>) {
250-
let (parent, i) = get_parent_and_index(&sibling)
251-
.expect("append_before_sibling called on node without parent");
244+
child: NodeOrText<Handle>) -> Result<(), NodeOrText<Handle>> {
245+
let (parent, i) = unwrap_or_return!(get_parent_and_index(&sibling), Err(child));
252246

253247
let child = match (child, i) {
254248
// No previous node.
@@ -259,7 +253,7 @@ impl TreeSink for RcDom {
259253
let parent = parent.borrow();
260254
let prev = &parent.children[i-1];
261255
if append_to_existing_text(prev, &text) {
262-
return;
256+
return Ok(());
263257
}
264258
new_node(Text(text))
265259
}
@@ -277,6 +271,7 @@ impl TreeSink for RcDom {
277271

278272
child.borrow_mut().parent = Some(Rc::downgrade(&parent));
279273
parent.borrow_mut().children.insert(i, child);
274+
Ok(())
280275
}
281276

282277
fn append_doctype_to_document(&mut self,

src/tree_builder/actions.rs

+36-34
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,40 @@ impl<Handle, Sink> TreeBuilderActions<Handle>
210210

211211
// Insert at the "appropriate place for inserting a node".
212212
fn insert_appropriately(&mut self, child: NodeOrText<Handle>, override_target: Option<Handle>) {
213-
let insertion_point = self.appropriate_place_for_insertion(override_target);
214-
self.insert_at(insertion_point, child);
213+
declare_tag_set!(foster_target = "table" "tbody" "tfoot" "thead" "tr");
214+
let target = override_target.unwrap_or_else(|| self.current_node());
215+
if !(self.foster_parenting && self.elem_in(target.clone(), foster_target)) {
216+
if self.html_elem_named(target.clone(), local_name!("template")) {
217+
// No foster parenting (inside template).
218+
let contents = self.sink.get_template_contents(target);
219+
self.sink.append(contents, child);
220+
} else {
221+
// No foster parenting (the common case).
222+
self.sink.append(target, child);
223+
}
224+
return;
225+
}
226+
227+
// Foster parenting
228+
let mut iter = self.open_elems.iter().rev().peekable();
229+
while let Some(elem) = iter.next() {
230+
if self.html_elem_named(elem.clone(), local_name!("template")) {
231+
let contents = self.sink.get_template_contents(elem.clone());
232+
self.sink.append(contents, child);
233+
return;
234+
} else if self.html_elem_named(elem.clone(), local_name!("table")) {
235+
// Try inserting "inside last table's parent node, immediately before last table"
236+
if let Err(child) = self.sink.append_before_sibling(elem.clone(), child) {
237+
// If last_table has no parent, we regain ownership of the child.
238+
// Insert "inside previous element, after its last child (if any)"
239+
let previous_element = (*iter.peek().unwrap()).clone();
240+
self.sink.append(previous_element, child);
241+
}
242+
return;
243+
}
244+
}
245+
let html_elem = self.html_elem();
246+
self.sink.append(html_elem, child);
215247
}
216248

217249
fn adoption_agency(&mut self, subject: LocalName) {
@@ -743,40 +775,10 @@ impl<Handle, Sink> TreeBuilderActions<Handle>
743775
// FIXME: application cache selection algorithm
744776
}
745777

746-
// https://html.spec.whatwg.org/multipage/#create-an-element-for-the-token
747778
fn insert_element(&mut self, push: PushFlag, ns: Namespace, name: LocalName, attrs: Vec<Attribute>)
748779
-> Handle {
749-
declare_tag_set!(form_associatable =
750-
"button" "fieldset" "input" "object"
751-
"output" "select" "textarea" "img");
752-
753-
declare_tag_set!(listed = [form_associatable] - "img");
754-
755-
// Step 7.
756-
let qname = QualName::new(ns, name);
757-
let elem = self.sink.create_element(qname.clone(), attrs.clone());
758-
759-
let insertion_point = self.appropriate_place_for_insertion(None);
760-
let tree_node = match insertion_point {
761-
LastChild(ref p) |
762-
BeforeSibling(ref p) => p.clone()
763-
};
764-
765-
// Step 12.
766-
if form_associatable(qname.clone()) &&
767-
self.form_elem.is_some() &&
768-
!self.in_html_elem_named(local_name!("template")) &&
769-
!(listed(qname.clone()) &&
770-
attrs.iter().any(|a| a.name == qualname!("","form"))) {
771-
772-
let form = self.form_elem.as_ref().unwrap().clone();
773-
if self.sink.same_tree(tree_node, form.clone()) {
774-
self.sink.associate_with_form(elem.clone(), form)
775-
}
776-
}
777-
778-
self.insert_at(insertion_point, AppendNode(elem.clone()));
779-
780+
let elem = self.sink.create_element(QualName::new(ns, name), attrs);
781+
self.insert_appropriately(AppendNode(elem.clone()), None);
780782
match push {
781783
Push => self.push(&elem),
782784
NoPush => (),

src/tree_builder/interface.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,6 @@ pub trait TreeSink {
7272
/// Do two handles refer to the same node?
7373
fn same_node(&self, x: Self::Handle, y: Self::Handle) -> bool;
7474

75-
/// Are two handles present in the same tree
76-
fn same_tree(&self, x: Self::Handle, y: Self::Handle) -> bool {
77-
true
78-
}
79-
8075
/// What is the name of this element?
8176
///
8277
/// Should never be called on a non-element node;
@@ -98,18 +93,15 @@ pub trait TreeSink {
9893
/// Create a comment node.
9994
fn create_comment(&mut self, text: StrTendril) -> Self::Handle;
10095

101-
/// Does the node have a parent?
102-
fn has_parent_node(&self, node: Self::Handle) -> bool;
103-
10496
/// Append a node as the last child of the given node. If this would
10597
/// produce adjacent sibling text nodes, it should concatenate the text
10698
/// instead.
10799
///
108100
/// The child node will not already have a parent.
109101
fn append(&mut self, parent: Self::Handle, child: NodeOrText<Self::Handle>);
110102

111-
/// Append a node as the sibling immediately before the given node.
112-
/// This method will only be called if has_parent_node(sibling) is true
103+
/// Append a node as the sibling immediately before the given node. If that node
104+
/// has no parent, do nothing and return Err(new_node).
113105
///
114106
/// The tree builder promises that `sibling` is not a text node. However its
115107
/// old previous sibling, which would become the new node's previous sibling,
@@ -119,7 +111,7 @@ pub trait TreeSink {
119111
/// NB: `new_node` may have an old parent, from which it should be removed.
120112
fn append_before_sibling(&mut self,
121113
sibling: Self::Handle,
122-
new_node: NodeOrText<Self::Handle>);
114+
new_node: NodeOrText<Self::Handle>) -> Result<(), NodeOrText<Self::Handle>>;
123115

124116
/// Append a `DOCTYPE` element to the `Document` node.
125117
fn append_doctype_to_document(&mut self,
@@ -132,9 +124,6 @@ pub trait TreeSink {
132124
/// with something else than an element.
133125
fn add_attrs_if_missing(&mut self, target: Self::Handle, attrs: Vec<Attribute>);
134126

135-
/// Associate the given form-associatable element with the form element
136-
fn associate_with_form(&mut self, target: Self::Handle, form: Self::Handle) {}
137-
138127
/// Detach the given node from its parent.
139128
fn remove_from_parent(&mut self, target: Self::Handle);
140129

src/tree_builder/mod.rs

+1-53
Original file line numberDiff line numberDiff line change
@@ -369,53 +369,6 @@ impl<Handle, Sink> TreeBuilder<Handle, Sink>
369369
pub fn is_fragment(&self) -> bool {
370370
self.context_elem.is_some()
371371
}
372-
373-
fn appropriate_place_for_insertion(&mut self,
374-
override_target: Option<Handle>)
375-
-> InsertionPoint<Handle> {
376-
use self::tag_sets::*;
377-
378-
declare_tag_set!(foster_target = "table" "tbody" "tfoot" "thead" "tr");
379-
let target = override_target.unwrap_or_else(|| self.current_node());
380-
if !(self.foster_parenting && self.elem_in(target.clone(), foster_target)) {
381-
if self.html_elem_named(target.clone(), local_name!("template")) {
382-
// No foster parenting (inside template).
383-
let contents = self.sink.get_template_contents(target);
384-
return LastChild(contents);
385-
} else {
386-
// No foster parenting (the common case).
387-
return LastChild(target);
388-
}
389-
}
390-
391-
// Foster parenting
392-
let mut iter = self.open_elems.iter().rev().peekable();
393-
while let Some(elem) = iter.next() {
394-
if self.html_elem_named(elem.clone(), local_name!("template")) {
395-
let contents = self.sink.get_template_contents(elem.clone());
396-
return LastChild(contents);
397-
} else if self.html_elem_named(elem.clone(), local_name!("table")) {
398-
// Try inserting "inside last table's parent node, immediately before last table"
399-
if self.sink.has_parent_node(elem.clone()) {
400-
return BeforeSibling(elem.clone());
401-
} else {
402-
// If elem has no parent, we regain ownership of the child.
403-
// Insert "inside previous element, after its last child (if any)"
404-
let previous_element = (*iter.peek().unwrap()).clone();
405-
return LastChild(previous_element);
406-
}
407-
}
408-
}
409-
let html_elem = self.html_elem();
410-
LastChild(html_elem)
411-
}
412-
413-
fn insert_at(&mut self, insertion_point: InsertionPoint<Handle>, child: NodeOrText<Handle>) {
414-
match insertion_point {
415-
LastChild(parent) => self.sink.append(parent, child),
416-
BeforeSibling(sibling) => self.sink.append_before_sibling(sibling, child)
417-
}
418-
}
419372
}
420373

421374
impl<Handle, Sink> TokenSink
@@ -576,18 +529,13 @@ mod test {
576529
self.rcdom.create_comment(text)
577530
}
578531

579-
fn has_parent_node(&self, node: Handle) -> bool {
580-
let node = node.borrow();
581-
node.parent.is_some()
582-
}
583-
584532
fn append(&mut self, parent: Handle, child: NodeOrText<Handle>) {
585533
self.rcdom.append(parent, child)
586534
}
587535

588536
fn append_before_sibling(&mut self,
589537
sibling: Handle,
590-
child: NodeOrText<Handle>) {
538+
child: NodeOrText<Handle>) -> Result<(), NodeOrText<Handle>> {
591539
self.rcdom.append_before_sibling(sibling, child)
592540
}
593541

src/tree_builder/types.rs

-11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub use self::SplitStatus::*;
1919
pub use self::Token::*;
2020
pub use self::ProcessResult::*;
2121
pub use self::FormatEntry::*;
22-
pub use self::InsertionPoint::*;
2322

2423
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
2524
pub enum InsertionMode {
@@ -81,13 +80,3 @@ pub enum FormatEntry<Handle> {
8180
Element(Handle, Tag),
8281
Marker,
8382
}
84-
85-
pub enum InsertionPoint<Handle> {
86-
/// Holds the parent
87-
LastChild(Handle),
88-
/// Holds the sibling before which the node will be inserted
89-
/// TODO: Is the parent node needed? Is there a problem with using
90-
/// the sibling to find if the form element is in the same home
91-
/// subtree?
92-
BeforeSibling(Handle)
93-
}

0 commit comments

Comments
 (0)