aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmltablesectionelement.rs
diff options
context:
space:
mode:
authorCorey Farwell <coreyf@rwell.org>2015-10-04 17:17:45 -0400
committerCorey Farwell <coreyf@rwell.org>2015-10-11 09:47:46 -0400
commit3d383f21ae416e528832b8ebf48b060b2c3105e8 (patch)
tree4e8b3257eca0dfd950c3ae1ed887cda35fcf04b1 /components/script/dom/htmltablesectionelement.rs
parent02d889494580d2b387a42d3a94360e40dce96282 (diff)
downloadservo-3d383f21ae416e528832b8ebf48b060b2c3105e8.tar.gz
servo-3d383f21ae416e528832b8ebf48b060b2c3105e8.zip
Implement deleteRow and insertRow for <table> element
Continued from #6936
Diffstat (limited to 'components/script/dom/htmltablesectionelement.rs')
-rw-r--r--components/script/dom/htmltablesectionelement.rs61
1 files changed, 61 insertions, 0 deletions
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 {