Add DeleteRow method

This commit is contained in:
Guillaume Gomez 2016-07-25 23:10:15 +02:00
parent 2de3b119a9
commit cf9fd7eb46
8 changed files with 106 additions and 37 deletions

View file

@ -34,6 +34,20 @@ pub struct HTMLTableElement {
tbodies: MutNullableHeap<JS<HTMLCollection>>,
}
#[allow(unrooted_must_root)]
#[derive(JSTraceable, HeapSizeOf)]
struct TableRowFilter {
sections: Vec<JS<Node>>,
}
impl CollectionFilter for TableRowFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
elem.is::<HTMLTableRowElement>() &&
(root.is_parent_of(elem.upcast())
|| self.sections.iter().any(|ref section| section.is_parent_of(elem.upcast())))
}
}
impl HTMLTableElement {
fn new_inherited(localName: Atom, prefix: Option<DOMString>, document: &Document)
-> HTMLTableElement {
@ -120,32 +134,22 @@ impl HTMLTableElement {
thead.upcast::<Node>().remove_self();
}
}
}
impl HTMLTableElementMethods for HTMLTableElement {
// https://html.spec.whatwg.org/multipage/#dom-table-rows
fn Rows(&self) -> Root<HTMLCollection> {
#[allow(unrooted_must_root)]
#[derive(JSTraceable, HeapSizeOf)]
struct TableRowFilter {
sections: Vec<JS<Node>>
}
impl CollectionFilter for TableRowFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
elem.is::<HTMLTableRowElement>() &&
(root.is_parent_of(elem.upcast())
|| self.sections.iter().any(|ref section| section.is_parent_of(elem.upcast())))
}
}
let filter = TableRowFilter {
fn get_rows(&self) -> TableRowFilter {
TableRowFilter {
sections: self.upcast::<Node>()
.children()
.filter_map(|ref node|
node.downcast::<HTMLTableSectionElement>().map(|_| JS::from_ref(&**node)))
.collect()
};
}
}
}
impl HTMLTableElementMethods for HTMLTableElement {
// https://html.spec.whatwg.org/multipage/#dom-table-rows
fn Rows(&self) -> Root<HTMLCollection> {
let filter = self.get_rows();
HTMLCollection::new(window_from_node(self).r(), self.upcast(), box filter)
}
@ -338,6 +342,22 @@ impl HTMLTableElementMethods for HTMLTableElement {
Ok(new_row)
}
// https://html.spec.whatwg.org/multipage/#dom-table-deleterow
fn DeleteRow(&self, mut index: i32) -> Fallible<()> {
let rows = self.Rows();
// Step 1.
if index == -1 {
index = rows.Length() as i32 - 1;
}
// Step 2.
if index < 0 || index as u32 >= rows.Length() {
return Err(Error::IndexSize);
}
// Step 3.
Root::upcast::<Node>(rows.Item(index as u32).unwrap()).remove_self();
Ok(())
}
// https://html.spec.whatwg.org/multipage/#dom-table-bgcolor
make_getter!(BgColor, "bgcolor");