mirror of
https://github.com/servo/servo.git
synced 2025-07-23 15:23:42 +01:00
Consider media attribute on link and style tags
Don't add a stylesheet if the current device does not match the media query specified in a link or style tag.
This commit is contained in:
parent
1e1c97adb3
commit
e7a06cd241
4 changed files with 48 additions and 17 deletions
|
@ -73,7 +73,7 @@ use style::selector_matching::Stylist;
|
||||||
use style::computed_values::{filter, mix_blend_mode};
|
use style::computed_values::{filter, mix_blend_mode};
|
||||||
use style::stylesheets::{Origin, Stylesheet, iter_font_face_rules};
|
use style::stylesheets::{Origin, Stylesheet, iter_font_face_rules};
|
||||||
use style::node::TNode;
|
use style::node::TNode;
|
||||||
use style::media_queries::{MediaType, Device};
|
use style::media_queries::{MediaType, MediaQueryList, Device};
|
||||||
use std::sync::{Arc, Mutex, MutexGuard};
|
use std::sync::{Arc, Mutex, MutexGuard};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
@ -405,8 +405,8 @@ impl LayoutTask {
|
||||||
LayoutTaskData>>)
|
LayoutTaskData>>)
|
||||||
-> bool {
|
-> bool {
|
||||||
match request {
|
match request {
|
||||||
Msg::AddStylesheet(sheet) => self.handle_add_stylesheet(sheet, possibly_locked_rw_data),
|
Msg::AddStylesheet(sheet, mq) => self.handle_add_stylesheet(sheet, mq, possibly_locked_rw_data),
|
||||||
Msg::LoadStylesheet(url) => self.handle_load_stylesheet(url, possibly_locked_rw_data),
|
Msg::LoadStylesheet(url, mq) => self.handle_load_stylesheet(url, mq, possibly_locked_rw_data),
|
||||||
Msg::SetQuirksMode => self.handle_set_quirks_mode(possibly_locked_rw_data),
|
Msg::SetQuirksMode => self.handle_set_quirks_mode(possibly_locked_rw_data),
|
||||||
Msg::GetRPC(response_chan) => {
|
Msg::GetRPC(response_chan) => {
|
||||||
response_chan.send(box LayoutRPCImpl(self.rw_data.clone()) as
|
response_chan.send(box LayoutRPCImpl(self.rw_data.clone()) as
|
||||||
|
@ -487,11 +487,13 @@ impl LayoutTask {
|
||||||
|
|
||||||
fn handle_load_stylesheet<'a>(&'a self,
|
fn handle_load_stylesheet<'a>(&'a self,
|
||||||
url: Url,
|
url: Url,
|
||||||
|
mq: MediaQueryList,
|
||||||
possibly_locked_rw_data:
|
possibly_locked_rw_data:
|
||||||
&mut Option<MutexGuard<'a, LayoutTaskData>>) {
|
&mut Option<MutexGuard<'a, LayoutTaskData>>) {
|
||||||
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
|
// TODO: Get the actual value. http://dev.w3.org/csswg/css-syntax/#environment-encoding
|
||||||
let environment_encoding = UTF_8 as EncodingRef;
|
let environment_encoding = UTF_8 as EncodingRef;
|
||||||
|
|
||||||
|
// TODO we don't really even need to load this if mq does not match
|
||||||
let (metadata, iter) = load_bytes_iter(&self.resource_task, url);
|
let (metadata, iter) = load_bytes_iter(&self.resource_task, url);
|
||||||
let protocol_encoding_label = metadata.charset.as_ref().map(|s| s.as_slice());
|
let protocol_encoding_label = metadata.charset.as_ref().map(|s| s.as_slice());
|
||||||
let final_url = metadata.final_url;
|
let final_url = metadata.final_url;
|
||||||
|
@ -501,20 +503,26 @@ impl LayoutTask {
|
||||||
protocol_encoding_label,
|
protocol_encoding_label,
|
||||||
Some(environment_encoding),
|
Some(environment_encoding),
|
||||||
Origin::Author);
|
Origin::Author);
|
||||||
self.handle_add_stylesheet(sheet, possibly_locked_rw_data);
|
self.handle_add_stylesheet(sheet, mq, possibly_locked_rw_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_add_stylesheet<'a>(&'a self,
|
fn handle_add_stylesheet<'a>(&'a self,
|
||||||
sheet: Stylesheet,
|
sheet: Stylesheet,
|
||||||
|
mq: MediaQueryList,
|
||||||
possibly_locked_rw_data:
|
possibly_locked_rw_data:
|
||||||
&mut Option<MutexGuard<'a, LayoutTaskData>>) {
|
&mut Option<MutexGuard<'a, LayoutTaskData>>) {
|
||||||
// Find all font-face rules and notify the font cache of them.
|
// Find all font-face rules and notify the font cache of them.
|
||||||
// GWTODO: Need to handle unloading web fonts (when we handle unloading stylesheets!)
|
// GWTODO: Need to handle unloading web fonts (when we handle unloading stylesheets!)
|
||||||
|
|
||||||
let mut rw_data = self.lock_rw_data(possibly_locked_rw_data);
|
let mut rw_data = self.lock_rw_data(possibly_locked_rw_data);
|
||||||
iter_font_face_rules(&sheet, &rw_data.stylist.device, &|&:family, src| {
|
|
||||||
self.font_cache_task.add_web_font(family.to_owned(), (*src).clone());
|
if mq.evaluate(&rw_data.stylist.device) {
|
||||||
});
|
iter_font_face_rules(&sheet, &rw_data.stylist.device, &|&:family, src| {
|
||||||
rw_data.stylist.add_stylesheet(sheet);
|
self.font_cache_task.add_web_font(family.to_owned(), (*src).clone());
|
||||||
|
});
|
||||||
|
rw_data.stylist.add_stylesheet(sheet);
|
||||||
|
}
|
||||||
|
|
||||||
LayoutTask::return_rw_data(possibly_locked_rw_data, rw_data);
|
LayoutTask::return_rw_data(possibly_locked_rw_data, rw_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ use dom::attr::AttrHelpers;
|
||||||
use dom::bindings::codegen::Bindings::HTMLLinkElementBinding;
|
use dom::bindings::codegen::Bindings::HTMLLinkElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::HTMLLinkElementBinding::HTMLLinkElementMethods;
|
use dom::bindings::codegen::Bindings::HTMLLinkElementBinding::HTMLLinkElementMethods;
|
||||||
use dom::bindings::codegen::InheritTypes::HTMLLinkElementDerived;
|
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::bindings::js::{MutNullableJS, JSRef, Temporary, OptionalRootable};
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::domtokenlist::DOMTokenList;
|
use dom::domtokenlist::DOMTokenList;
|
||||||
|
@ -20,6 +20,9 @@ use dom::virtualmethods::VirtualMethods;
|
||||||
use dom::window::WindowHelpers;
|
use dom::window::WindowHelpers;
|
||||||
use layout_interface::{LayoutChan, Msg};
|
use layout_interface::{LayoutChan, Msg};
|
||||||
use util::str::{DOMString, HTML_SPACE_CHARACTERS};
|
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::ascii::AsciiExt;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
|
@ -80,11 +83,16 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLLinkElement> {
|
||||||
s.after_set_attr(attr);
|
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 element: JSRef<Element> = ElementCast::from_ref(*self);
|
||||||
let rel = get_attr(element, &atom!("rel"));
|
let rel = get_attr(element, &atom!("rel"));
|
||||||
|
|
||||||
match (rel, attr.local_name()) {
|
match (rel, attr.local_name()) {
|
||||||
(ref rel, &atom!("href")) => {
|
(ref rel, &atom!("href")) | (ref rel, &atom!("media")) => {
|
||||||
if is_stylesheet(rel) {
|
if is_stylesheet(rel) {
|
||||||
self.handle_stylesheet_url(attr.value().as_slice());
|
self.handle_stylesheet_url(attr.value().as_slice());
|
||||||
}
|
}
|
||||||
|
@ -131,8 +139,14 @@ impl<'a> PrivateHTMLLinkElementHelpers for JSRef<'a, HTMLLinkElement> {
|
||||||
let window = window.r();
|
let window = window.r();
|
||||||
match UrlParser::new().base_url(&window.get_url()).parse(href) {
|
match UrlParser::new().base_url(&window.get_url()).parse(href) {
|
||||||
Ok(url) => {
|
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();
|
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)
|
Err(e) => debug!("Parsing url {} failed: {}", href, e)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
use dom::bindings::codegen::Bindings::HTMLStyleElementBinding;
|
use dom::bindings::codegen::Bindings::HTMLStyleElementBinding;
|
||||||
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
|
||||||
use dom::bindings::codegen::InheritTypes::{HTMLElementCast, HTMLStyleElementDerived, NodeCast};
|
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, HTMLStyleElementDerived, NodeCast};
|
||||||
use dom::bindings::js::{JSRef, Temporary};
|
use dom::bindings::js::{JSRef, Temporary, OptionalRootable};
|
||||||
use dom::document::Document;
|
use dom::document::Document;
|
||||||
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
use dom::eventtarget::{EventTarget, EventTargetTypeId};
|
||||||
use dom::element::ElementTypeId;
|
use dom::element::{Element, ElementTypeId};
|
||||||
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
|
||||||
use dom::node::{Node, NodeHelpers, NodeTypeId, window_from_node};
|
use dom::node::{Node, NodeHelpers, NodeTypeId, window_from_node};
|
||||||
use dom::virtualmethods::VirtualMethods;
|
use dom::virtualmethods::VirtualMethods;
|
||||||
|
@ -16,6 +16,9 @@ use dom::window::WindowHelpers;
|
||||||
use layout_interface::{LayoutChan, Msg};
|
use layout_interface::{LayoutChan, Msg};
|
||||||
use util::str::DOMString;
|
use util::str::DOMString;
|
||||||
use style::stylesheets::{Origin, Stylesheet};
|
use style::stylesheets::{Origin, Stylesheet};
|
||||||
|
use style::media_queries::parse_media_query_list;
|
||||||
|
use style::node::TElement;
|
||||||
|
use cssparser::Parser as CssParser;
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct HTMLStyleElement {
|
pub struct HTMLStyleElement {
|
||||||
|
@ -49,16 +52,21 @@ pub trait StyleElementHelpers {
|
||||||
impl<'a> StyleElementHelpers for JSRef<'a, HTMLStyleElement> {
|
impl<'a> StyleElementHelpers for JSRef<'a, HTMLStyleElement> {
|
||||||
fn parse_own_css(self) {
|
fn parse_own_css(self) {
|
||||||
let node: JSRef<Node> = NodeCast::from_ref(self);
|
let node: JSRef<Node> = NodeCast::from_ref(self);
|
||||||
|
let element: JSRef<Element> = ElementCast::from_ref(self);
|
||||||
assert!(node.is_in_doc());
|
assert!(node.is_in_doc());
|
||||||
|
|
||||||
let win = window_from_node(node).root();
|
let win = window_from_node(node).root();
|
||||||
let win = win.r();
|
let win = win.r();
|
||||||
let url = win.get_url();
|
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 data = node.GetTextContent().expect("Element.textContent must be a string");
|
||||||
let sheet = Stylesheet::from_str(data.as_slice(), url, Origin::Author);
|
let sheet = Stylesheet::from_str(data.as_slice(), url, Origin::Author);
|
||||||
let LayoutChan(ref layout_chan) = win.layout_chan();
|
let LayoutChan(ref layout_chan) = win.layout_chan();
|
||||||
layout_chan.send(Msg::AddStylesheet(sheet)).unwrap();
|
layout_chan.send(Msg::AddStylesheet(sheet, media)).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ use std::any::Any;
|
||||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||||
use std::boxed::BoxAny;
|
use std::boxed::BoxAny;
|
||||||
use style::stylesheets::Stylesheet;
|
use style::stylesheets::Stylesheet;
|
||||||
|
use style::media_queries::MediaQueryList;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
pub use dom::node::TrustedNodeAddress;
|
pub use dom::node::TrustedNodeAddress;
|
||||||
|
@ -24,10 +25,10 @@ pub use dom::node::TrustedNodeAddress;
|
||||||
/// Asynchronous messages that script can send to layout.
|
/// Asynchronous messages that script can send to layout.
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
/// Adds the given stylesheet to the document.
|
/// Adds the given stylesheet to the document.
|
||||||
AddStylesheet(Stylesheet),
|
AddStylesheet(Stylesheet, MediaQueryList),
|
||||||
|
|
||||||
/// Adds the given stylesheet to the document.
|
/// 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.
|
/// Puts a document into quirks mode, causing the quirks mode stylesheet to be loaded.
|
||||||
SetQuirksMode,
|
SetQuirksMode,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue