() && n.local_name() == atom)
.and_then(|n| n.downcast().map(Root::from_ref))
}
// https://html.spec.whatwg.org/multipage/#dom-table-thead
// https://html.spec.whatwg.org/multipage/#dom-table-tfoot
fn set_first_section_of_type(&self,
atom: &Atom,
section: Option<&HTMLTableSectionElement>,
reference_predicate: P)
-> ErrorResult
where P: FnMut(&Root) -> bool {
if let Some(e) = section {
if e.upcast::().local_name() != atom {
return Err(Error::HierarchyRequest)
}
}
self.delete_first_section_of_type(atom);
let node = self.upcast::();
if let Some(section) = section {
let reference_element = node.child_elements().find(reference_predicate);
let reference_node = reference_element.r().map(|e| e.upcast());
try!(node.InsertBefore(section.upcast(), reference_node));
}
Ok(())
}
// https://html.spec.whatwg.org/multipage/#dom-table-createthead
// https://html.spec.whatwg.org/multipage/#dom-table-createtfoot
fn create_section_of_type(&self, atom: &Atom) -> Root {
if let Some(section) = self.get_first_section_of_type(atom) {
return section
}
let section = HTMLTableSectionElement::new(atom.clone(),
None,
document_from_node(self).r());
match atom {
&atom!("thead") => self.SetTHead(Some(§ion)),
&atom!("tfoot") => self.SetTFoot(Some(§ion)),
_ => unreachable!("unexpected section type")
}.expect("unexpected section type");
section
}
// https://html.spec.whatwg.org/multipage/#dom-table-deletethead
// https://html.spec.whatwg.org/multipage/#dom-table-deletetfoot
fn delete_first_section_of_type(&self, atom: &Atom) {
if let Some(thead) = self.get_first_section_of_type(atom) {
thead.upcast::().remove_self();
}
}
}
impl HTMLTableElementMethods for HTMLTableElement {
// https://html.spec.whatwg.org/multipage/#dom-table-rows
fn Rows(&self) -> Root {
#[allow(unrooted_must_root)]
#[derive(JSTraceable, HeapSizeOf)]
struct TableRowFilter {
sections: Vec>
}
impl CollectionFilter for TableRowFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
elem.is::()
&& (root.is_parent_of(elem.upcast())
|| self.sections.iter().any(|ref section| section.is_parent_of(elem.upcast())))
}
}
let filter = TableRowFilter {
sections: self.upcast::()
.children()
.filter_map(|ref node|
node.downcast::().map(|_| JS::from_rooted(node)))
.collect()
};
HTMLCollection::new(window_from_node(self).r(), self.upcast(), box filter)
}
// https://html.spec.whatwg.org/multipage/#dom-table-caption
fn GetCaption(&self) -> Option> {
self.upcast::().children().filter_map(Root::downcast).next()
}
// https://html.spec.whatwg.org/multipage/#dom-table-caption
fn SetCaption(&self, new_caption: Option<&HTMLTableCaptionElement>) {
if let Some(ref caption) = self.GetCaption() {
caption.upcast::().remove_self();
}
if let Some(caption) = new_caption {
let node = self.upcast::();
node.InsertBefore(caption.upcast(), node.GetFirstChild().r())
.expect("Insertion failed");
}
}
// https://html.spec.whatwg.org/multipage/#dom-table-createcaption
fn CreateCaption(&self) -> Root {
match self.GetCaption() {
Some(caption) => caption,
None => {
let caption = HTMLTableCaptionElement::new(atom!("caption"),
None,
document_from_node(self).r());
self.SetCaption(Some(caption.r()));
caption
}
}
}
// https://html.spec.whatwg.org/multipage/#dom-table-deletecaption
fn DeleteCaption(&self) {
if let Some(caption) = self.GetCaption() {
caption.upcast::().remove_self();
}
}
// https://html.spec.whatwg.org/multipage/#dom-table-thead
fn GetTHead(&self) -> Option> {
self.get_first_section_of_type(&atom!("thead"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-thead
fn SetTHead(&self, thead: Option<&HTMLTableSectionElement>) -> ErrorResult {
self.set_first_section_of_type(&atom!("thead"), thead, |n| {
!n.is::() && !n.is::()
})
}
// https://html.spec.whatwg.org/multipage/#dom-table-createthead
fn CreateTHead(&self) -> Root {
self.create_section_of_type(&atom!("thead"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-deletethead
fn DeleteTHead(&self) {
self.delete_first_section_of_type(&atom!("thead"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-tfoot
fn GetTFoot(&self) -> Option> {
self.get_first_section_of_type(&atom!("tfoot"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-tfoot
fn SetTFoot(&self, tfoot: Option<&HTMLTableSectionElement>) -> ErrorResult {
self.set_first_section_of_type(&atom!("tfoot"), tfoot, |n| {
if n.is::() || n.is::() {
return false;
}
if n.is::() {
let name = n.local_name();
if name == &atom!("thead") || name == &atom!("tbody") {
return false;
}
}
true
})
}
// https://html.spec.whatwg.org/multipage/#dom-table-createtfoot
fn CreateTFoot(&self) -> Root {
self.create_section_of_type(&atom!("tfoot"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-deletetfoot
fn DeleteTFoot(&self) {
self.delete_first_section_of_type(&atom!("tfoot"))
}
// https://html.spec.whatwg.org/multipage/#dom-table-tbodies
fn TBodies(&self) -> Root {
#[derive(JSTraceable)]
struct TBodiesFilter;
impl CollectionFilter for TBodiesFilter {
fn filter(&self, elem: &Element, root: &Node) -> bool {
elem.is::()
&& elem.local_name() == &atom!("tbody")
&& elem.upcast::().GetParentNode().r() == Some(root)
}
}
self.tbodies.or_init(|| {
let window = window_from_node(self);
let filter = box TBodiesFilter;
HTMLCollection::create(window.r(), self.upcast(), filter)
})
}
// https://html.spec.whatwg.org/multipage/#dom-table-createtbody
fn CreateTBody(&self) -> Root {
let tbody = HTMLTableSectionElement::new(atom!("tbody"),
None,
document_from_node(self).r());
let node = self.upcast::();
let last_tbody =
node.rev_children()
.filter_map(Root::downcast::)
.find(|n| n.is::() && n.local_name() == &atom!("tbody"));
let reference_element =
last_tbody.and_then(|t| t.upcast::().GetNextSibling());
node.InsertBefore(tbody.upcast(), reference_element.r())
.expect("Insertion failed");
tbody
}
// https://html.spec.whatwg.org/multipage/#dom-table-insertrow
fn InsertRow(&self, index: i32) -> Fallible> {
let rows = self.Rows();
let number_of_row_elements = rows.Length();
if index < -1 || index > number_of_row_elements as i32 {
return Err(Error::IndexSize);
}
let new_row = HTMLTableRowElement::new(atom!("tr"),
None,
document_from_node(self).r());
let node = self.upcast::();
if number_of_row_elements == 0 {
// append new row to last or new tbody in table
if let Some(last_tbody) = node.rev_children()
.filter_map(Root::downcast::)
.find(|n| n.is::() && n.local_name() == &atom!("tbody")) {
last_tbody.upcast::().AppendChild(new_row.upcast::())
.expect("InsertRow failed to append first row.");
} else {
let tbody = self.CreateTBody();
node.AppendChild(tbody.upcast())
.expect("InsertRow failed to append new tbody.");
tbody.upcast::().AppendChild(new_row.upcast::())
.expect("InsertRow failed to append first row.");
}
} else if index == number_of_row_elements as i32 || index == -1 {
// append new row to parent of last row in table
let last_row = rows.Item(number_of_row_elements - 1)
.expect("InsertRow failed to find last row in table.");
let last_row_parent =
last_row.upcast::().GetParentNode()
.expect("InsertRow failed to find parent of last row in table.");
last_row_parent.upcast::().AppendChild(new_row.upcast::())
.expect("InsertRow failed to append last row.");
} else {
// insert new row before the index-th row in rows using the same parent
let ith_row = rows.Item(index as u32)
.expect("InsertRow failed to find a row in table.");
let ith_row_parent = ith_row.upcast::().GetParentNode()
.expect("InsertRow failed to find parent of a row in table.");
ith_row_parent.upcast::().InsertBefore(new_row.upcast::(), Some(ith_row.upcast::()))
.expect("InsertRow failed to append row");
}
Ok(new_row)
}
// https://html.spec.whatwg.org/multipage/#dom-table-bgcolor
make_getter!(BgColor, "bgcolor");
// https://html.spec.whatwg.org/multipage/#dom-table-bgcolor
make_legacy_color_setter!(SetBgColor, "bgcolor");
// https://html.spec.whatwg.org/multipage/#dom-table-width
make_getter!(Width, "width");
// https://html.spec.whatwg.org/multipage/#dom-table-width
make_nonzero_dimension_setter!(SetWidth, "width");
}
pub trait HTMLTableElementLayoutHelpers {
fn get_background_color(&self) -> Option;
fn get_border(&self) -> Option;
fn get_cellspacing(&self) -> Option;
fn get_width(&self) -> LengthOrPercentageOrAuto;
}
impl HTMLTableElementLayoutHelpers for LayoutJS {
#[allow(unsafe_code)]
fn get_background_color(&self) -> Option {
unsafe {
(*self.upcast::().unsafe_get())
.get_attr_for_layout(&ns!(), &atom!("bgcolor"))
.and_then(AttrValue::as_color)
.cloned()
}
}
#[allow(unsafe_code)]
fn get_border(&self) -> Option {
unsafe {
(*self.unsafe_get()).border.get()
}
}
#[allow(unsafe_code)]
fn get_cellspacing(&self) -> Option {
unsafe {
(*self.unsafe_get()).cellspacing.get()
}
}
#[allow(unsafe_code)]
fn get_width(&self) -> LengthOrPercentageOrAuto {
unsafe {
(*self.upcast::().unsafe_get())
.get_attr_for_layout(&ns!(), &atom!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}
}
impl VirtualMethods for HTMLTableElement {
fn super_type(&self) -> Option<&VirtualMethods> {
Some(self.upcast::() as &VirtualMethods)
}
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match *attr.local_name() {
atom!("border") => {
// According to HTML5 ยง 14.3.9, invalid values map to 1px.
self.border.set(mutation.new_value(attr).map(|value| {
parse_unsigned_integer(value.chars()).unwrap_or(1)
}));
}
atom!("cellspacing") => {
self.cellspacing.set(mutation.new_value(attr).and_then(|value| {
parse_unsigned_integer(value.chars())
}));
},
_ => {},
}
}
fn parse_plain_attribute(&self, local_name: &Atom, value: DOMString) -> AttrValue {
match *local_name {
atom!("border") => AttrValue::from_u32(value, 1),
atom!("width") => AttrValue::from_nonzero_dimension(value),
atom!("bgcolor") => AttrValue::from_legacy_color(value),
_ => self.super_type().unwrap().parse_plain_attribute(local_name, value),
}
}
}