Bug 1507305 - Add a mechanism to serialize shorthands for getComputedStyle(). r=heycam

This implements the mechanism reusing the animation machinery for now, so it
asserts in a few cases that this wouldn't handle correctly.

For shorthands that have colors and other bits we'd need a more sophisticated
mechanism with a bit more code (that resolves colors and such), but it'd look
something like this regardless, and we should have this in any case.

Differential Revision: https://phabricator.services.mozilla.com/D11944

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2018-11-15 08:25:13 +00:00
parent e31725c66f
commit f3bce5b4dd
3 changed files with 47 additions and 11 deletions

View File

@ -309,7 +309,9 @@ impl PropertyDeclarationBlock {
.find(|(declaration, _)| declaration.id() == property)
}
fn shorthand_to_css(
/// Tries to serialize a given shorthand from the declarations in this
/// block.
pub fn shorthand_to_css(
&self,
shorthand: ShorthandId,
dest: &mut CssStringWriter,

View File

@ -1354,6 +1354,13 @@ impl ShorthandId {
NonCustomPropertyId::from(self).to_nscsspropertyid()
}
/// Converts from a nsCSSPropertyID to a ShorthandId.
#[cfg(feature = "gecko")]
#[inline]
pub fn from_nscsspropertyid(prop: nsCSSPropertyID) -> Result<Self, ()> {
PropertyId::from_nscsspropertyid(prop)?.as_shorthand().map_err(|_| ())
}
/// Get the longhand ids that form this shorthand.
pub fn longhands(&self) -> NonCustomPropertyIterator<LonghandId> {
% for property in data.shorthands:

View File

@ -5398,21 +5398,48 @@ pub extern "C" fn Servo_StyleSet_HasDocumentStateDependency(
#[no_mangle]
pub unsafe extern "C" fn Servo_GetPropertyValue(
computed_values: ComputedStyleBorrowed,
style: ComputedStyleBorrowed,
prop: nsCSSPropertyID,
value: *mut nsAString,
) {
use style::properties::PropertyFlags;
let longhand = LonghandId::from_nscsspropertyid(prop).expect("Not a longhand?");
debug_assert!(
!longhand.flags().contains(PropertyFlags::GETCS_NEEDS_LAYOUT_FLUSH),
"We're not supposed to serialize layout-dependent properties"
);
computed_values.get_longhand_property_value(
longhand,
&mut CssWriter::new(&mut *value),
).unwrap();
if let Ok(longhand) = LonghandId::from_nscsspropertyid(prop) {
debug_assert!(
!longhand.flags().contains(PropertyFlags::GETCS_NEEDS_LAYOUT_FLUSH),
"We're not supposed to serialize layout-dependent properties"
);
style.get_longhand_property_value(
longhand,
&mut CssWriter::new(&mut *value),
).unwrap();
return;
}
let shorthand = ShorthandId::from_nscsspropertyid(prop)
.expect("Not a shorthand nor a longhand?");
let mut block = PropertyDeclarationBlock::new();
// NOTE(emilio): We reuse the animation value machinery to avoid blowing up
// code size, but may need to come up with something different if ever care
// about supporting the cases that assert below. Fortunately we don't right
// now.
for longhand in shorthand.longhands() {
debug_assert!(
!longhand.is_logical(),
"This won't quite do the right thing if we want to serialize \
logical shorthands"
);
debug_assert!(
!longhand.flags().contains(PropertyFlags::GETCS_NEEDS_LAYOUT_FLUSH),
"Layout-dependent properties shouldn't get here"
);
let animated = AnimationValue::from_computed_values(longhand, style)
.expect("Somebody tried to serialize a shorthand with \
non-animatable properties, would need more code \
to do this");
block.push(animated.uncompute(), Importance::Normal);
}
block.shorthand_to_css(shorthand, &mut *value).unwrap();
}
#[no_mangle]