Add base_url and namespaces to style::Stylesheet

This commit is contained in:
Simon Sapin 2016-11-28 16:16:59 +01:00
parent eb7032f6dd
commit 0714e2291c
3 changed files with 35 additions and 23 deletions

View file

@ -100,6 +100,8 @@ impl HTMLMetaElement {
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet { *self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))].into(), rules: vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))].into(),
origin: Origin::Author, origin: Origin::Author,
base_url: window_from_node(self).get_url(),
namespaces: Default::default(),
media: Default::default(), media: Default::default(),
// Viewport constraints are always recomputed on resize; they don't need to // Viewport constraints are always recomputed on resize; they don't need to
// force all styles to be recomputed. // force all styles to be recomputed.

View file

@ -44,7 +44,7 @@ pub enum Origin {
User, User,
} }
#[derive(Default)] #[derive(Default, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Namespaces { pub struct Namespaces {
pub default: Option<Namespace>, pub default: Option<Namespace>,
@ -166,6 +166,8 @@ pub struct Stylesheet {
/// List of media associated with the Stylesheet. /// List of media associated with the Stylesheet.
pub media: Arc<RwLock<MediaList>>, pub media: Arc<RwLock<MediaList>>,
pub origin: Origin, pub origin: Origin,
pub base_url: ServoUrl,
pub namespaces: RwLock<Namespaces>,
pub dirty_on_viewport_size_change: AtomicBool, pub dirty_on_viewport_size_change: AtomicBool,
pub disabled: AtomicBool, pub disabled: AtomicBool,
} }
@ -435,37 +437,40 @@ impl Stylesheet {
error_reporter: Box<ParseErrorReporter + Send>, error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData) -> Stylesheet { extra_data: ParserContextExtraData) -> Stylesheet {
let mut namespaces = Namespaces::default(); let mut namespaces = Namespaces::default();
let rule_parser = TopLevelRuleParser {
stylesheet_origin: origin,
namespaces: &mut namespaces,
context: ParserContext::new_with_extra_data(origin, &base_url, error_reporter.clone(),
extra_data),
state: Cell::new(State::Start),
};
let mut input = Parser::new(css);
input.look_for_viewport_percentages();
let mut rules = vec![]; let mut rules = vec![];
let dirty_on_viewport_size_change;
{ {
let mut iter = RuleListParser::new_for_stylesheet(&mut input, rule_parser); let rule_parser = TopLevelRuleParser {
while let Some(result) = iter.next() { stylesheet_origin: origin,
match result { namespaces: &mut namespaces,
Ok(rule) => rules.push(rule), context: ParserContext::new_with_extra_data(origin, &base_url, error_reporter, extra_data),
Err(range) => { state: Cell::new(State::Start),
let pos = range.start; };
let message = format!("Invalid rule: '{}'", iter.input.slice(range)); let mut input = Parser::new(css);
let context = ParserContext::new(origin, &base_url, error_reporter.clone()); input.look_for_viewport_percentages();
log_css_error(iter.input, pos, &*message, &context); {
let mut iter = RuleListParser::new_for_stylesheet(&mut input, rule_parser);
while let Some(result) = iter.next() {
match result {
Ok(rule) => rules.push(rule),
Err(range) => {
let pos = range.start;
let message = format!("Invalid rule: '{}'", iter.input.slice(range));
log_css_error(iter.input, pos, &*message, &iter.parser.context);
}
} }
} }
} }
dirty_on_viewport_size_change = input.seen_viewport_percentages();
} }
Stylesheet { Stylesheet {
origin: origin, origin: origin,
base_url: base_url,
namespaces: RwLock::new(namespaces),
rules: rules.into(), rules: rules.into(),
media: Arc::new(RwLock::new(media)), media: Arc::new(RwLock::new(media)),
dirty_on_viewport_size_change: AtomicBool::new(input.seen_viewport_percentages()), dirty_on_viewport_size_change: AtomicBool::new(dirty_on_viewport_size_change),
disabled: AtomicBool::new(false), disabled: AtomicBool::new(false),
} }
} }

View file

@ -19,7 +19,8 @@ use style::parser::ParserContextExtraData;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands}; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands};
use style::properties::Importance; use style::properties::Importance;
use style::properties::longhands::animation_play_state; use style::properties::longhands::animation_play_state;
use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, StyleRule, KeyframesRule, Origin}; use style::stylesheets::{Origin, Namespaces};
use style::stylesheets::{Stylesheet, NamespaceRule, CssRule, StyleRule, KeyframesRule};
use style::values::specified::{LengthOrPercentageOrAuto, Percentage}; use style::values::specified::{LengthOrPercentageOrAuto, Percentage};
#[test] #[test]
@ -50,12 +51,16 @@ fn test_parse_stylesheet() {
} }
}"; }";
let url = ServoUrl::parse("about::test").unwrap(); let url = ServoUrl::parse("about::test").unwrap();
let stylesheet = Stylesheet::from_str(css, url, Origin::UserAgent, Default::default(), let stylesheet = Stylesheet::from_str(css, url.clone(), Origin::UserAgent, Default::default(),
Box::new(CSSErrorReporterTest), Box::new(CSSErrorReporterTest),
ParserContextExtraData::default()); ParserContextExtraData::default());
let mut namespaces = Namespaces::default();
namespaces.default = Some(ns!(html));
let expected = Stylesheet { let expected = Stylesheet {
origin: Origin::UserAgent, origin: Origin::UserAgent,
media: Default::default(), media: Default::default(),
namespaces: RwLock::new(namespaces),
base_url: url,
dirty_on_viewport_size_change: AtomicBool::new(false), dirty_on_viewport_size_change: AtomicBool::new(false),
disabled: AtomicBool::new(false), disabled: AtomicBool::new(false),
rules: vec![ rules: vec![