Skip to content

Commit

Permalink
Add base_url and namespaces to style::Stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonSapin committed Nov 28, 2016
1 parent eb7032f commit 0714e22
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 2 additions & 0 deletions components/script/dom/htmlmetaelement.rs
Expand Up @@ -100,6 +100,8 @@ impl HTMLMetaElement {
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: vec![CssRule::Viewport(Arc::new(RwLock::new(translated_rule)))].into(),
origin: Origin::Author,
base_url: window_from_node(self).get_url(),
namespaces: Default::default(),
media: Default::default(),
// Viewport constraints are always recomputed on resize; they don't need to
// force all styles to be recomputed.
Expand Down
47 changes: 26 additions & 21 deletions components/style/stylesheets.rs
Expand Up @@ -44,7 +44,7 @@ pub enum Origin {
User,
}

#[derive(Default)]
#[derive(Default, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct Namespaces {
pub default: Option<Namespace>,
Expand Down Expand Up @@ -166,6 +166,8 @@ pub struct Stylesheet {
/// List of media associated with the Stylesheet.
pub media: Arc<RwLock<MediaList>>,
pub origin: Origin,
pub base_url: ServoUrl,
pub namespaces: RwLock<Namespaces>,
pub dirty_on_viewport_size_change: AtomicBool,
pub disabled: AtomicBool,
}
Expand Down Expand Up @@ -435,37 +437,40 @@ impl Stylesheet {
error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData) -> Stylesheet {
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 dirty_on_viewport_size_change;
{
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));
let context = ParserContext::new(origin, &base_url, error_reporter.clone());
log_css_error(iter.input, pos, &*message, &context);
let rule_parser = TopLevelRuleParser {
stylesheet_origin: origin,
namespaces: &mut namespaces,
context: ParserContext::new_with_extra_data(origin, &base_url, error_reporter, extra_data),
state: Cell::new(State::Start),
};
let mut input = Parser::new(css);
input.look_for_viewport_percentages();
{
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 {
origin: origin,
base_url: base_url,
namespaces: RwLock::new(namespaces),
rules: rules.into(),
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),
}
}
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/style/stylesheets.rs
Expand Up @@ -19,7 +19,8 @@ use style::parser::ParserContextExtraData;
use style::properties::{PropertyDeclaration, PropertyDeclarationBlock, DeclaredValue, longhands};
use style::properties::Importance;
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};

#[test]
Expand Down Expand Up @@ -50,12 +51,16 @@ fn test_parse_stylesheet() {
}
}";
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),
ParserContextExtraData::default());
let mut namespaces = Namespaces::default();
namespaces.default = Some(ns!(html));
let expected = Stylesheet {
origin: Origin::UserAgent,
media: Default::default(),
namespaces: RwLock::new(namespaces),
base_url: url,
dirty_on_viewport_size_change: AtomicBool::new(false),
disabled: AtomicBool::new(false),
rules: vec![
Expand Down

0 comments on commit 0714e22

Please sign in to comment.