mirror of
https://github.com/servo/servo.git
synced 2025-07-16 11:53:39 +01:00
Implement deleteRow and insertRow for <table> element
Continued from #6936
This commit is contained in:
parent
02d8894945
commit
3d383f21ae
8 changed files with 81 additions and 77 deletions
|
@ -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>,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue