style: Parse the legacy bgcolor attribute per the HTML5 specification.

Additionally, this patch cleans up some miscellaneous formatting issues.
This commit is contained in:
Patrick Walton 2014-12-07 22:59:38 -08:00
parent 10f1ed5e31
commit 14bafb11be
19 changed files with 370 additions and 36 deletions

View file

@ -48,7 +48,7 @@ use script_traits::UntrustedNodeAddress;
use servo_msg::compositor_msg::ScriptListener;
use servo_msg::constellation_msg::ConstellationChan;
use servo_util::smallvec::{SmallVec1, SmallVec};
use servo_util::str::LengthOrPercentageOrAuto;
use servo_util::str::{LengthOrPercentageOrAuto, SimpleColor};
use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::comm::{Receiver, Sender};
@ -214,6 +214,7 @@ no_jsmanaged_fields!(LayoutChan)
no_jsmanaged_fields!(WindowProxyHandler)
no_jsmanaged_fields!(UntrustedNodeAddress)
no_jsmanaged_fields!(LengthOrPercentageOrAuto)
no_jsmanaged_fields!(SimpleColor)
impl<'a> JSTraceable for &'a str {
#[inline]

View file

@ -16,9 +16,10 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, ElementDerived, EventTargetCast};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, HTMLInputElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLTableElementCast, HTMLTableElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLTableCellElementDerived, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLInputElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementDerived, HTMLTableElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLTableElementDerived, HTMLTableCellElementDerived};
use dom::bindings::codegen::InheritTypes::{NodeCast};
use dom::bindings::js::{MutNullableJS, JS, JSRef, Temporary, TemporaryPushable};
use dom::bindings::js::{OptionalRootable, Root};
use dom::bindings::utils::{Reflectable, Reflector};
@ -31,6 +32,7 @@ use dom::document::{Document, DocumentHelpers, LayoutDocumentHelpers};
use dom::domtokenlist::DOMTokenList;
use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId, EventTargetHelpers};
use dom::htmlbodyelement::{HTMLBodyElement, HTMLBodyElementHelpers};
use dom::htmlcollection::HTMLCollection;
use dom::htmlinputelement::{HTMLInputElement, RawLayoutHTMLInputElementHelpers};
use dom::htmlserializer::serialize;
@ -42,12 +44,11 @@ use dom::node::{window_from_node};
use dom::nodelist::NodeList;
use dom::virtualmethods::{VirtualMethods, vtable_for};
use devtools_traits::AttrInfo;
use style::{BorderUnsignedIntegerAttribute, IntegerAttribute, LengthAttribute};
use style::{SizeIntegerAttribute, UnsignedIntegerAttribute, WidthLengthAttribute};
use style::{matches, parse_selector_list_from_str};
use style;
use style::{mod, BgColorSimpleColorAttribute, BorderUnsignedIntegerAttribute, IntegerAttribute};
use style::{LengthAttribute, SimpleColorAttribute, SizeIntegerAttribute, UnsignedIntegerAttribute};
use style::{WidthLengthAttribute, matches, parse_selector_list_from_str};
use servo_util::namespace;
use servo_util::str::{DOMString, LengthOrPercentageOrAuto};
use servo_util::str::{DOMString, LengthOrPercentageOrAuto, SimpleColor};
use std::ascii::AsciiExt;
use std::cell::{Ref, RefMut};
@ -207,6 +208,8 @@ pub trait RawLayoutElementHelpers {
unsafe fn get_checked_state_for_layout(&self) -> bool;
unsafe fn get_unsigned_integer_attribute_for_layout(&self, attribute: UnsignedIntegerAttribute)
-> Option<u32>;
unsafe fn get_simple_color_attribute_for_layout(&self, attribute: SimpleColorAttribute)
-> Option<SimpleColor>;
fn local_name<'a>(&'a self) -> &'a Atom;
fn namespace<'a>(&'a self) -> &'a Namespace;
fn style_attribute<'a>(&'a self) -> &'a DOMRefCell<Option<style::PropertyDeclarationBlock>>;
@ -349,6 +352,28 @@ impl RawLayoutElementHelpers for Element {
}
}
#[inline]
#[allow(unrooted_must_root)]
unsafe fn get_simple_color_attribute_for_layout(&self, attribute: SimpleColorAttribute)
-> Option<SimpleColor> {
match attribute {
BgColorSimpleColorAttribute => {
if self.is_htmlbodyelement() {
let this: &HTMLBodyElement = mem::transmute(self);
this.get_background_color()
} else if self.is_htmltableelement() {
let this: &HTMLTableElement = mem::transmute(self);
this.get_background_color()
} else if self.is_htmltablecellelement() {
let this: &HTMLTableCellElement = mem::transmute(self);
this.get_background_color()
} else {
panic!("I'm not a body, table, or table cell!")
}
}
}
}
// Getters used in components/layout/wrapper.rs
fn local_name<'a>(&'a self) -> &'a Atom {

View file

@ -2,11 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::attr::Attr;
use dom::attr::AttrHelpers;
use dom::attr::{Attr, AttrHelpers};
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding::HTMLBodyElementMethods;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding::{mod, HTMLBodyElementMethods};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::EventTargetCast;
use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLElementCast};
@ -19,11 +17,13 @@ use dom::htmlelement::HTMLElement;
use dom::node::{Node, ElementNodeTypeId, window_from_node};
use dom::virtualmethods::VirtualMethods;
use servo_util::str::DOMString;
use servo_util::str::{mod, DOMString, SimpleColor};
use std::cell::Cell;
#[dom_struct]
pub struct HTMLBodyElement {
htmlelement: HTMLElement
htmlelement: HTMLElement,
background_color: Cell<Option<SimpleColor>>,
}
impl HTMLBodyElementDerived for EventTarget {
@ -33,14 +33,20 @@ impl HTMLBodyElementDerived for EventTarget {
}
impl HTMLBodyElement {
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: JSRef<Document>) -> HTMLBodyElement {
fn new_inherited(localName: DOMString, prefix: Option<DOMString>, document: JSRef<Document>)
-> HTMLBodyElement {
HTMLBodyElement {
htmlelement: HTMLElement::new_inherited(HTMLBodyElementTypeId, localName, prefix, document)
htmlelement: HTMLElement::new_inherited(HTMLBodyElementTypeId,
localName,
prefix,
document),
background_color: Cell::new(None),
}
}
#[allow(unrooted_must_root)]
pub fn new(localName: DOMString, prefix: Option<DOMString>, document: JSRef<Document>) -> Temporary<HTMLBodyElement> {
pub fn new(localName: DOMString, prefix: Option<DOMString>, document: JSRef<Document>)
-> Temporary<HTMLBodyElement> {
let element = HTMLBodyElement::new_inherited(localName, prefix, document);
Node::reflect_node(box element, document, HTMLBodyElementBinding::Wrap)
}
@ -58,6 +64,16 @@ impl<'a> HTMLBodyElementMethods for JSRef<'a, HTMLBodyElement> {
}
}
pub trait HTMLBodyElementHelpers {
fn get_background_color(&self) -> Option<SimpleColor>;
}
impl HTMLBodyElementHelpers for HTMLBodyElement {
fn get_background_color(&self) -> Option<SimpleColor> {
self.background_color.get()
}
}
impl<'a> VirtualMethods for JSRef<'a, HTMLBodyElement> {
fn super_type<'a>(&'a self) -> Option<&'a VirtualMethods> {
let element: &JSRef<HTMLElement> = HTMLElementCast::from_borrowed_ref(self);
@ -91,6 +107,25 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLBodyElement> {
name.slice_from(2),
attr.value().as_slice().to_string());
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(attr.value().as_slice()).ok())
}
_ => {}
}
}
fn before_remove_attr(&self, attr: JSRef<Attr>) {
match self.super_type() {
Some(ref s) => s.before_remove_attr(attr),
_ => {}
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
_ => {}
}
}
}

View file

@ -14,13 +14,13 @@ use dom::htmlelement::HTMLElement;
use dom::node::ElementNodeTypeId;
use dom::virtualmethods::VirtualMethods;
use servo_util::str::{AutoLpa, DOMString, LengthOrPercentageOrAuto};
use servo_util::str;
use servo_util::str::{mod, AutoLpa, DOMString, LengthOrPercentageOrAuto, SimpleColor};
use std::cell::Cell;
#[dom_struct]
pub struct HTMLTableCellElement {
htmlelement: HTMLElement,
background_color: Cell<Option<SimpleColor>>,
border: Cell<Option<u32>>,
width: Cell<LengthOrPercentageOrAuto>,
}
@ -43,6 +43,7 @@ impl HTMLTableCellElement {
-> HTMLTableCellElement {
HTMLTableCellElement {
htmlelement: HTMLElement::new_inherited(type_id, tag_name, prefix, document),
background_color: Cell::new(None),
border: Cell::new(None),
width: Cell::new(AutoLpa),
}
@ -55,11 +56,16 @@ impl HTMLTableCellElement {
}
pub trait HTMLTableCellElementHelpers {
fn get_background_color(&self) -> Option<SimpleColor>;
fn get_border(&self) -> Option<u32>;
fn get_width(&self) -> LengthOrPercentageOrAuto;
}
impl HTMLTableCellElementHelpers for HTMLTableCellElement {
fn get_background_color(&self) -> Option<SimpleColor> {
self.background_color.get()
}
fn get_border(&self) -> Option<u32> {
self.border.get()
}
@ -82,6 +88,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableCellElement> {
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(attr.value().as_slice()).ok())
}
&atom!("border") => {
// According to HTML5 § 14.3.9, invalid values map to 1px.
self.border.set(Some(str::parse_unsigned_integer(attr.value()
@ -100,6 +109,7 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableCellElement> {
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
&atom!("border") => self.border.set(None),
&atom!("width") => self.width.set(AutoLpa),
_ => ()

View file

@ -18,12 +18,13 @@ use dom::htmltablecaptionelement::HTMLTableCaptionElement;
use dom::node::{Node, NodeHelpers, ElementNodeTypeId};
use dom::virtualmethods::VirtualMethods;
use servo_util::str::{mod, AutoLpa, DOMString, LengthOrPercentageOrAuto};
use servo_util::str::{mod, AutoLpa, DOMString, LengthOrPercentageOrAuto, SimpleColor};
use std::cell::Cell;
#[dom_struct]
pub struct HTMLTableElement {
htmlelement: HTMLElement,
background_color: Cell<Option<SimpleColor>>,
border: Cell<Option<u32>>,
width: Cell<LengthOrPercentageOrAuto>,
}
@ -42,6 +43,7 @@ impl HTMLTableElement {
localName,
prefix,
document),
background_color: Cell::new(None),
border: Cell::new(None),
width: Cell::new(AutoLpa),
}
@ -93,11 +95,16 @@ impl<'a> HTMLTableElementMethods for JSRef<'a, HTMLTableElement> {
}
pub trait HTMLTableElementHelpers {
fn get_background_color(&self) -> Option<SimpleColor>;
fn get_border(&self) -> Option<u32>;
fn get_width(&self) -> LengthOrPercentageOrAuto;
}
impl HTMLTableElementHelpers for HTMLTableElement {
fn get_background_color(&self) -> Option<SimpleColor> {
self.background_color.get()
}
fn get_border(&self) -> Option<u32> {
self.border.get()
}
@ -119,6 +126,9 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableElement> {
}
match attr.local_name() {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(attr.value().as_slice()).ok())
}
&atom!("border") => {
// According to HTML5 § 14.3.9, invalid values map to 1px.
self.border.set(Some(str::parse_unsigned_integer(attr.value()
@ -136,6 +146,7 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTableElement> {
}
match attr.local_name() {
&atom!("bgcolor") => self.background_color.set(None),
&atom!("border") => self.border.set(None),
_ => ()
}