diff options
author | bors-servo <metajack+bors@gmail.com> | 2015-03-16 09:54:56 -0600 |
---|---|---|
committer | bors-servo <metajack+bors@gmail.com> | 2015-03-16 09:54:56 -0600 |
commit | 660ea05ddb3b17b0bb914cb09968cbcf7c6b1aec (patch) | |
tree | 3ad9c92ccaf65c4dde575fa588654969fedc84bb /components/script | |
parent | 1e1c97adb3cb44351df913daf8265de2edc74704 (diff) | |
parent | 51ceaed0d04783387dd8a99a75c7e4eadc9e50b9 (diff) | |
download | servo-660ea05ddb3b17b0bb914cb09968cbcf7c6b1aec.tar.gz servo-660ea05ddb3b17b0bb914cb09968cbcf7c6b1aec.zip |
auto merge of #5173 : cyndis/servo/media-attr, r=jdm
Don't add a stylesheet if the current device does not match the media
query specified in a link or style tag.
Cheers,
cyndis
Diffstat (limited to 'components/script')
-rw-r--r-- | components/script/dom/htmllinkelement.rs | 20 | ||||
-rw-r--r-- | components/script/dom/htmlstyleelement.rs | 16 | ||||
-rw-r--r-- | components/script/layout_interface.rs | 5 |
3 files changed, 32 insertions, 9 deletions
diff --git a/components/script/dom/htmllinkelement.rs b/components/script/dom/htmllinkelement.rs index 38716553c36..0cf9bf40ae0 100644 --- a/components/script/dom/htmllinkelement.rs +++ b/components/script/dom/htmllinkelement.rs @@ -7,7 +7,7 @@ use dom::attr::AttrHelpers; use dom::bindings::codegen::Bindings::HTMLLinkElementBinding; use dom::bindings::codegen::Bindings::HTMLLinkElementBinding::HTMLLinkElementMethods; use dom::bindings::codegen::InheritTypes::HTMLLinkElementDerived; -use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast}; +use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, NodeCast}; use dom::bindings::js::{MutNullableJS, JSRef, Temporary, OptionalRootable}; use dom::document::Document; use dom::domtokenlist::DOMTokenList; @@ -20,6 +20,9 @@ use dom::virtualmethods::VirtualMethods; use dom::window::WindowHelpers; use layout_interface::{LayoutChan, Msg}; use util::str::{DOMString, HTML_SPACE_CHARACTERS}; +use style::media_queries::parse_media_query_list; +use style::node::TElement; +use cssparser::Parser as CssParser; use std::ascii::AsciiExt; use std::borrow::ToOwned; @@ -80,11 +83,16 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> { s.after_set_attr(attr); } + let node: JSRef<Node> = NodeCast::from_ref(*self); + if !node.is_in_doc() { + return; + } + let element: JSRef<Element> = ElementCast::from_ref(*self); let rel = get_attr(element, &atom!("rel")); match (rel, attr.local_name()) { - (ref rel, &atom!("href")) => { + (ref rel, &atom!("href")) | (ref rel, &atom!("media")) => { if is_stylesheet(rel) { self.handle_stylesheet_url(attr.value().as_slice()); } @@ -131,8 +139,14 @@ impl<'a> PrivateHTMLLinkElementHelpers for JSRef<'a, HTMLLinkElement> { let window = window.r(); match UrlParser::new().base_url(&window.get_url()).parse(href) { Ok(url) => { + let element: JSRef<Element> = ElementCast::from_ref(self); + + let mq_str = element.get_attr(&ns!(""), &atom!("media")).unwrap_or(""); + let mut css_parser = CssParser::new(&mq_str); + let media = parse_media_query_list(&mut css_parser); + let LayoutChan(ref layout_chan) = window.layout_chan(); - layout_chan.send(Msg::LoadStylesheet(url)).unwrap(); + layout_chan.send(Msg::LoadStylesheet(url, media)).unwrap(); } Err(e) => debug!("Parsing url {} failed: {}", href, e) } diff --git a/components/script/dom/htmlstyleelement.rs b/components/script/dom/htmlstyleelement.rs index e3c64c45790..e7eb266b023 100644 --- a/components/script/dom/htmlstyleelement.rs +++ b/components/script/dom/htmlstyleelement.rs @@ -4,11 +4,11 @@ use dom::bindings::codegen::Bindings::HTMLStyleElementBinding; use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods; -use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLStyleElementDerived, NodeCast}; -use dom::bindings::js::{JSRef, Temporary}; +use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, HTMLStyleElementDerived, NodeCast}; +use dom::bindings::js::{JSRef, Temporary, OptionalRootable}; use dom::document::Document; use dom::eventtarget::{EventTarget, EventTargetTypeId}; -use dom::element::ElementTypeId; +use dom::element::{Element, ElementTypeId}; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; use dom::node::{Node, NodeHelpers, NodeTypeId, window_from_node}; use dom::virtualmethods::VirtualMethods; @@ -16,6 +16,9 @@ use dom::window::WindowHelpers; use layout_interface::{LayoutChan, Msg}; use util::str::DOMString; use style::stylesheets::{Origin, Stylesheet}; +use style::media_queries::parse_media_query_list; +use style::node::TElement; +use cssparser::Parser as CssParser; #[dom_struct] pub struct HTMLStyleElement { @@ -49,16 +52,21 @@ pub trait StyleElementHelpers { impl<'a> StyleElementHelpers for JSRef<'a, HTMLStyleElement> { fn parse_own_css(self) { let node: JSRef<Node> = NodeCast::from_ref(self); + let element: JSRef<Element> = ElementCast::from_ref(self); assert!(node.is_in_doc()); let win = window_from_node(node).root(); let win = win.r(); let url = win.get_url(); + let mq_str = element.get_attr(&ns!(""), &atom!("media")).unwrap_or(""); + let mut css_parser = CssParser::new(&mq_str); + let media = parse_media_query_list(&mut css_parser); + let data = node.GetTextContent().expect("Element.textContent must be a string"); let sheet = Stylesheet::from_str(data.as_slice(), url, Origin::Author); let LayoutChan(ref layout_chan) = win.layout_chan(); - layout_chan.send(Msg::AddStylesheet(sheet)).unwrap(); + layout_chan.send(Msg::AddStylesheet(sheet, media)).unwrap(); } } diff --git a/components/script/layout_interface.rs b/components/script/layout_interface.rs index 5b83c471b73..e6e448dd800 100644 --- a/components/script/layout_interface.rs +++ b/components/script/layout_interface.rs @@ -17,6 +17,7 @@ use std::any::Any; use std::sync::mpsc::{channel, Receiver, Sender}; use std::boxed::BoxAny; use style::stylesheets::Stylesheet; +use style::media_queries::MediaQueryList; use url::Url; pub use dom::node::TrustedNodeAddress; @@ -24,10 +25,10 @@ pub use dom::node::TrustedNodeAddress; /// Asynchronous messages that script can send to layout. pub enum Msg { /// Adds the given stylesheet to the document. - AddStylesheet(Stylesheet), + AddStylesheet(Stylesheet, MediaQueryList), /// Adds the given stylesheet to the document. - LoadStylesheet(Url), + LoadStylesheet(Url, MediaQueryList), /// Puts a document into quirks mode, causing the quirks mode stylesheet to be loaded. SetQuirksMode, |