Introduce <LayoutDom<Element>>::attrs()

This safe method is the basic block to access element attributes from layout.
We reuse it in the other attr-related layout methods to remove a pretty big
source of rampant unsafe code between script and layout.
This commit is contained in:
Anthony Ramine 2020-03-31 18:46:49 +02:00
parent 0bda174823
commit 5ff931d171
16 changed files with 203 additions and 301 deletions

View file

@ -104,41 +104,32 @@ pub trait HTMLTableCellElementLayoutHelpers {
fn get_width(self) -> LengthOrPercentageOrAuto;
}
#[allow(unsafe_code)]
impl HTMLTableCellElementLayoutHelpers for LayoutDom<'_, HTMLTableCellElement> {
fn get_background_color(self) -> Option<RGBA> {
unsafe {
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
.and_then(AttrValue::as_color)
.cloned()
}
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("bgcolor"))
.and_then(AttrValue::as_color)
.cloned()
}
fn get_colspan(self) -> Option<u32> {
unsafe {
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("colspan"))
.map(AttrValue::as_uint)
}
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("colspan"))
.map(AttrValue::as_uint)
}
fn get_rowspan(self) -> Option<u32> {
unsafe {
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("rowspan"))
.map(AttrValue::as_uint)
}
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("rowspan"))
.map(AttrValue::as_uint)
}
fn get_width(self) -> LengthOrPercentageOrAuto {
unsafe {
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
self.upcast::<Element>()
.get_attr_for_layout(&ns!(), &local_name!("width"))
.map(AttrValue::as_dimension)
.cloned()
.unwrap_or(LengthOrPercentageOrAuto::Auto)
}
}