mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Auto merge of #9054 - frewsxcv:parse-non-zero-dimension, r=eefriedman
Implement non-zero dimension attribute parsing Fixes #8445 The only attributes I found that we have implemented that uses non-zero dimenion attributes: * `width` for `<td>` and `<th>` (table cells) * `width` for `<table>` I updated these implementations to use the new non-zero dimension attribute parsing and added associated regression tests. <!-- Reviewable:start --> [<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/9054) <!-- Reviewable:end -->
This commit is contained in:
commit
0d52170f35
13 changed files with 227 additions and 291 deletions
|
@ -3,27 +3,25 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use cssparser::RGBA;
|
||||
use dom::attr::{Attr, AttrValue};
|
||||
use dom::attr::AttrValue;
|
||||
use dom::bindings::codegen::Bindings::HTMLTableCellElementBinding::HTMLTableCellElementMethods;
|
||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::bindings::js::LayoutJS;
|
||||
use dom::document::Document;
|
||||
use dom::element::{AttributeMutation, Element, RawLayoutElementHelpers};
|
||||
use dom::element::{Element, RawLayoutElementHelpers};
|
||||
use dom::htmlelement::HTMLElement;
|
||||
use dom::htmltablerowelement::HTMLTableRowElement;
|
||||
use dom::node::Node;
|
||||
use dom::virtualmethods::VirtualMethods;
|
||||
use std::cell::Cell;
|
||||
use string_cache::Atom;
|
||||
use util::str::{self, DOMString, LengthOrPercentageOrAuto};
|
||||
use util::str::{DOMString, LengthOrPercentageOrAuto};
|
||||
|
||||
const DEFAULT_COLSPAN: u32 = 1;
|
||||
|
||||
#[dom_struct]
|
||||
pub struct HTMLTableCellElement {
|
||||
htmlelement: HTMLElement,
|
||||
width: Cell<LengthOrPercentageOrAuto>,
|
||||
}
|
||||
|
||||
impl HTMLTableCellElement {
|
||||
|
@ -33,7 +31,6 @@ impl HTMLTableCellElement {
|
|||
-> HTMLTableCellElement {
|
||||
HTMLTableCellElement {
|
||||
htmlelement: HTMLElement::new_inherited(tag_name, prefix, document),
|
||||
width: Cell::new(LengthOrPercentageOrAuto::Auto),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,6 +53,12 @@ impl HTMLTableCellElementMethods for HTMLTableCellElement {
|
|||
// https://html.spec.whatwg.org/multipage/#dom-tdth-bgcolor
|
||||
make_legacy_color_setter!(SetBgColor, "bgcolor");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-tdth-width
|
||||
make_getter!(Width, "width");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-tdth-width
|
||||
make_nonzero_dimension_setter!(SetWidth, "width");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-tdth-cellindex
|
||||
fn CellIndex(&self) -> i32 {
|
||||
let self_node = self.upcast::<Node>();
|
||||
|
@ -101,7 +104,11 @@ impl HTMLTableCellElementLayoutHelpers for LayoutJS<HTMLTableCellElement> {
|
|||
|
||||
fn get_width(&self) -> LengthOrPercentageOrAuto {
|
||||
unsafe {
|
||||
(*self.unsafe_get()).width.get()
|
||||
(&*self.upcast::<Element>().unsafe_get())
|
||||
.get_attr_for_layout(&ns!(), &atom!("width"))
|
||||
.map(AttrValue::as_dimension)
|
||||
.cloned()
|
||||
.unwrap_or(LengthOrPercentageOrAuto::Auto)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,23 +118,11 @@ impl VirtualMethods for HTMLTableCellElement {
|
|||
Some(self.upcast::<HTMLElement>() as &VirtualMethods)
|
||||
}
|
||||
|
||||
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
|
||||
self.super_type().unwrap().attribute_mutated(attr, mutation);
|
||||
match *attr.local_name() {
|
||||
atom!("width") => {
|
||||
let width = mutation.new_value(attr).map(|value| {
|
||||
str::parse_length(&value)
|
||||
});
|
||||
self.width.set(width.unwrap_or(LengthOrPercentageOrAuto::Auto));
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_plain_attribute(&self, local_name: &Atom, value: DOMString) -> AttrValue {
|
||||
match *local_name {
|
||||
atom!("colspan") => AttrValue::from_u32(value, DEFAULT_COLSPAN),
|
||||
atom!("bgcolor") => AttrValue::from_legacy_color(value),
|
||||
atom!("width") => AttrValue::from_nonzero_dimension(value),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(local_name, value),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,7 +120,7 @@ impl HTMLTableElementMethods for HTMLTableElement {
|
|||
make_getter!(Width, "width");
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/#dom-table-width
|
||||
make_dimension_setter!(SetWidth, "width");
|
||||
make_nonzero_dimension_setter!(SetWidth, "width");
|
||||
}
|
||||
|
||||
pub trait HTMLTableElementLayoutHelpers {
|
||||
|
@ -195,7 +195,7 @@ impl VirtualMethods for HTMLTableElement {
|
|||
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_dimension(value),
|
||||
atom!("width") => AttrValue::from_nonzero_dimension(value),
|
||||
_ => self.super_type().unwrap().parse_plain_attribute(local_name, value),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -236,6 +236,19 @@ macro_rules! make_dimension_setter(
|
|||
);
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! make_nonzero_dimension_setter(
|
||||
( $attr:ident, $htmlname:tt ) => (
|
||||
fn $attr(&self, value: DOMString) {
|
||||
use dom::bindings::inheritance::Castable;
|
||||
use dom::element::Element;
|
||||
let element = self.upcast::<Element>();
|
||||
let value = AttrValue::from_nonzero_dimension(value);
|
||||
element.set_attribute(&atom!($htmlname), value)
|
||||
}
|
||||
);
|
||||
);
|
||||
|
||||
/// For use on non-jsmanaged types
|
||||
/// Use #[derive(JSTraceable)] on JS managed types
|
||||
macro_rules! no_jsmanaged_fields(
|
||||
|
|
|
@ -19,7 +19,7 @@ partial interface HTMLTableCellElement {
|
|||
// attribute DOMString align;
|
||||
// attribute DOMString axis;
|
||||
// attribute DOMString height;
|
||||
// attribute DOMString width;
|
||||
attribute DOMString width;
|
||||
|
||||
// attribute DOMString ch;
|
||||
// attribute DOMString chOff;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue