style: CSSOM should respect rule-level property restrictions.

Differential Revision: https://phabricator.services.mozilla.com/D90729
This commit is contained in:
Emilio Cobos Álvarez 2020-09-21 09:57:46 +00:00
parent c99d6adf01
commit 60d89cfbc0
3 changed files with 19 additions and 13 deletions

View file

@ -1263,11 +1263,12 @@ pub fn parse_style_attribute(
url_data: &UrlExtraData, url_data: &UrlExtraData,
error_reporter: Option<&dyn ParseErrorReporter>, error_reporter: Option<&dyn ParseErrorReporter>,
quirks_mode: QuirksMode, quirks_mode: QuirksMode,
rule_type: CssRuleType,
) -> PropertyDeclarationBlock { ) -> PropertyDeclarationBlock {
let context = ParserContext::new( let context = ParserContext::new(
Origin::Author, Origin::Author,
url_data, url_data,
Some(CssRuleType::Style), Some(rule_type),
ParsingMode::DEFAULT, ParsingMode::DEFAULT,
quirks_mode, quirks_mode,
error_reporter, error_reporter,
@ -1291,11 +1292,12 @@ pub fn parse_one_declaration_into(
error_reporter: Option<&dyn ParseErrorReporter>, error_reporter: Option<&dyn ParseErrorReporter>,
parsing_mode: ParsingMode, parsing_mode: ParsingMode,
quirks_mode: QuirksMode, quirks_mode: QuirksMode,
rule_type: CssRuleType,
) -> Result<(), ()> { ) -> Result<(), ()> {
let context = ParserContext::new( let context = ParserContext::new(
Origin::Author, Origin::Author,
url_data, url_data,
Some(CssRuleType::Style), Some(rule_type),
parsing_mode, parsing_mode,
quirks_mode, quirks_mode,
error_reporter, error_reporter,

View file

@ -542,10 +542,12 @@ impl NonCustomPropertyId {
false false
} }
fn allowed_in(self, context: &ParserContext) -> bool { /// Returns whether a given rule allows a given property.
#[inline]
pub fn allowed_in_rule(self, rule_type: CssRuleType) -> bool {
debug_assert!( debug_assert!(
matches!( matches!(
context.rule_type(), rule_type,
CssRuleType::Keyframe | CssRuleType::Page | CssRuleType::Style CssRuleType::Keyframe | CssRuleType::Page | CssRuleType::Style
), ),
"Declarations are only expected inside a keyframe, page, or style rule." "Declarations are only expected inside a keyframe, page, or style rule."
@ -559,14 +561,16 @@ impl NonCustomPropertyId {
"DISALLOWED_IN_PAGE_RULE", "DISALLOWED_IN_PAGE_RULE",
lambda p: not p.allowed_in_page_rule lambda p: not p.allowed_in_page_rule
)} )}
match context.rule_type() { match rule_type {
CssRuleType::Keyframe if DISALLOWED_IN_KEYFRAME_BLOCK.contains(self) => { CssRuleType::Keyframe => !DISALLOWED_IN_KEYFRAME_BLOCK.contains(self),
return false; CssRuleType::Page => !DISALLOWED_IN_PAGE_RULE.contains(self),
_ => true
} }
CssRuleType::Page if DISALLOWED_IN_PAGE_RULE.contains(self) => {
return false;
} }
_ => {}
fn allowed_in(self, context: &ParserContext) -> bool {
if !self.allowed_in_rule(context.rule_type()) {
return false;
} }
self.allowed_in_ignoring_rule_type(context) self.allowed_in_ignoring_rule_type(context)

View file

@ -299,7 +299,7 @@ impl CssRule {
} }
#[allow(missing_docs)] #[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)] #[derive(Clone, Copy, Debug, Eq, FromPrimitive, PartialEq)]
pub enum CssRuleType { pub enum CssRuleType {
// https://drafts.csswg.org/cssom/#the-cssrule-interface // https://drafts.csswg.org/cssom/#the-cssrule-interface
Style = 1, Style = 1,