fn set_first_section_of_type(
&self,
cx: &mut JSContext,
atom: &LocalName,
section: Option<&HTMLTableSectionElement>,
reference_predicate: P,
) -> ErrorResult
where
P: FnMut(&DomRoot) -> bool,
{
if let Some(e) = section {
if e.upcast::().local_name() != atom {
return Err(Error::HierarchyRequest(None));
}
}
self.delete_first_section_of_type(cx, atom);
let node = self.upcast::();
if let Some(section) = section {
let reference_element = node.child_elements().find(reference_predicate);
let reference_node = reference_element.as_ref().map(|e| e.upcast());
node.InsertBefore(cx, section.upcast(), reference_node)?;
}
Ok(())
}
///
///
fn create_section_of_type(
&self,
cx: &mut JSContext,
atom: &LocalName,
) -> DomRoot {
if let Some(section) = self.get_first_section_of_type(atom) {
return section;
}
let section = Element::create(
cx,
QualName::new(None, ns!(html), atom.clone()),
None,
&self.owner_document(),
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
);
let section = DomRoot::downcast::(section).unwrap();
match *atom {
local_name!("thead") => self.SetTHead(cx, Some(§ion)),
local_name!("tfoot") => self.SetTFoot(cx, Some(§ion)),
_ => unreachable!("unexpected section type"),
}
.expect("unexpected section type");
section
}
///
///
fn delete_first_section_of_type(&self, cx: &mut JSContext, atom: &LocalName) {
if let Some(thead) = self.get_first_section_of_type(atom) {
thead.upcast::().remove_self(cx);
}
}
fn get_rows(&self) -> TableRowFilter {
TableRowFilter {
sections: self
.upcast::()
.children()
.filter_map(|ref node| {
node.downcast::()
.map(|_| Dom::from_ref(&**node))
})
.collect(),
}
}
}
impl HTMLTableElementMethods for HTMLTableElement {
///
fn Rows(&self) -> DomRoot {
let filter = self.get_rows();
HTMLCollection::new(
&self.owner_window(),
self.upcast(),
Box::new(filter),
CanGc::deprecated_note(),
)
}
///
fn GetCaption(&self) -> Option> {
self.upcast::().children().find_map(DomRoot::downcast)
}
///
fn SetCaption(
&self,
cx: &mut JSContext,
new_caption: Option<&HTMLTableCaptionElement>,
) -> Fallible<()> {
if let Some(ref caption) = self.GetCaption() {
caption.upcast::().remove_self(cx);
}
if let Some(caption) = new_caption {
let node = self.upcast::();
node.InsertBefore(cx, caption.upcast(), node.GetFirstChild().as_deref())?;
}
Ok(())
}
///
fn CreateCaption(&self, cx: &mut JSContext) -> DomRoot {
match self.GetCaption() {
Some(caption) => caption,
None => {
let caption = Element::create(
cx,
QualName::new(None, ns!(html), local_name!("caption")),
None,
&self.owner_document(),
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
);
let caption = DomRoot::downcast::(caption).unwrap();
self.SetCaption(cx, Some(&caption))
.expect("Generated caption is invalid");
caption
},
}
}
///
fn DeleteCaption(&self, cx: &mut JSContext) {
if let Some(caption) = self.GetCaption() {
caption.upcast::().remove_self(cx);
}
}
///
fn GetTHead(&self) -> Option> {
self.get_first_section_of_type(&local_name!("thead"))
}
///
fn SetTHead(&self, cx: &mut JSContext, thead: Option<&HTMLTableSectionElement>) -> ErrorResult {
self.set_first_section_of_type(cx, &local_name!("thead"), thead, |n| {
!n.is::() && !n.is::()
})
}
///
fn CreateTHead(&self, cx: &mut JSContext) -> DomRoot {
self.create_section_of_type(cx, &local_name!("thead"))
}
///
fn DeleteTHead(&self, cx: &mut JSContext) {
self.delete_first_section_of_type(cx, &local_name!("thead"))
}
///
fn GetTFoot(&self) -> Option> {
self.get_first_section_of_type(&local_name!("tfoot"))
}
///
fn SetTFoot(&self, cx: &mut JSContext, tfoot: Option<&HTMLTableSectionElement>) -> ErrorResult {
self.set_first_section_of_type(cx, &local_name!("tfoot"), tfoot, |n| {
if n.is::() || n.is::() {
return false;
}
if n.is::() {
let name = n.local_name();
if name == &local_name!("thead") || name == &local_name!("tbody") {
return false;
}
}
true
})
}
///
fn CreateTFoot(&self, cx: &mut JSContext) -> DomRoot {
self.create_section_of_type(cx, &local_name!("tfoot"))
}
///
fn DeleteTFoot(&self, cx: &mut JSContext) {
self.delete_first_section_of_type(cx, &local_name!("tfoot"))
}
///
fn TBodies(&self) -> DomRoot {
self.tbodies.or_init(|| {
HTMLCollection::new_with_filter_fn(
&self.owner_window(),
self.upcast(),
|element, root| {
element.is::() &&
element.local_name() == &local_name!("tbody") &&
element.upcast::().GetParentNode().as_deref() == Some(root)
},
CanGc::deprecated_note(),
)
})
}
///
fn CreateTBody(&self, cx: &mut JSContext) -> DomRoot {
let tbody = Element::create(
cx,
QualName::new(None, ns!(html), local_name!("tbody")),
None,
&self.owner_document(),
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
);
let tbody = DomRoot::downcast::(tbody).unwrap();
let node = self.upcast::();
let last_tbody = node
.rev_children()
.filter_map(DomRoot::downcast::)
.find(|n| n.is::() && n.local_name() == &local_name!("tbody"));
let reference_element = last_tbody.and_then(|t| t.upcast::().GetNextSibling());
node.InsertBefore(cx, tbody.upcast(), reference_element.as_deref())
.expect("Insertion failed");
tbody
}
///
fn InsertRow(&self, cx: &mut JSContext, 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(None));
}
let new_row = Element::create(
cx,
QualName::new(None, ns!(html), local_name!("tr")),
None,
&self.owner_document(),
ElementCreator::ScriptCreated,
CustomElementCreationMode::Asynchronous,
None,
);
let new_row = DomRoot::downcast::(new_row).unwrap();
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(DomRoot::downcast::)
.find(|n| {
n.is::() && n.local_name() == &local_name!("tbody")
})
{
last_tbody
.upcast::()
.AppendChild(cx, new_row.upcast::())
.expect("InsertRow failed to append first row.");
} else {
let tbody = self.CreateTBody(cx);
node.AppendChild(cx, tbody.upcast())
.expect("InsertRow failed to append new tbody.");
tbody
.upcast::()
.AppendChild(cx, 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(cx, 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(cx, new_row.upcast::(), Some(ith_row.upcast::()))
.expect("InsertRow failed to append row");
}
Ok(new_row)
}
///
fn DeleteRow(&self, cx: &mut JSContext, mut index: i32) -> Fallible<()> {
let rows = self.Rows();
let num_rows = rows.Length() as i32;
// Step 1: If index is less than −1 or greater than or equal to the number of elements
// in the rows collection, then throw an "IndexSizeError".
if !(-1..num_rows).contains(&index) {
return Err(Error::IndexSize(None));
}
let num_rows = rows.Length() as i32;
// Step 2: If index is −1, then remove the last element in the rows collection from its
// parent, or do nothing if the rows collection is empty.
if index == -1 {
index = num_rows - 1;
}
if num_rows == 0 {
return Ok(());
}
// Step 3: Otherwise, remove the indexth element in the rows collection from its parent.
DomRoot::upcast::(rows.Item(index as u32).unwrap()).remove_self(cx);
Ok(())
}
// 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");
//
make_getter!(Width, "width");
//
make_nonzero_dimension_setter!(SetWidth, "width");
//
make_setter!(cx, SetAlign, "align");
make_getter!(Align, "align");
//
make_setter!(cx, SetCellPadding, "cellpadding");
make_getter!(CellPadding, "cellpadding");
//
make_setter!(cx, SetCellSpacing, "cellspacing");
make_getter!(CellSpacing, "cellspacing");
}
impl LayoutDom<'_, HTMLTableElement> {
pub(crate) fn get_background_color(self) -> Option {
self.upcast::()
.get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
.and_then(AttrValue::as_color)
.cloned()
}
pub(crate) fn get_border(self) -> Option {
(self.unsafe_get()).border.get()
}
pub(crate) fn get_cellpadding(self) -> Option {
(self.unsafe_get()).cellpadding.get()
}
pub(crate) fn get_cellspacing(self) -> Option {
(self.unsafe_get()).cellspacing.get()
}
pub(crate) fn get_width(self) -> LengthOrPercentageOrAuto {
self.upcast::()
.get_attr_for_layout(&ns!(), &local_name!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
pub(crate) fn get_height(self) -> LengthOrPercentageOrAuto {
self.upcast::()
.get_attr_for_layout(&ns!(), &local_name!("height"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}
impl VirtualMethods for HTMLTableElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::() as &dyn VirtualMethods)
}
fn attribute_mutated(
&self,
cx: &mut js::context::JSContext,
attr: &Attr,
mutation: AttributeMutation,
) {
self.super_type()
.unwrap()
.attribute_mutated(cx, attr, mutation);
match *attr.local_name() {
local_name!("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)),
);
},
local_name!("cellpadding") => {
self.cellpadding.set(
mutation
.new_value(attr)
.and_then(|value| parse_unsigned_integer(value.chars()).ok()),
);
},
local_name!("cellspacing") => {
self.cellspacing.set(
mutation
.new_value(attr)
.and_then(|value| parse_unsigned_integer(value.chars()).ok()),
);
},
_ => {},
}
}
fn attribute_affects_presentational_hints(&self, attr: &Attr) -> bool {
match attr.local_name() {
&local_name!("width") | &local_name!("height") => true,
_ => self
.super_type()
.unwrap()
.attribute_affects_presentational_hints(attr),
}
}
fn parse_plain_attribute(&self, local_name: &LocalName, value: DOMString) -> AttrValue {
match *local_name {
local_name!("border") => AttrValue::from_u32(value.into(), 1),
local_name!("width") => AttrValue::from_nonzero_dimension(value.into()),
local_name!("height") => AttrValue::from_dimension(value.into()),
local_name!("bgcolor") => AttrValue::from_legacy_color(value.into()),
_ => self
.super_type()
.unwrap()
.parse_plain_attribute(local_name, value),
}
}
}