diff options
8 files changed, 81 insertions, 77 deletions
diff --git a/components/script/dom/htmlcollection.rs b/components/script/dom/htmlcollection.rs index f9a6ba38350..71d57d94005 100644 --- a/components/script/dom/htmlcollection.rs +++ b/components/script/dom/htmlcollection.rs @@ -160,7 +160,7 @@ impl HTMLCollection { HTMLCollection::create(window, root, box ElementChildFilter) } - fn elements_iter(&self) -> HTMLCollectionElementsIter { + pub fn elements_iter(&self) -> HTMLCollectionElementsIter { let ref filter = self.collection.1; let root = self.collection.0.root(); let mut node_iter = root.traverse_preorder(); @@ -173,7 +173,7 @@ impl HTMLCollection { } } -struct HTMLCollectionElementsIter<'a> { +pub struct HTMLCollectionElementsIter<'a> { node_iter: TreeIterator, root: Root<Node>, filter: &'a Box<CollectionFilter>, diff --git a/components/script/dom/htmltablesectionelement.rs b/components/script/dom/htmltablesectionelement.rs index 03e83f3d3ed..7afee5d1917 100644 --- a/components/script/dom/htmltablesectionelement.rs +++ b/components/script/dom/htmltablesectionelement.rs @@ -4,19 +4,25 @@ use cssparser::RGBA; use dom::attr::Attr; +use dom::bindings::codegen::Bindings::HTMLCollectionBinding::HTMLCollectionMethods; use dom::bindings::codegen::Bindings::HTMLTableSectionElementBinding::{self, HTMLTableSectionElementMethods}; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; +use dom::bindings::codegen::InheritTypes::ElementCast; use dom::bindings::codegen::InheritTypes::NodeCast; use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLTableRowElementDerived, HTMLTableSectionElementDerived}; +use dom::bindings::error::Error; +use dom::bindings::error::{ErrorResult, Fallible}; use dom::bindings::js::{Root, RootedReference}; use dom::document::Document; use dom::element::{AttributeMutation, Element, ElementTypeId}; use dom::eventtarget::{EventTarget, EventTargetTypeId}; use dom::htmlcollection::{CollectionFilter, HTMLCollection}; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; +use dom::htmltablerowelement::HTMLTableRowElement; use dom::node::{Node, NodeTypeId, window_from_node}; use dom::virtualmethods::VirtualMethods; use std::cell::Cell; +use std::iter; use util::str::{self, DOMString}; #[dom_struct] @@ -71,6 +77,61 @@ impl HTMLTableSectionElementMethods for HTMLTableSectionElement { fn Rows(&self) -> Root<HTMLCollection> { HTMLCollection::create(&window_from_node(self), NodeCast::from_ref(self), box RowsFilter) } + + // https://html.spec.whatwg.org/multipage/#dom-tbody-insertrow + fn InsertRow(&self, index: i32) -> Fallible<Root<HTMLElement>> { + if index < -1 { + return Err(Error::IndexSize); + } + + let node = NodeCast::from_ref(self); + let tr = HTMLTableRowElement::new("tr".to_owned(), None, node.owner_doc().r()); + + let after_node = if index == -1 { + None + } else { + match self.Rows() + .elements_iter() + .map(NodeCast::from_root) + .map(Some) + .chain(iter::once(None)) + .nth(index as usize) { + None => return Err(Error::IndexSize), + Some(node) => node, + } + }; + + { + let tr_node = NodeCast::from_ref(tr.r()); + try!(node.InsertBefore(tr_node, after_node.r())); + } + + Ok(HTMLElementCast::from_root(tr)) + } + + // https://html.spec.whatwg.org/multipage/#dom-tbody-deleterow + fn DeleteRow(&self, index: i32) -> ErrorResult { + let element = match index { + index if index < -1 => return Err(Error::IndexSize), + -1 => { + let last_child = NodeCast::from_ref(self).GetLastChild(); + match last_child.and_then(|node| node.inclusively_preceding_siblings() + .filter_map(ElementCast::to_root) + .filter(|n| n.is_htmltablerowelement()) + .next()) { + Some(element) => element, + None => return Ok(()), + } + }, + index => match self.Rows().Item(index as u32) { + Some(element) => element, + None => return Err(Error::IndexSize), + }, + }; + + NodeCast::from_ref(element.r()).remove_self(); + Ok(()) + } } impl VirtualMethods for HTMLTableSectionElement { diff --git a/components/script/dom/webidls/HTMLTableSectionElement.webidl b/components/script/dom/webidls/HTMLTableSectionElement.webidl index 3389912edcd..483fe197229 100644 --- a/components/script/dom/webidls/HTMLTableSectionElement.webidl +++ b/components/script/dom/webidls/HTMLTableSectionElement.webidl @@ -6,8 +6,10 @@ // https://html.spec.whatwg.org/multipage/#htmltablesectionelement interface HTMLTableSectionElement : HTMLElement { readonly attribute HTMLCollection rows; - //HTMLElement insertRow(optional long index = -1); - //void deleteRow(long index); + [Throws] + HTMLElement insertRow(optional long index = -1); + [Throws] + void deleteRow(long index); // also has obsolete members }; diff --git a/tests/wpt/metadata/html/dom/interfaces.html.ini b/tests/wpt/metadata/html/dom/interfaces.html.ini index 42e9300a4b6..123d9c48d66 100644 --- a/tests/wpt/metadata/html/dom/interfaces.html.ini +++ b/tests/wpt/metadata/html/dom/interfaces.html.ini @@ -4650,12 +4650,6 @@ [HTMLTableSectionElement interface: existence and properties of interface object] expected: FAIL - [HTMLTableSectionElement interface: operation insertRow(long)] - expected: FAIL - - [HTMLTableSectionElement interface: operation deleteRow(long)] - expected: FAIL - [HTMLTableSectionElement interface: attribute align] expected: FAIL @@ -4668,18 +4662,6 @@ [HTMLTableSectionElement interface: attribute vAlign] expected: FAIL - [HTMLTableSectionElement interface: document.createElement("tbody") must inherit property "insertRow" with the proper type (1)] - expected: FAIL - - [HTMLTableSectionElement interface: calling insertRow(long) on document.createElement("tbody") with too few arguments must throw TypeError] - expected: FAIL - - [HTMLTableSectionElement interface: document.createElement("tbody") must inherit property "deleteRow" with the proper type (2)] - expected: FAIL - - [HTMLTableSectionElement interface: calling deleteRow(long) on document.createElement("tbody") with too few arguments must throw TypeError] - expected: FAIL - [HTMLTableSectionElement interface: document.createElement("tbody") must inherit property "align" with the proper type (3)] expected: FAIL @@ -4692,18 +4674,6 @@ [HTMLTableSectionElement interface: document.createElement("tbody") must inherit property "vAlign" with the proper type (6)] expected: FAIL - [HTMLTableSectionElement interface: document.createElement("thead") must inherit property "insertRow" with the proper type (1)] - expected: FAIL - - [HTMLTableSectionElement interface: calling insertRow(long) on document.createElement("thead") with too few arguments must throw TypeError] - expected: FAIL - - [HTMLTableSectionElement interface: document.createElement("thead") must inherit property "deleteRow" with the proper type (2)] - expected: FAIL - - [HTMLTableSectionElement interface: calling deleteRow(long) on document.createElement("thead") with too few arguments must throw TypeError] - expected: FAIL - [HTMLTableSectionElement interface: document.createElement("thead") must inherit property "align" with the proper type (3)] expected: FAIL @@ -4716,18 +4686,6 @@ [HTMLTableSectionElement interface: document.createElement("thead") must inherit property "vAlign" with the proper type (6)] expected: FAIL - [HTMLTableSectionElement interface: document.createElement("tfoot") must inherit property "insertRow" with the proper type (1)] - expected: FAIL - - [HTMLTableSectionElement interface: calling insertRow(long) on document.createElement("tfoot") with too few arguments must throw TypeError] - expected: FAIL - - [HTMLTableSectionElement interface: document.createElement("tfoot") must inherit property "deleteRow" with the proper type (2)] - expected: FAIL - - [HTMLTableSectionElement interface: calling deleteRow(long) on document.createElement("tfoot") with too few arguments must throw TypeError] - expected: FAIL - [HTMLTableSectionElement interface: document.createElement("tfoot") must inherit property "align" with the proper type (3)] expected: FAIL diff --git a/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/deleteRow.html.ini b/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/deleteRow.html.ini deleted file mode 100644 index 5d14e0a04b0..00000000000 --- a/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/deleteRow.html.ini +++ /dev/null @@ -1,14 +0,0 @@ -[deleteRow.html] - type: testharness - [HTMLTableSectionElement deleteRow(0)] - expected: FAIL - - [HTMLTableSectionElement deleteRow(-1)] - expected: FAIL - - [HTMLTableSectionElement deleteRow(rows.length)] - expected: FAIL - - [HTMLTableSectionElement deleteRow(-2)] - expected: FAIL - diff --git a/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/insertRow.html.ini b/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/insertRow.html.ini deleted file mode 100644 index 8a4a0b0253e..00000000000 --- a/tests/wpt/metadata/html/semantics/tabular-data/the-tbody-element/insertRow.html.ini +++ /dev/null @@ -1,17 +0,0 @@ -[insertRow.html] - type: testharness - [HTMLTableSectionElement insertRow(0)] - expected: FAIL - - [HTMLTableSectionElement insertRow(-1)] - expected: FAIL - - [HTMLTableSectionElement insertRow()] - expected: FAIL - - [HTMLTableSectionElement insertRow(-2)] - expected: FAIL - - [HTMLTableSectionElement insertRow(rows.length + 1)] - expected: FAIL - diff --git a/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/deleteRow.html b/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/deleteRow.html index 820c7dd87b0..c81abd82600 100644 --- a/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/deleteRow.html +++ b/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/deleteRow.html @@ -43,4 +43,12 @@ test(function () { }); }, "HTMLTableSectionElement deleteRow(-2)"); +test(function () { + assert_equals(tbody.rows.length, 1); + tbody.deleteRow(-1); + assert_equals(tbody.rows.length, 0); + tbody.deleteRow(-1); + assert_equals(tbody.rows.length, 0); +}, "HTMLTableSectionElement deleteRow(-1) with no rows"); + </script> diff --git a/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/insertRow.html b/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/insertRow.html index 1318323986e..4c13db7aa5f 100644 --- a/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/insertRow.html +++ b/tests/wpt/web-platform-tests/html/semantics/tabular-data/the-tbody-element/insertRow.html @@ -36,6 +36,12 @@ test(function () { }, "HTMLTableSectionElement insertRow()"); test(function () { + var trEle = tbody.insertRow(tbody.rows.length); + assert_equals(tbody.rows[tbody.rows.length - 1], trEle); + assert_equals(tbody.rows.length, 5); +}, "HTMLTableSectionElement insertRow(rows.length)"); + +test(function () { assert_throws("IndexSizeError", function () { tbody.insertRow(-2); }); |