Auto merge of #14395 - servo:stylesheet-metadata, r=Manishearth

Use stylesheet’s base URL and ns prefixes in CSSOM insert/appendRule.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/14395)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-11-29 01:28:06 -08:00 committed by GitHub
commit 40917e7991
15 changed files with 143 additions and 112 deletions

View file

@ -22,17 +22,18 @@ pub struct CSSFontFaceRule {
}
impl CSSFontFaceRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, fontfacerule: Arc<RwLock<FontFaceRule>>) -> CSSFontFaceRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, fontfacerule: Arc<RwLock<FontFaceRule>>)
-> CSSFontFaceRule {
CSSFontFaceRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
fontfacerule: fontfacerule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
fontfacerule: Arc<RwLock<FontFaceRule>>) -> Root<CSSFontFaceRule> {
reflect_dom_object(box CSSFontFaceRule::new_inherited(parent, fontfacerule),
reflect_dom_object(box CSSFontFaceRule::new_inherited(parent_stylesheet, fontfacerule),
window,
CSSFontFaceRuleBinding::Wrap)
}

View file

@ -4,7 +4,6 @@
use dom::bindings::codegen::Bindings::CSSGroupingRuleBinding;
use dom::bindings::codegen::Bindings::CSSGroupingRuleBinding::CSSGroupingRuleMethods;
use dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleBinding::CSSRuleMethods;
use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
@ -25,27 +24,26 @@ pub struct CSSGroupingRule {
}
impl CSSGroupingRule {
pub fn new_inherited(parent: Option<&CSSStyleSheet>,
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet,
rules: StyleCssRules) -> CSSGroupingRule {
CSSGroupingRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
rules: rules,
rulelist: MutNullableHeap::new(None),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>, rules: StyleCssRules) -> Root<CSSGroupingRule> {
reflect_dom_object(box CSSGroupingRule::new_inherited(parent, rules),
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet, rules: StyleCssRules) -> Root<CSSGroupingRule> {
reflect_dom_object(box CSSGroupingRule::new_inherited(parent_stylesheet, rules),
window,
CSSGroupingRuleBinding::Wrap)
}
fn rulelist(&self) -> Root<CSSRuleList> {
let sheet = self.upcast::<CSSRule>().GetParentStyleSheet();
let sheet = sheet.as_ref().map(|s| &**s);
let parent_stylesheet = self.upcast::<CSSRule>().parent_stylesheet();
self.rulelist.or_init(|| CSSRuleList::new(self.global().as_window(),
sheet,
parent_stylesheet,
RulesSource::Rules(self.rules.clone())))
}
}

View file

@ -22,17 +22,18 @@ pub struct CSSKeyframeRule {
}
impl CSSKeyframeRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, keyframerule: Arc<RwLock<Keyframe>>) -> CSSKeyframeRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, keyframerule: Arc<RwLock<Keyframe>>)
-> CSSKeyframeRule {
CSSKeyframeRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
keyframerule: keyframerule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
keyframerule: Arc<RwLock<Keyframe>>) -> Root<CSSKeyframeRule> {
reflect_dom_object(box CSSKeyframeRule::new_inherited(parent, keyframerule),
reflect_dom_object(box CSSKeyframeRule::new_inherited(parent_stylesheet, keyframerule),
window,
CSSKeyframeRuleBinding::Wrap)
}

View file

@ -5,7 +5,6 @@
use cssparser::Parser;
use dom::bindings::codegen::Bindings::CSSKeyframesRuleBinding;
use dom::bindings::codegen::Bindings::CSSKeyframesRuleBinding::CSSKeyframesRuleMethods;
use dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleMethods;
use dom::bindings::codegen::Bindings::WindowBinding::WindowBinding::WindowMethods;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
@ -20,7 +19,7 @@ use parking_lot::RwLock;
use std::sync::Arc;
use style::keyframes::{Keyframe, KeyframeSelector};
use style::parser::ParserContextExtraData;
use style::stylesheets::{KeyframesRule, Origin};
use style::stylesheets::KeyframesRule;
use style_traits::ToCss;
#[dom_struct]
@ -32,28 +31,28 @@ pub struct CSSKeyframesRule {
}
impl CSSKeyframesRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, keyframesrule: Arc<RwLock<KeyframesRule>>) -> CSSKeyframesRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, keyframesrule: Arc<RwLock<KeyframesRule>>)
-> CSSKeyframesRule {
CSSKeyframesRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
keyframesrule: keyframesrule,
rulelist: MutNullableHeap::new(None),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
keyframesrule: Arc<RwLock<KeyframesRule>>) -> Root<CSSKeyframesRule> {
reflect_dom_object(box CSSKeyframesRule::new_inherited(parent, keyframesrule),
reflect_dom_object(box CSSKeyframesRule::new_inherited(parent_stylesheet, keyframesrule),
window,
CSSKeyframesRuleBinding::Wrap)
}
fn rulelist(&self) -> Root<CSSRuleList> {
self.rulelist.or_init(|| {
let sheet = self.upcast::<CSSRule>().GetParentStyleSheet();
let sheet = sheet.as_ref().map(|s| &**s);
let parent_stylesheet = &self.upcast::<CSSRule>().parent_stylesheet();
CSSRuleList::new(self.global().as_window(),
sheet,
parent_stylesheet,
RulesSource::Keyframes(self.keyframesrule.clone()))
})
}
@ -84,8 +83,7 @@ impl CSSKeyframesRuleMethods for CSSKeyframesRule {
fn AppendRule(&self, rule: DOMString) {
let global = self.global();
let window = global.as_window();
let doc = window.Document();
let rule = Keyframe::parse(&rule, Origin::Author, doc.url().clone(),
let rule = Keyframe::parse(&rule, self.cssrule.parent_stylesheet().style_stylesheet(),
ParserContextExtraData::default());
if let Ok(rule) = rule {
self.keyframesrule.write().keyframes.push(rule);

View file

@ -23,18 +23,19 @@ pub struct CSSMediaRule {
}
impl CSSMediaRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, mediarule: Arc<RwLock<MediaRule>>) -> CSSMediaRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, mediarule: Arc<RwLock<MediaRule>>)
-> CSSMediaRule {
let list = mediarule.read().rules.clone();
CSSMediaRule {
cssrule: CSSGroupingRule::new_inherited(parent, list),
cssrule: CSSGroupingRule::new_inherited(parent_stylesheet, list),
mediarule: mediarule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
mediarule: Arc<RwLock<MediaRule>>) -> Root<CSSMediaRule> {
reflect_dom_object(box CSSMediaRule::new_inherited(parent, mediarule),
reflect_dom_object(box CSSMediaRule::new_inherited(parent_stylesheet, mediarule),
window,
CSSMediaRuleBinding::Wrap)
}

View file

@ -23,17 +23,18 @@ pub struct CSSNamespaceRule {
}
impl CSSNamespaceRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, namespacerule: Arc<RwLock<NamespaceRule>>) -> CSSNamespaceRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, namespacerule: Arc<RwLock<NamespaceRule>>)
-> CSSNamespaceRule {
CSSNamespaceRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
namespacerule: namespacerule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
namespacerule: Arc<RwLock<NamespaceRule>>) -> Root<CSSNamespaceRule> {
reflect_dom_object(box CSSNamespaceRule::new_inherited(parent, namespacerule),
reflect_dom_object(box CSSNamespaceRule::new_inherited(parent_stylesheet, namespacerule),
window,
CSSNamespaceRuleBinding::Wrap)
}

View file

@ -5,7 +5,7 @@
use dom::bindings::codegen::Bindings::CSSRuleBinding;
use dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleMethods;
use dom::bindings::inheritance::Castable;
use dom::bindings::js::{JS, MutNullableHeap, Root};
use dom::bindings::js::{JS, Root};
use dom::bindings::reflector::{Reflector, reflect_dom_object};
use dom::bindings::str::DOMString;
use dom::cssfontfacerule::CSSFontFaceRule;
@ -17,27 +17,34 @@ use dom::cssstylerule::CSSStyleRule;
use dom::cssstylesheet::CSSStyleSheet;
use dom::cssviewportrule::CSSViewportRule;
use dom::window::Window;
use std::cell::Cell;
use style::stylesheets::CssRule as StyleCssRule;
#[dom_struct]
pub struct CSSRule {
reflector_: Reflector,
parent: MutNullableHeap<JS<CSSStyleSheet>>,
parent_stylesheet: JS<CSSStyleSheet>,
/// Whether the parentStyleSheet attribute should return null.
/// We keep parent_stylesheet in that case because insertRule needs it
/// for the stylesheets base URL and namespace prefixes.
parent_stylesheet_removed: Cell<bool>,
}
impl CSSRule {
#[allow(unrooted_must_root)]
pub fn new_inherited(parent: Option<&CSSStyleSheet>) -> CSSRule {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet) -> CSSRule {
CSSRule {
reflector_: Reflector::new(),
parent: MutNullableHeap::new(parent),
parent_stylesheet: JS::from_ref(parent_stylesheet),
parent_stylesheet_removed: Cell::new(false),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>) -> Root<CSSRule> {
reflect_dom_object(box CSSRule::new_inherited(parent),
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet) -> Root<CSSRule> {
reflect_dom_object(box CSSRule::new_inherited(parent_stylesheet),
window,
CSSRuleBinding::Wrap)
}
@ -64,16 +71,16 @@ impl CSSRule {
// Given a StyleCssRule, create a new instance of a derived class of
// CSSRule based on which rule it is
pub fn new_specific(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new_specific(window: &Window, parent_stylesheet: &CSSStyleSheet,
rule: StyleCssRule) -> Root<CSSRule> {
// be sure to update the match in as_specific when this is updated
match rule {
StyleCssRule::Style(s) => Root::upcast(CSSStyleRule::new(window, parent, s)),
StyleCssRule::FontFace(s) => Root::upcast(CSSFontFaceRule::new(window, parent, s)),
StyleCssRule::Keyframes(s) => Root::upcast(CSSKeyframesRule::new(window, parent, s)),
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent, s)),
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::new(window, parent, s)),
StyleCssRule::Viewport(s) => Root::upcast(CSSViewportRule::new(window, parent, s)),
StyleCssRule::Style(s) => Root::upcast(CSSStyleRule::new(window, parent_stylesheet, s)),
StyleCssRule::FontFace(s) => Root::upcast(CSSFontFaceRule::new(window, parent_stylesheet, s)),
StyleCssRule::Keyframes(s) => Root::upcast(CSSKeyframesRule::new(window, parent_stylesheet, s)),
StyleCssRule::Media(s) => Root::upcast(CSSMediaRule::new(window, parent_stylesheet, s)),
StyleCssRule::Namespace(s) => Root::upcast(CSSNamespaceRule::new(window, parent_stylesheet, s)),
StyleCssRule::Viewport(s) => Root::upcast(CSSViewportRule::new(window, parent_stylesheet, s)),
}
}
@ -85,12 +92,16 @@ impl CSSRule {
/// Sets owner sheet to null (and does the same for all children)
pub fn deparent(&self) {
self.parent.set(None);
self.parent_stylesheet_removed.set(true);
// https://github.com/w3c/csswg-drafts/issues/722
// Spec doesn't ask us to do this, but it makes sense
// and browsers implement this behavior
self.as_specific().deparent_children();
}
pub fn parent_stylesheet(&self) -> &CSSStyleSheet {
&self.parent_stylesheet
}
}
impl CSSRuleMethods for CSSRule {
@ -101,7 +112,11 @@ impl CSSRuleMethods for CSSRule {
// https://drafts.csswg.org/cssom/#dom-cssrule-parentstylesheet
fn GetParentStyleSheet(&self) -> Option<Root<CSSStyleSheet>> {
self.parent.get()
if self.parent_stylesheet_removed.get() {
None
} else {
Some(Root::from_ref(&*self.parent_stylesheet))
}
}
// https://drafts.csswg.org/cssom/#dom-cssrule-csstext
@ -118,7 +133,7 @@ impl CSSRuleMethods for CSSRule {
pub trait SpecificCSSRule {
fn ty(&self) -> u16;
fn get_css(&self) -> DOMString;
/// Remove CSSStyleSheet parent from all transitive children
/// Remove parentStylesheet from all transitive children
fn deparent_children(&self) {
// most CSSRules do nothing here
}

View file

@ -34,7 +34,7 @@ impl From<RulesMutateError> for Error {
#[dom_struct]
pub struct CSSRuleList {
reflector_: Reflector,
sheet: MutNullableHeap<JS<CSSStyleSheet>>,
parent_stylesheet: JS<CSSStyleSheet>,
#[ignore_heap_size_of = "Arc"]
rules: RulesSource,
dom_rules: DOMRefCell<Vec<MutNullableHeap<JS<CSSRule>>>>
@ -47,7 +47,7 @@ pub enum RulesSource {
impl CSSRuleList {
#[allow(unrooted_must_root)]
pub fn new_inherited(sheet: Option<&CSSStyleSheet>, rules: RulesSource) -> CSSRuleList {
pub fn new_inherited(parent_stylesheet: &CSSStyleSheet, rules: RulesSource) -> CSSRuleList {
let dom_rules = match rules {
RulesSource::Rules(ref rules) => {
rules.0.read().iter().map(|_| MutNullableHeap::new(None)).collect()
@ -59,16 +59,16 @@ impl CSSRuleList {
CSSRuleList {
reflector_: Reflector::new(),
sheet: MutNullableHeap::new(sheet),
parent_stylesheet: JS::from_ref(parent_stylesheet),
rules: rules,
dom_rules: DOMRefCell::new(dom_rules),
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, sheet: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
rules: RulesSource) -> Root<CSSRuleList> {
reflect_dom_object(box CSSRuleList::new_inherited(sheet, rules),
reflect_dom_object(box CSSRuleList::new_inherited(parent_stylesheet, rules),
window,
CSSRuleListBinding::Wrap)
}
@ -84,14 +84,13 @@ impl CSSRuleList {
let global = self.global();
let window = global.as_window();
let doc = window.Document();
let index = idx as usize;
let new_rule = css_rules.insert_rule(rule, doc.url().clone(), index, nested)?;
let parent_stylesheet = self.parent_stylesheet.style_stylesheet();
let new_rule = css_rules.insert_rule(rule, parent_stylesheet, index, nested)?;
let sheet = self.sheet.get();
let sheet = sheet.as_ref().map(|sheet| &**sheet);
let dom_rule = CSSRule::new_specific(&window, sheet, new_rule);
let parent_stylesheet = &*self.parent_stylesheet;
let dom_rule = CSSRule::new_specific(&window, parent_stylesheet, new_rule);
self.dom_rules.borrow_mut().insert(index, MutNullableHeap::new(Some(&*dom_rule)));
Ok((idx))
}
@ -129,17 +128,16 @@ impl CSSRuleList {
pub fn item(&self, idx: u32) -> Option<Root<CSSRule>> {
self.dom_rules.borrow().get(idx as usize).map(|rule| {
rule.or_init(|| {
let sheet = self.sheet.get();
let sheet = sheet.as_ref().map(|sheet| &**sheet);
let parent_stylesheet = &self.parent_stylesheet;
match self.rules {
RulesSource::Rules(ref rules) => {
CSSRule::new_specific(self.global().as_window(),
sheet,
parent_stylesheet,
rules.0.read()[idx as usize].clone())
}
RulesSource::Keyframes(ref rules) => {
Root::upcast(CSSKeyframeRule::new(self.global().as_window(),
sheet,
parent_stylesheet,
rules.read()
.keyframes[idx as usize]
.clone()))

View file

@ -22,17 +22,18 @@ pub struct CSSStyleRule {
}
impl CSSStyleRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, stylerule: Arc<RwLock<StyleRule>>) -> CSSStyleRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, stylerule: Arc<RwLock<StyleRule>>)
-> CSSStyleRule {
CSSStyleRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
stylerule: stylerule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
stylerule: Arc<RwLock<StyleRule>>) -> Root<CSSStyleRule> {
reflect_dom_object(box CSSStyleRule::new_inherited(parent, stylerule),
reflect_dom_object(box CSSStyleRule::new_inherited(parent_stylesheet, stylerule),
window,
CSSStyleRuleBinding::Wrap)
}

View file

@ -53,7 +53,7 @@ impl CSSStyleSheet {
fn rulelist(&self) -> Root<CSSRuleList> {
self.rulelist.or_init(|| CSSRuleList::new(self.global().as_window(),
Some(self),
self,
RulesSource::Rules(self.style_stylesheet
.rules.clone())))
}
@ -67,6 +67,10 @@ impl CSSStyleSheet {
self.global().as_window().Document().invalidate_stylesheets();
}
}
pub fn style_stylesheet(&self) -> &StyleStyleSheet {
&self.style_stylesheet
}
}
impl CSSStyleSheetMethods for CSSStyleSheet {

View file

@ -22,17 +22,17 @@ pub struct CSSViewportRule {
}
impl CSSViewportRule {
fn new_inherited(parent: Option<&CSSStyleSheet>, viewportrule: Arc<RwLock<ViewportRule>>) -> CSSViewportRule {
fn new_inherited(parent_stylesheet: &CSSStyleSheet, viewportrule: Arc<RwLock<ViewportRule>>) -> CSSViewportRule {
CSSViewportRule {
cssrule: CSSRule::new_inherited(parent),
cssrule: CSSRule::new_inherited(parent_stylesheet),
viewportrule: viewportrule,
}
}
#[allow(unrooted_must_root)]
pub fn new(window: &Window, parent: Option<&CSSStyleSheet>,
pub fn new(window: &Window, parent_stylesheet: &CSSStyleSheet,
viewportrule: Arc<RwLock<ViewportRule>>) -> Root<CSSViewportRule> {
reflect_dom_object(box CSSViewportRule::new_inherited(parent, viewportrule),
reflect_dom_object(box CSSViewportRule::new_inherited(parent_stylesheet, viewportrule),
window,
CSSViewportRuleBinding::Wrap)
}

View file

@ -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.

View file

@ -9,11 +9,10 @@ use parser::{ParserContext, ParserContextExtraData, log_css_error};
use properties::{Importance, PropertyDeclaration, PropertyDeclarationBlock};
use properties::PropertyDeclarationParseResult;
use properties::animated_properties::TransitionProperty;
use servo_url::ServoUrl;
use std::fmt;
use std::sync::Arc;
use style_traits::ToCss;
use stylesheets::{MemoryHoleReporter, Origin};
use stylesheets::{MemoryHoleReporter, Stylesheet};
/// A number from 1 to 100, indicating the percentage of the animation where
/// this keyframe should run.
@ -108,11 +107,11 @@ impl ToCss for Keyframe {
impl Keyframe {
pub fn parse(css: &str, origin: Origin,
base_url: ServoUrl,
extra_data: ParserContextExtraData) -> Result<Arc<RwLock<Self>>, ()> {
pub fn parse(css: &str, parent_stylesheet: &Stylesheet, extra_data: ParserContextExtraData)
-> Result<Arc<RwLock<Self>>, ()> {
let error_reporter = Box::new(MemoryHoleReporter);
let context = ParserContext::new_with_extra_data(origin, &base_url,
let context = ParserContext::new_with_extra_data(parent_stylesheet.origin,
&parent_stylesheet.base_url,
error_reporter,
extra_data);
let mut input = Parser::new(css);

View file

@ -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>,
@ -88,7 +88,7 @@ impl CssRules {
}
// https://drafts.csswg.org/cssom/#insert-a-css-rule
pub fn insert_rule(&self, rule: &str, base_url: ServoUrl, index: usize, nested: bool)
pub fn insert_rule(&self, rule: &str, parent_stylesheet: &Stylesheet, index: usize, nested: bool)
-> Result<CssRule, RulesMutateError> {
let mut rules = self.0.write();
@ -108,7 +108,7 @@ impl CssRules {
// Step 3, 4
// XXXManishearth should we also store the namespace map?
let (new_rule, new_state) = try!(CssRule::parse(&rule, Origin::Author, base_url,
let (new_rule, new_state) = try!(CssRule::parse(&rule, parent_stylesheet,
ParserContextExtraData::default(), state));
// Step 5
@ -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,
}
@ -280,13 +282,15 @@ impl CssRule {
// input state is None for a nested rule
// Returns a parsed CSS rule and the final state of the parser
pub fn parse(css: &str, origin: Origin,
base_url: ServoUrl,
pub fn parse(css: &str,
parent_stylesheet: &Stylesheet,
extra_data: ParserContextExtraData,
state: Option<State>) -> Result<(Self, State), SingleRuleParseError> {
state: Option<State>)
-> Result<(Self, State), SingleRuleParseError> {
let error_reporter = Box::new(MemoryHoleReporter);
let mut namespaces = Namespaces::default();
let context = ParserContext::new_with_extra_data(origin, &base_url,
let mut namespaces = parent_stylesheet.namespaces.write();
let context = ParserContext::new_with_extra_data(parent_stylesheet.origin,
&parent_stylesheet.base_url,
error_reporter.clone(),
extra_data);
let mut input = Parser::new(css);
@ -294,7 +298,7 @@ impl CssRule {
// nested rules are in the body state
let state = state.unwrap_or(State::Body);
let mut rule_parser = TopLevelRuleParser {
stylesheet_origin: origin,
stylesheet_origin: parent_stylesheet.origin,
context: context,
state: Cell::new(state),
namespaces: &mut namespaces,
@ -435,17 +439,17 @@ impl Stylesheet {
error_reporter: Box<ParseErrorReporter + Send>,
extra_data: ParserContextExtraData) -> Stylesheet {
let mut namespaces = Namespaces::default();
let mut rules = vec![];
let dirty_on_viewport_size_change;
{
let rule_parser = TopLevelRuleParser {
stylesheet_origin: origin,
namespaces: &mut namespaces,
context: ParserContext::new_with_extra_data(origin, &base_url, error_reporter.clone(),
extra_data),
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 rules = vec![];
{
let mut iter = RuleListParser::new_for_stylesheet(&mut input, rule_parser);
while let Some(result) = iter.next() {
@ -454,18 +458,21 @@ impl Stylesheet {
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);
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),
}
}

View file

@ -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]
@ -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![