Skip to content

Commit

Permalink
Implement the HTML background attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
evilpie committed Jun 20, 2015
1 parent 72ead88 commit e44f8f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
15 changes: 14 additions & 1 deletion components/script/dom/element.rs
Expand Up @@ -64,7 +64,7 @@ use style;
use style::legacy::{UnsignedIntegerAttribute, from_declaration};
use style::properties::{PropertyDeclarationBlock, PropertyDeclaration, parse_style_attribute};
use style::properties::DeclaredValue::SpecifiedValue;
use style::properties::longhands::{self, border_spacing, height};
use style::properties::longhands::{self, background_image, border_spacing, height};
use style::values::CSSFloat;
use style::values::specified::{self, CSSColor, CSSRGBA};
use util::geometry::Au;
Expand Down Expand Up @@ -280,6 +280,19 @@ impl RawLayoutElementHelpers for Element {
CSSColor { parsed: Color::RGBA(color), authored: None }))));
}

let background = if self.is_htmlbodyelement() {
let this: &HTMLBodyElement = mem::transmute(self);
this.get_background()
} else {
None
};

if let Some(url) = background {
hints.push(from_declaration(
PropertyDeclaration::BackgroundImage(SpecifiedValue(
background_image::SpecifiedValue(Some(specified::Image::Url(url)))))));
}

let color = if self.is_htmlfontelement() {
let this: &HTMLFontElement = mem::transmute(self);
this.get_color()
Expand Down
22 changes: 21 additions & 1 deletion components/script/dom/htmlbodyelement.rs
Expand Up @@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use dom::attr::{Attr, AttrHelpers};
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;
Expand All @@ -14,14 +15,15 @@ use dom::document::{Document, DocumentHelpers};
use dom::element::ElementTypeId;
use dom::eventtarget::{EventTarget, EventTargetTypeId, EventTargetHelpers};
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::node::{Node, NodeTypeId, window_from_node};
use dom::node::{Node, NodeTypeId, window_from_node, document_from_node};
use dom::virtualmethods::VirtualMethods;
use dom::window::WindowHelpers;
use msg::constellation_msg::ConstellationChan;
use msg::constellation_msg::Msg as ConstellationMsg;

use cssparser::RGBA;
use util::str::{self, DOMString};
use url::{Url, UrlParser};

use std::borrow::ToOwned;
use std::cell::Cell;
Expand All @@ -36,6 +38,7 @@ const INITIAL_REFLOW_DELAY: u64 = 200_000_000;
pub struct HTMLBodyElement {
htmlelement: HTMLElement,
background_color: Cell<Option<RGBA>>,
background: DOMRefCell<Option<Url>>
}

impl HTMLBodyElementDerived for EventTarget {
Expand All @@ -54,6 +57,7 @@ impl HTMLBodyElement {
prefix,
document),
background_color: Cell::new(None),
background: DOMRefCell::new(None)
}
}

Expand Down Expand Up @@ -84,12 +88,20 @@ impl<'a> HTMLBodyElementMethods for &'a HTMLBodyElement {

pub trait HTMLBodyElementHelpers {
fn get_background_color(self) -> Option<RGBA>;
fn get_background(self) -> Option<Url>;
}

impl<'a> HTMLBodyElementHelpers for &'a HTMLBodyElement {
fn get_background_color(self) -> Option<RGBA> {
self.background_color.get()
}

#[allow(unsafe_code)]
fn get_background(self) -> Option<Url> {
unsafe {
self.background.borrow_for_layout().clone()
}
}
}

impl<'a> VirtualMethods for &'a HTMLBodyElement {
Expand Down Expand Up @@ -146,6 +158,14 @@ impl<'a> VirtualMethods for &'a HTMLBodyElement {
&atom!("bgcolor") => {
self.background_color.set(str::parse_legacy_color(&attr.value()).ok())
}
&atom!("background") => {
let doc = document_from_node(*self);
let base = doc.r().url();

if let Ok(url) = UrlParser::new().base_url(&base).parse(&attr.value()) {
*self.background.borrow_mut() = Some(url)
}
}
_ => {}
}
}
Expand Down

0 comments on commit e44f8f5

Please sign in to comment.