From 2157cac781c8a0ea3d1f195a7cad7f1454078bc4 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Fri, 9 Oct 2015 06:11:46 -0600 Subject: [PATCH] servo: Merge #7841 - Implement 's "text" attribute (from frewsxcv:htmlbodyelement-text); r=nox Source-Repo: https://github.com/servo/servo Source-Revision: 1029feb55d004e4417dd6cfdcb864d18871ae74a --- servo/components/script/dom/attr.rs | 17 ++++++++- servo/components/script/dom/element.rs | 3 ++ .../components/script/dom/htmlbodyelement.rs | 37 +++++++++++++++++-- .../script/dom/webidls/HTMLBodyElement.webidl | 2 +- 4 files changed, 54 insertions(+), 5 deletions(-) diff --git a/servo/components/script/dom/attr.rs b/servo/components/script/dom/attr.rs index bebe74935aa6..c597a0a4204e 100644 --- a/servo/components/script/dom/attr.rs +++ b/servo/components/script/dom/attr.rs @@ -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), + Color(DOMString, Option), } 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, } } diff --git a/servo/components/script/dom/element.rs b/servo/components/script/dom/element.rs index b10f5c3f2723..39633b64fef1 100644 --- a/servo/components/script/dom/element.rs +++ b/servo/components/script/dom/element.rs @@ -298,6 +298,9 @@ impl LayoutElementHelpers for LayoutJS { 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 }; diff --git a/servo/components/script/dom/htmlbodyelement.rs b/servo/components/script/dom/htmlbodyelement.rs index a86e1375453e..6dc5bcdcd83d 100644 --- a/servo/components/script/dom/htmlbodyelement.rs +++ b/servo/components/script/dom/htmlbodyelement.rs @@ -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> { 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 { + 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 { 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) { diff --git a/servo/components/script/dom/webidls/HTMLBodyElement.webidl b/servo/components/script/dom/webidls/HTMLBodyElement.webidl index f68f51e866e0..baf9a106e2a4 100644 --- a/servo/components/script/dom/webidls/HTMLBodyElement.webidl +++ b/servo/components/script/dom/webidls/HTMLBodyElement.webidl @@ -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;