servo: Merge #7841 - Implement <body>'s "text" attribute (from frewsxcv:htmlbodyelement-text); r=nox

Source-Repo: https://github.com/servo/servo
Source-Revision: 1029feb55d004e4417dd6cfdcb864d18871ae74a
This commit is contained in:
Corey Farwell 2015-10-09 06:11:46 -06:00
parent abc6fc4ff8
commit 2157cac781
4 changed files with 54 additions and 5 deletions

View File

@ -2,6 +2,7 @@
* 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 cssparser::RGBA;
use devtools_traits::AttrInfo;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods};
@ -28,6 +29,7 @@ pub enum AttrValue {
UInt(DOMString, u32),
Atom(Atom),
Length(DOMString, Option<Length>),
Color(DOMString, Option<RGBA>),
}
impl AttrValue {
@ -98,6 +100,18 @@ impl AttrValue {
}
}
/// Assumes the `AttrValue` is a `Color` and returns its value
///
/// ## Panics
///
/// Panics if the `AttrValue` is not a `Color`
pub fn as_color(&self) -> Option<&RGBA> {
match *self {
AttrValue::Color(_, ref color) => color.as_ref(),
_ => panic!("Color not found"),
}
}
/// Assumes the `AttrValue` is a `Length` and returns its value
///
/// ## Panics
@ -134,7 +148,8 @@ impl Deref for AttrValue {
AttrValue::String(ref value) |
AttrValue::TokenList(ref value, _) |
AttrValue::UInt(ref value, _) |
AttrValue::Length(ref value, _) => &value,
AttrValue::Length(ref value, _) |
AttrValue::Color(ref value, _) => &value,
AttrValue::Atom(ref value) => &value,
}
}

View File

@ -298,6 +298,9 @@ impl LayoutElementHelpers for LayoutJS<Element> {
let color = if let Some(this) = HTMLFontElementCast::to_layout_js(self) {
(*this.unsafe_get()).get_color()
} else if let Some(this) = HTMLBodyElementCast::to_layout_js(self) {
// https://html.spec.whatwg.org/multipage/#the-page:the-body-element-20
(*this.unsafe_get()).get_color()
} else {
None
};

View File

@ -3,17 +3,17 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::RGBA;
use dom::attr::Attr;
use dom::attr::{Attr, AttrValue};
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::HTMLBodyElementBinding::{self, HTMLBodyElementMethods};
use dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
use dom::bindings::codegen::InheritTypes::{EventTargetCast};
use dom::bindings::codegen::InheritTypes::{ElementCast, EventTargetCast};
use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLElementCast};
use dom::bindings::js::Root;
use dom::bindings::utils::Reflectable;
use dom::document::Document;
use dom::element::{AttributeMutation, ElementTypeId};
use dom::element::{AttributeMutation, ElementTypeId, RawLayoutElementHelpers};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeTypeId, document_from_node, window_from_node};
@ -23,6 +23,7 @@ use msg::constellation_msg::Msg as ConstellationMsg;
use std::borrow::ToOwned;
use std::cell::Cell;
use std::rc::Rc;
use string_cache::Atom;
use time;
use url::{Url, UrlParser};
use util::str::{self, DOMString};
@ -73,6 +74,16 @@ impl HTMLBodyElementMethods for HTMLBodyElement {
// https://html.spec.whatwg.org/multipage#dom-body-bgcolor
make_setter!(SetBgColor, "bgcolor");
// https://html.spec.whatwg.org/multipage/#dom-body-text
make_getter!(Text);
// https://html.spec.whatwg.org/multipage/#dom-body-text
fn SetText(&self, value: DOMString) {
let element = ElementCast::from_ref(self);
let color = str::parse_legacy_color(&value).ok();
element.set_attribute(&Atom::from_slice("text"), AttrValue::Color(value, color));
}
// https://html.spec.whatwg.org/multipage/#the-body-element
fn GetOnunload(&self) -> Option<Rc<EventHandlerNonNull>> {
let win = window_from_node(self);
@ -92,6 +103,16 @@ impl HTMLBodyElement {
self.background_color.get()
}
#[allow(unsafe_code)]
pub fn get_color(&self) -> Option<RGBA> {
unsafe {
ElementCast::from_ref(self)
.get_attr_for_layout(&ns!(""), &atom!("text"))
.and_then(AttrValue::as_color)
.cloned()
}
}
#[allow(unsafe_code)]
pub fn get_background(&self) -> Option<Url> {
unsafe {
@ -123,6 +144,16 @@ impl VirtualMethods for HTMLBodyElement {
chan.send(event).unwrap();
}
fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue {
match name {
&atom!("text") => {
let parsed = str::parse_legacy_color(&value).ok();
AttrValue::Color(value, parsed)
},
_ => self.super_type().unwrap().parse_plain_attribute(name, value),
}
}
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match (attr.local_name(), mutation) {

View File

@ -11,7 +11,7 @@ HTMLBodyElement implements WindowEventHandlers;
// https://www.whatwg.org/html/#HTMLBodyElement-partial
partial interface HTMLBodyElement {
//[TreatNullAs=EmptyString] attribute DOMString text;
[TreatNullAs=EmptyString] attribute DOMString text;
//[TreatNullAs=EmptyString] attribute DOMString link;
//[TreatNullAs=EmptyString] attribute DOMString vLink;
//[TreatNullAs=EmptyString] attribute DOMString aLink;