style: Switch properties to use a bitfield to determine validity in rules.

This doesn't use a full bitmap for every single rule type, as we only expect
that keyframe, page, and style rules will be checked.

Differential Revision: https://phabricator.services.mozilla.com/D104949
This commit is contained in:
Emily McDonough 2021-02-16 21:36:57 +00:00 committed by Emilio Cobos Álvarez
parent 36e6c68c9f
commit 1b18b06186
6 changed files with 51 additions and 47 deletions

View file

@ -37,6 +37,32 @@ SYSTEM_FONT_LONGHANDS = """font_family font_size font_style
font_feature_settings font_variation_settings font_feature_settings font_variation_settings
font_optical_sizing""".split() font_optical_sizing""".split()
# Bitfield values for all rule types which can have property declarations.
STYLE_RULE = 1 << 0
PAGE_RULE = 1 << 1
KEYFRAME_RULE = 1 << 2
ALL_RULES = STYLE_RULE | PAGE_RULE | KEYFRAME_RULE
DEFAULT_RULES = STYLE_RULE | KEYFRAME_RULE
DEFAULT_RULES_AND_PAGE = DEFAULT_RULES | PAGE_RULE
DEFAULT_RULES_EXCEPT_KEYFRAME = STYLE_RULE
# Rule name to value dict
RULE_VALUES = {
"Style": STYLE_RULE,
"Page": PAGE_RULE,
"Keyframe": KEYFRAME_RULE,
}
def rule_values_from_arg(that):
if isinstance(that, int):
return that
mask = 0
for rule in that.split():
mask |= RULE_VALUES[rule]
return mask
def maybe_moz_logical_alias(engine, side, prop): def maybe_moz_logical_alias(engine, side, prop):
if engine == "gecko" and side[1]: if engine == "gecko" and side[1]:
@ -214,7 +240,7 @@ class Longhand(object):
need_index=False, need_index=False,
gecko_ffi_name=None, gecko_ffi_name=None,
has_effect_on_gecko_scrollbars=None, has_effect_on_gecko_scrollbars=None,
allowed_in_keyframe_block=True, rule_types_allowed=DEFAULT_RULES,
cast_type="u8", cast_type="u8",
logical=False, logical=False,
logical_group=None, logical_group=None,
@ -222,7 +248,6 @@ class Longhand(object):
extra_prefixes=None, extra_prefixes=None,
boxed=False, boxed=False,
flags=None, flags=None,
allowed_in_page_rule=False,
allow_quirks="No", allow_quirks="No",
ignored_when_colors_disabled=False, ignored_when_colors_disabled=False,
simple_vector_bindings=False, simple_vector_bindings=False,
@ -270,20 +295,11 @@ class Longhand(object):
self.extra_prefixes = parse_property_aliases(extra_prefixes) self.extra_prefixes = parse_property_aliases(extra_prefixes)
self.boxed = arg_to_bool(boxed) self.boxed = arg_to_bool(boxed)
self.flags = flags.split() if flags else [] self.flags = flags.split() if flags else []
self.allowed_in_page_rule = arg_to_bool(allowed_in_page_rule)
self.allow_quirks = allow_quirks self.allow_quirks = allow_quirks
self.ignored_when_colors_disabled = ignored_when_colors_disabled self.ignored_when_colors_disabled = ignored_when_colors_disabled
self.is_vector = vector self.is_vector = vector
self.simple_vector_bindings = simple_vector_bindings self.simple_vector_bindings = simple_vector_bindings
# https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property
# > except those defined in this specification,
# > but does accept the `animation-play-state` property and interprets it specially.
self.allowed_in_keyframe_block = (
allowed_in_keyframe_block and allowed_in_keyframe_block != "False"
)
# This is done like this since just a plain bool argument seemed like # This is done like this since just a plain bool argument seemed like
# really random. # really random.
if animation_value_type is None: if animation_value_type is None:
@ -487,10 +503,9 @@ class Shorthand(object):
servo_2020_pref=None, servo_2020_pref=None,
gecko_pref=None, gecko_pref=None,
enabled_in="content", enabled_in="content",
allowed_in_keyframe_block=True, rule_types_allowed=DEFAULT_RULES,
alias=None, alias=None,
extra_prefixes=None, extra_prefixes=None,
allowed_in_page_rule=False,
flags=None, flags=None,
): ):
self.name = name self.name = name
@ -507,17 +522,9 @@ class Shorthand(object):
self.enabled_in = enabled_in self.enabled_in = enabled_in
self.alias = parse_property_aliases(alias) self.alias = parse_property_aliases(alias)
self.extra_prefixes = parse_property_aliases(extra_prefixes) self.extra_prefixes = parse_property_aliases(extra_prefixes)
self.allowed_in_page_rule = arg_to_bool(allowed_in_page_rule) self.rule_types_allowed = rule_values_from_arg(rule_types_allowed)
self.flags = flags.split() if flags else [] self.flags = flags.split() if flags else []
# https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property
# > except those defined in this specification,
# > but does accept the `animation-play-state` property and interprets it specially.
self.allowed_in_keyframe_block = (
allowed_in_keyframe_block and allowed_in_keyframe_block != "False"
)
def get_animatable(self): def get_animatable(self):
for sub in self.sub_properties: for sub in self.sub_properties:
if sub.animatable: if sub.animatable:
@ -575,8 +582,7 @@ class Alias(object):
self.servo_2020_pref = original.servo_2020_pref self.servo_2020_pref = original.servo_2020_pref
self.gecko_pref = gecko_pref self.gecko_pref = gecko_pref
self.transitionable = original.transitionable self.transitionable = original.transitionable
self.allowed_in_page_rule = original.allowed_in_page_rule self.rule_types_allowed = original.rule_types_allowed
self.allowed_in_keyframe_block = original.allowed_in_keyframe_block
@staticmethod @staticmethod
def type(): def type():

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
<%namespace name="helpers" file="/helpers.mako.rs" /> <%namespace name="helpers" file="/helpers.mako.rs" />
<% from data import ALL_AXES, Keyword, Method, to_rust_ident, to_camel_case%> <% from data import ALL_AXES, DEFAULT_RULES_EXCEPT_KEYFRAME, Keyword, Method, to_rust_ident, to_camel_case%>
<% data.new_style_struct("Box", <% data.new_style_struct("Box",
inherited=False, inherited=False,
@ -217,7 +217,7 @@ ${helpers.predefined_type(
need_index=True, need_index=True,
animation_value_type="none", animation_value_type="none",
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
spec="https://drafts.csswg.org/css-animations/#propdef-animation-name", spec="https://drafts.csswg.org/css-animations/#propdef-animation-name",
)} )}
@ -247,7 +247,6 @@ ${helpers.predefined_type(
need_index=True, need_index=True,
animation_value_type="none", animation_value_type="none",
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
allowed_in_keyframe_block=True,
spec="https://drafts.csswg.org/css-transitions/#propdef-animation-timing-function", spec="https://drafts.csswg.org/css-transitions/#propdef-animation-timing-function",
)} )}
@ -261,7 +260,7 @@ ${helpers.predefined_type(
need_index=True, need_index=True,
animation_value_type="none", animation_value_type="none",
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
spec="https://drafts.csswg.org/css-animations/#propdef-animation-iteration-count", spec="https://drafts.csswg.org/css-animations/#propdef-animation-iteration-count",
)} )}
@ -278,7 +277,7 @@ ${helpers.single_keyword(
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
gecko_inexhaustive=True, gecko_inexhaustive=True,
spec="https://drafts.csswg.org/css-animations/#propdef-animation-direction", spec="https://drafts.csswg.org/css-animations/#propdef-animation-direction",
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
)} )}
${helpers.single_keyword( ${helpers.single_keyword(
@ -291,7 +290,7 @@ ${helpers.single_keyword(
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
gecko_enum_prefix="StyleAnimationPlayState", gecko_enum_prefix="StyleAnimationPlayState",
spec="https://drafts.csswg.org/css-animations/#propdef-animation-play-state", spec="https://drafts.csswg.org/css-animations/#propdef-animation-play-state",
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
)} )}
${helpers.single_keyword( ${helpers.single_keyword(
@ -305,7 +304,7 @@ ${helpers.single_keyword(
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
gecko_inexhaustive=True, gecko_inexhaustive=True,
spec="https://drafts.csswg.org/css-animations/#propdef-animation-fill-mode", spec="https://drafts.csswg.org/css-animations/#propdef-animation-fill-mode",
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
)} )}
${helpers.predefined_type( ${helpers.predefined_type(
@ -319,7 +318,7 @@ ${helpers.predefined_type(
animation_value_type="none", animation_value_type="none",
extra_prefixes=animation_extra_prefixes, extra_prefixes=animation_extra_prefixes,
spec="https://drafts.csswg.org/css-animations/#propdef-animation-delay", spec="https://drafts.csswg.org/css-animations/#propdef-animation-delay",
allowed_in_keyframe_block=False, rule_types_allowed=DEFAULT_RULES_EXCEPT_KEYFRAME,
)} )}
<% transform_extra_prefixes = "moz:layout.css.prefixes.transforms webkit" %> <% transform_extra_prefixes = "moz:layout.css.prefixes.transforms webkit" %>

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
<%namespace name="helpers" file="/helpers.mako.rs" /> <%namespace name="helpers" file="/helpers.mako.rs" />
<% from data import ALL_SIDES, maybe_moz_logical_alias %> <% from data import ALL_SIDES, DEFAULT_RULES_AND_PAGE, maybe_moz_logical_alias %>
<% data.new_style_struct("Margin", inherited=False) %> <% data.new_style_struct("Margin", inherited=False) %>
% for side in ALL_SIDES: % for side in ALL_SIDES:
@ -23,7 +23,7 @@
logical=side[1], logical=side[1],
logical_group="margin", logical_group="margin",
spec=spec, spec=spec,
allowed_in_page_rule=True, rule_types_allowed=DEFAULT_RULES_AND_PAGE,
servo_restyle_damage="reflow" servo_restyle_damage="reflow"
)} )}
% endfor % endfor

View file

@ -54,7 +54,7 @@ pub use self::cascade::*;
<%! <%!
from collections import defaultdict from collections import defaultdict
from data import Method, PropertyRestrictions, Keyword, to_rust_ident, to_camel_case, SYSTEM_FONT_LONGHANDS from data import Method, PropertyRestrictions, Keyword, to_rust_ident, to_camel_case, RULE_VALUES, SYSTEM_FONT_LONGHANDS
import os.path import os.path
%> %>
@ -553,17 +553,15 @@ impl NonCustomPropertyId {
"Declarations are only expected inside a keyframe, page, or style rule." "Declarations are only expected inside a keyframe, page, or style rule."
); );
${static_non_custom_property_id_set( static MAP: [u8; NON_CUSTOM_PROPERTY_ID_COUNT] = [
"DISALLOWED_IN_KEYFRAME_BLOCK", % for property in data.longhands + data.shorthands + data.all_aliases():
lambda p: not p.allowed_in_keyframe_block ${property.rule_types_allowed},
)} % endfor
${static_non_custom_property_id_set( ];
"DISALLOWED_IN_PAGE_RULE",
lambda p: not p.allowed_in_page_rule
)}
match rule_type { match rule_type {
CssRuleType::Keyframe => !DISALLOWED_IN_KEYFRAME_BLOCK.contains(self), % for name in RULE_VALUES:
CssRuleType::Page => !DISALLOWED_IN_PAGE_RULE.contains(self), CssRuleType::${name} => MAP[self.0] & ${RULE_VALUES[name]} != 0,
% endfor
_ => true _ => true
} }
} }

View file

@ -190,7 +190,7 @@ macro_rules! try_parse_one {
animation-timing-function animation-delay animation-timing-function animation-delay
animation-iteration-count animation-direction animation-iteration-count animation-direction
animation-fill-mode animation-play-state" animation-fill-mode animation-play-state"
allowed_in_keyframe_block="False" rule_types_allowed="Style"
spec="https://drafts.csswg.org/css-animations/#propdef-animation"> spec="https://drafts.csswg.org/css-animations/#propdef-animation">
<% <%
props = "name duration timing_function delay iteration_count \ props = "name duration timing_function delay iteration_count \

View file

@ -3,6 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
<%namespace name="helpers" file="/helpers.mako.rs" /> <%namespace name="helpers" file="/helpers.mako.rs" />
<% from data import DEFAULT_RULES_AND_PAGE %>
${helpers.four_sides_shorthand( ${helpers.four_sides_shorthand(
"margin", "margin",
@ -10,7 +11,7 @@ ${helpers.four_sides_shorthand(
"specified::LengthPercentageOrAuto::parse", "specified::LengthPercentageOrAuto::parse",
engines="gecko servo-2013 servo-2020", engines="gecko servo-2013 servo-2020",
spec="https://drafts.csswg.org/css-box/#propdef-margin", spec="https://drafts.csswg.org/css-box/#propdef-margin",
allowed_in_page_rule=True, rule_types_allowed=DEFAULT_RULES_AND_PAGE,
allow_quirks="Yes", allow_quirks="Yes",
)} )}