Skip to content

Commit

Permalink
Support property aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Jan 7, 2017
1 parent 6d4ccab commit 33966a8
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 56 deletions.
16 changes: 10 additions & 6 deletions components/style/properties/build.py
Expand Up @@ -70,13 +70,17 @@ def write(directory, filename, content):
open(os.path.join(directory, filename), "wb").write(content)


def static_id_generator(properties):
for kind, props in [("Longhand", properties.longhands),
("Shorthand", properties.shorthands)]:
for p in props:
yield "%s\tStaticId::%s(%sId::%s)" % (p.name, kind, kind, p.camel_case)
for alias in p.alias:
yield "%s\tStaticId::%s(%sId::%s)" % (alias, kind, kind, p.camel_case)


def static_ids(properties):
return '\n'.join(
"%s\tStaticId::%s(%sId::%s)" % (p.name, kind, kind, p.camel_case)
for kind, props in [("Longhand", properties.longhands),
("Shorthand", properties.shorthands)]
for p in props
)
return '\n'.join(static_id_generator(properties))


def write_html(properties):
Expand Down
6 changes: 4 additions & 2 deletions components/style/properties/data.py
Expand Up @@ -88,7 +88,7 @@ def __init__(self, style_struct, name, spec=None, animatable=None, derived_from=
predefined_type=None, custom_cascade=False, experimental=False, internal=False,
need_clone=False, need_index=False, gecko_ffi_name=None, depend_on_viewport_size=False,
allowed_in_keyframe_block=True, complex_color=False, cast_type='u8',
has_uncacheable_values=False, logical=False):
has_uncacheable_values=False, logical=False, alias=None):
self.name = name
if not spec:
raise TypeError("Spec should be specified for %s" % name)
Expand All @@ -109,6 +109,7 @@ def __init__(self, style_struct, name, spec=None, animatable=None, derived_from=
self.complex_color = complex_color
self.cast_type = cast_type
self.logical = arg_to_bool(logical)
self.alias = alias.split() if alias else []

# https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property
Expand All @@ -134,7 +135,7 @@ def __init__(self, style_struct, name, spec=None, animatable=None, derived_from=

class Shorthand(object):
def __init__(self, name, sub_properties, spec=None, experimental=False, internal=False,
allowed_in_keyframe_block=True):
allowed_in_keyframe_block=True, alias=None):
self.name = name
if not spec:
raise TypeError("Spec should be specified for %s" % name)
Expand All @@ -145,6 +146,7 @@ def __init__(self, name, sub_properties, spec=None, experimental=False, internal
self.experimental = ("layout.%s.enabled" % name) if experimental else None
self.sub_properties = sub_properties
self.internal = internal
self.alias = alias.split() if alias else []

# https://drafts.csswg.org/css-animations/#keyframes
# > The <declaration-list> inside of <keyframe-block> accepts any CSS property
Expand Down
3 changes: 2 additions & 1 deletion components/style/properties/longhand/inherited_text.mako.rs
Expand Up @@ -170,7 +170,8 @@ ${helpers.single_keyword("overflow-wrap",
"normal break-word",
gecko_constant_prefix="NS_STYLE_OVERFLOWWRAP",
animatable=False,
spec="https://drafts.csswg.org/css-text/#propdef-overflow-wrap")}
spec="https://drafts.csswg.org/css-text/#propdef-overflow-wrap",
alias="word-wrap")}

// TODO(pcwalton): Support `word-break: keep-all` once we have better CJK support.
${helpers.single_keyword("word-break",
Expand Down
31 changes: 21 additions & 10 deletions components/style/properties/properties.mako.rs
Expand Up @@ -48,7 +48,7 @@ macro_rules! property_name {
}

<%!
from data import Method, Keyword, to_rust_ident
from data import Method, Keyword, to_rust_ident, to_camel_case
import os.path
%>

Expand Down Expand Up @@ -758,24 +758,33 @@ impl PropertyId {
pub fn from_nscsspropertyid(id: nsCSSPropertyID) -> Result<Self, ()> {
use gecko_bindings::structs::*;
<%
def alias_to_nscsspropertyid(alias):
return "nsCSSPropertyID_eCSSPropertyAlias_%s" % to_camel_case(alias)
def to_nscsspropertyid(ident):
if ident == "word_wrap":
return "nsCSSPropertyID_eCSSPropertyAlias_WordWrap"

if ident == "float":
ident = "float_"
return "nsCSSPropertyID::eCSSProperty_" + ident
return "nsCSSPropertyID::eCSSProperty_%s" % ident
%>
match id {
% for property in data.longhands:
${to_nscsspropertyid(property.ident)} => {
Ok(PropertyId::Longhand(LonghandId::${property.camel_case}))
}
% for alias in property.alias:
${alias_to_nscsspropertyid(alias)} => {
Ok(PropertyId::Longhand(LonghandId::${property.camel_case}))
}
% endfor
% endfor
% for property in data.shorthands:
${to_nscsspropertyid(property.ident)} => {
Ok(PropertyId::Shorthand(ShorthandId::${property.camel_case}))
}
% for alias in property.alias:
${alias_to_nscsspropertyid(alias)} => {
Ok(PropertyId::Longhand(LonghandId::${property.camel_case}))
}
% endfor
% endfor
_ => Err(())
}
Expand Down Expand Up @@ -2223,12 +2232,14 @@ macro_rules! css_properties_accessors {
% for kind, props in [("Longhand", data.longhands), ("Shorthand", data.shorthands)]:
% for property in props:
% if not property.derived_from and not property.internal:
% if '-' in property.name:
[${property.ident.capitalize()}, Set${property.ident.capitalize()},
% for name in [property.name] + property.alias:
% if '-' in name:
[${to_rust_ident(name).capitalize()}, Set${to_rust_ident(name).capitalize()},
PropertyId::${kind}(${kind}Id::${property.camel_case})],
% endif
[${to_camel_case(name)}, Set${to_camel_case(name)},
PropertyId::${kind}(${kind}Id::${property.camel_case})],
% endif
[${property.camel_case}, Set${property.camel_case},
PropertyId::${kind}(${kind}Id::${property.camel_case})],
% endfor
% endif
% endfor
% endfor
Expand Down
19 changes: 0 additions & 19 deletions components/style/properties/shorthand/inherited_text.mako.rs
Expand Up @@ -4,25 +4,6 @@

<%namespace name="helpers" file="/helpers.mako.rs" />

// Per CSS-TEXT 6.2, "for legacy reasons, UAs must treat `word-wrap` as an alternate name for
// the `overflow-wrap` property, as if it were a shorthand of `overflow-wrap`."
<%helpers:shorthand name="word-wrap" sub_properties="overflow-wrap"
spec="https://drafts.csswg.org/css-text/#propdef-word-wrap">
use properties::longhands::overflow_wrap;

pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result<Longhands, ()> {
Ok(Longhands {
overflow_wrap: Some(try!(overflow_wrap::parse(context, input))),
})
}

impl<'a> LonghandsToSerialize<'a> {
fn to_css_declared<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.overflow_wrap.to_css(dest)
}
}
</%helpers:shorthand>

<%helpers:shorthand name="text-emphasis" products="gecko" sub_properties="text-emphasis-color
text-emphasis-style"
spec="https://drafts.csswg.org/css-text-decor-3/#text-emphasis-property">
Expand Down
18 changes: 0 additions & 18 deletions tests/unit/style/properties/serialization.rs
Expand Up @@ -457,24 +457,6 @@ mod shorthand_serialization {
}
}

#[test]
fn overflow_wrap_should_only_serialize_with_a_single_property() {
use style::properties::longhands::overflow_wrap::computed_value::T as OverflowWrap;

let value = DeclaredValue::Value(OverflowWrap::break_word);

let properties = vec![
PropertyDeclaration::OverflowWrap(value)
];

let serialization = shorthand_properties_to_string(properties);

// word-wrap is considered an outdated alternative to overflow-wrap, but it is currently
// what servo is using in its naming conventions:
// https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap
assert_eq!(serialization, "word-wrap: break-word;");
}

mod outline {
use style::properties::longhands::outline_width::SpecifiedValue as WidthContainer;
use super::*;
Expand Down

0 comments on commit 33966a8

Please sign in to comment.