mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
Use generics for the border-image-width property
This commit is contained in:
parent
7a214831f0
commit
c8eb277ca5
8 changed files with 115 additions and 189 deletions
|
@ -993,17 +993,17 @@ fn static_assert() {
|
|||
}
|
||||
|
||||
pub fn set_border_image_width(&mut self, v: longhands::border_image_width::computed_value::T) {
|
||||
use properties::longhands::border_image_width::computed_value::SingleComputedValue;
|
||||
use values::generics::border::BorderImageWidthSide;
|
||||
|
||||
% for side in SIDES:
|
||||
match v.${side.index} {
|
||||
SingleComputedValue::Auto => {
|
||||
match v.${side.ident} {
|
||||
BorderImageWidthSide::Auto => {
|
||||
self.gecko.mBorderImageWidth.data_at_mut(${side.index}).set_value(CoordDataValue::Auto)
|
||||
},
|
||||
SingleComputedValue::LengthOrPercentage(l) => {
|
||||
BorderImageWidthSide::Length(l) => {
|
||||
l.to_gecko_style_coord(&mut self.gecko.mBorderImageWidth.data_at_mut(${side.index}))
|
||||
},
|
||||
SingleComputedValue::Number(n) => {
|
||||
BorderImageWidthSide::Number(n) => {
|
||||
self.gecko.mBorderImageWidth.data_at_mut(${side.index}).set_value(CoordDataValue::Factor(n))
|
||||
},
|
||||
}
|
||||
|
|
|
@ -284,190 +284,12 @@ ${helpers.predefined_type("border-image-outset", "LengthOrNumberRect",
|
|||
}
|
||||
</%helpers:longhand>
|
||||
|
||||
<%helpers:longhand name="border-image-width" animation_value_type="none"
|
||||
spec="https://drafts.csswg.org/css-backgrounds/#border-image-width">
|
||||
use std::fmt;
|
||||
use style_traits::ToCss;
|
||||
use values::specified::{LengthOrPercentage, Number};
|
||||
|
||||
pub mod computed_value {
|
||||
use values::computed::{LengthOrPercentage, Number};
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct T(pub SingleComputedValue, pub SingleComputedValue,
|
||||
pub SingleComputedValue, pub SingleComputedValue);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub enum SingleComputedValue {
|
||||
LengthOrPercentage(LengthOrPercentage),
|
||||
Number(Number),
|
||||
Auto,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub struct SpecifiedValue(pub Vec<SingleSpecifiedValue>);
|
||||
|
||||
impl ToCss for computed_value::T {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
try!(self.0.to_css(dest));
|
||||
try!(dest.write_str(" "));
|
||||
try!(self.1.to_css(dest));
|
||||
try!(dest.write_str(" "));
|
||||
try!(self.2.to_css(dest));
|
||||
try!(dest.write_str(" "));
|
||||
self.3.to_css(dest)
|
||||
}
|
||||
}
|
||||
impl ToCss for SpecifiedValue {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
try!(self.0[0].to_css(dest));
|
||||
for value in self.0.iter().skip(1) {
|
||||
try!(dest.write_str(" "));
|
||||
try!(value.to_css(dest));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, HasViewportPercentage, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub enum SingleSpecifiedValue {
|
||||
LengthOrPercentage(LengthOrPercentage),
|
||||
Number(Number),
|
||||
Auto,
|
||||
}
|
||||
|
||||
impl ToCss for computed_value::SingleComputedValue {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
match *self {
|
||||
computed_value::SingleComputedValue::LengthOrPercentage(ref len) => len.to_css(dest),
|
||||
computed_value::SingleComputedValue::Number(number) => number.to_css(dest),
|
||||
computed_value::SingleComputedValue::Auto => dest.write_str("auto"),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ToCss for SingleSpecifiedValue {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
match *self {
|
||||
SingleSpecifiedValue::LengthOrPercentage(ref len) => len.to_css(dest),
|
||||
SingleSpecifiedValue::Number(number) => number.to_css(dest),
|
||||
SingleSpecifiedValue::Auto => dest.write_str("auto"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToComputedValue for SingleSpecifiedValue {
|
||||
type ComputedValue = computed_value::SingleComputedValue;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> computed_value::SingleComputedValue {
|
||||
match *self {
|
||||
SingleSpecifiedValue::LengthOrPercentage(ref len) => {
|
||||
computed_value::SingleComputedValue::LengthOrPercentage(
|
||||
len.to_computed_value(context))
|
||||
},
|
||||
SingleSpecifiedValue::Number(number) =>
|
||||
computed_value::SingleComputedValue::Number(number.to_computed_value(context)),
|
||||
SingleSpecifiedValue::Auto => computed_value::SingleComputedValue::Auto,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &computed_value::SingleComputedValue) -> Self {
|
||||
match *computed {
|
||||
computed_value::SingleComputedValue::LengthOrPercentage(len) => {
|
||||
SingleSpecifiedValue::LengthOrPercentage(
|
||||
ToComputedValue::from_computed_value(&len))
|
||||
},
|
||||
computed_value::SingleComputedValue::Number(number) =>
|
||||
SingleSpecifiedValue::Number(ToComputedValue::from_computed_value(&number)),
|
||||
computed_value::SingleComputedValue::Auto => SingleSpecifiedValue::Auto,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_initial_value() -> computed_value::T {
|
||||
computed_value::T(computed_value::SingleComputedValue::Number(1.0),
|
||||
computed_value::SingleComputedValue::Number(1.0),
|
||||
computed_value::SingleComputedValue::Number(1.0),
|
||||
computed_value::SingleComputedValue::Number(1.0))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_initial_specified_value() -> SpecifiedValue {
|
||||
SpecifiedValue(vec![SingleSpecifiedValue::Number(Number::new(1.0))])
|
||||
}
|
||||
|
||||
impl ToComputedValue for SpecifiedValue {
|
||||
type ComputedValue = computed_value::T;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> computed_value::T {
|
||||
let length = self.0.len();
|
||||
match length {
|
||||
4 => computed_value::T(self.0[0].to_computed_value(context),
|
||||
self.0[1].to_computed_value(context),
|
||||
self.0[2].to_computed_value(context),
|
||||
self.0[3].to_computed_value(context)),
|
||||
3 => computed_value::T(self.0[0].to_computed_value(context),
|
||||
self.0[1].to_computed_value(context),
|
||||
self.0[2].to_computed_value(context),
|
||||
self.0[1].to_computed_value(context)),
|
||||
2 => computed_value::T(self.0[0].to_computed_value(context),
|
||||
self.0[1].to_computed_value(context),
|
||||
self.0[0].to_computed_value(context),
|
||||
self.0[1].to_computed_value(context)),
|
||||
1 => computed_value::T(self.0[0].to_computed_value(context),
|
||||
self.0[0].to_computed_value(context),
|
||||
self.0[0].to_computed_value(context),
|
||||
self.0[0].to_computed_value(context)),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &computed_value::T) -> Self {
|
||||
SpecifiedValue(vec![ToComputedValue::from_computed_value(&computed.0),
|
||||
ToComputedValue::from_computed_value(&computed.1),
|
||||
ToComputedValue::from_computed_value(&computed.2),
|
||||
ToComputedValue::from_computed_value(&computed.3)])
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for SingleSpecifiedValue {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
if input.try(|input| input.expect_ident_matching("auto")).is_ok() {
|
||||
return Ok(SingleSpecifiedValue::Auto);
|
||||
}
|
||||
|
||||
if let Ok(len) = input.try(|input| LengthOrPercentage::parse_non_negative(context, input)) {
|
||||
return Ok(SingleSpecifiedValue::LengthOrPercentage(len));
|
||||
}
|
||||
|
||||
let num = try!(Number::parse_non_negative(context, input));
|
||||
Ok(SingleSpecifiedValue::Number(num))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
|
||||
let mut values = vec![];
|
||||
for _ in 0..4 {
|
||||
let value = input.try(|input| SingleSpecifiedValue::parse(context, input));
|
||||
match value {
|
||||
Ok(val) => values.push(val),
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
if values.len() > 0 {
|
||||
Ok(SpecifiedValue(values))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
</%helpers:longhand>
|
||||
${helpers.predefined_type("border-image-width", "BorderImageWidth",
|
||||
initial_value="computed::BorderImageWidthSide::one().into()",
|
||||
initial_specified_value="specified::BorderImageWidthSide::one().into()",
|
||||
spec="https://drafts.csswg.org/css-backgrounds/#border-image-width",
|
||||
animation_value_type="none",
|
||||
boxed=True)}
|
||||
|
||||
<%helpers:longhand name="border-image-slice" boxed="True" animation_value_type="none"
|
||||
spec="https://drafts.csswg.org/css-backgrounds/#border-image-slice">
|
||||
|
|
24
components/style/values/computed/border.rs
Normal file
24
components/style/values/computed/border.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! Computed types for CSS values related to borders.
|
||||
|
||||
use values::computed::Number;
|
||||
use values::computed::length::LengthOrPercentage;
|
||||
use values::generics::border::BorderImageWidthSide as GenericBorderImageWidthSide;
|
||||
use values::generics::rect::Rect;
|
||||
|
||||
/// A computed value for the `border-image-width` property.
|
||||
pub type BorderImageWidth = Rect<BorderImageWidthSide>;
|
||||
|
||||
/// A computed value for a single side of a `border-image-width` property.
|
||||
pub type BorderImageWidthSide = GenericBorderImageWidthSide<LengthOrPercentage, Number>;
|
||||
|
||||
impl BorderImageWidthSide {
|
||||
/// Returns `1`.
|
||||
#[inline]
|
||||
pub fn one() -> Self {
|
||||
GenericBorderImageWidthSide::Number(1.)
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ use super::specified;
|
|||
|
||||
pub use app_units::Au;
|
||||
pub use cssparser::Color as CSSColor;
|
||||
pub use self::border::{BorderImageWidth, BorderImageWidthSide};
|
||||
pub use self::image::{Gradient, GradientItem, ImageLayer, LineDirection, Image, ImageRect};
|
||||
pub use self::rect::LengthOrNumberRect;
|
||||
pub use super::{Auto, Either, None_};
|
||||
|
@ -38,6 +39,7 @@ pub use self::length::{MaxLength, MozLength};
|
|||
pub use self::position::Position;
|
||||
|
||||
pub mod basic_shape;
|
||||
pub mod border;
|
||||
pub mod image;
|
||||
pub mod length;
|
||||
pub mod position;
|
||||
|
|
34
components/style/values/generics/border.rs
Normal file
34
components/style/values/generics/border.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! Generic types for CSS values related to borders.
|
||||
|
||||
use std::fmt;
|
||||
use style_traits::ToCss;
|
||||
|
||||
/// A generic value for a single side of a `border-image-width` property.
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
#[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, ToComputedValue)]
|
||||
pub enum BorderImageWidthSide<LengthOrPercentage, Number> {
|
||||
/// `<length-or-percentage>`
|
||||
Length(LengthOrPercentage),
|
||||
/// `<number>`
|
||||
Number(Number),
|
||||
/// `auto`
|
||||
Auto,
|
||||
}
|
||||
|
||||
impl<L, N> ToCss for BorderImageWidthSide<L, N>
|
||||
where L: ToCss, N: ToCss,
|
||||
{
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
|
||||
where W: fmt::Write
|
||||
{
|
||||
match *self {
|
||||
BorderImageWidthSide::Length(ref length) => length.to_css(dest),
|
||||
BorderImageWidthSide::Number(ref number) => number.to_css(dest),
|
||||
BorderImageWidthSide::Auto => dest.write_str("auto"),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ use super::CustomIdent;
|
|||
pub use self::basic_shape::serialize_radius_values;
|
||||
|
||||
pub mod basic_shape;
|
||||
pub mod border;
|
||||
pub mod grid;
|
||||
pub mod image;
|
||||
pub mod position;
|
||||
|
|
41
components/style/values/specified/border.rs
Normal file
41
components/style/values/specified/border.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
//! Specified types for CSS values related to borders.
|
||||
|
||||
use cssparser::Parser;
|
||||
use parser::{Parse, ParserContext};
|
||||
use values::generics::border::BorderImageWidthSide as GenericBorderImageWidthSide;
|
||||
use values::generics::rect::Rect;
|
||||
use values::specified::Number;
|
||||
use values::specified::length::LengthOrPercentage;
|
||||
|
||||
/// A specified value for the `border-image-width` property.
|
||||
pub type BorderImageWidth = Rect<BorderImageWidthSide>;
|
||||
|
||||
/// A specified value for a single side of a `border-image-width` property.
|
||||
pub type BorderImageWidthSide = GenericBorderImageWidthSide<LengthOrPercentage, Number>;
|
||||
|
||||
impl BorderImageWidthSide {
|
||||
/// Returns `1`.
|
||||
#[inline]
|
||||
pub fn one() -> Self {
|
||||
GenericBorderImageWidthSide::Number(Number::new(1.))
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse for BorderImageWidthSide {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
if input.try(|i| i.expect_ident_matching("auto")).is_ok() {
|
||||
return Ok(GenericBorderImageWidthSide::Auto);
|
||||
}
|
||||
|
||||
if let Ok(len) = input.try(|i| LengthOrPercentage::parse_non_negative(context, i)) {
|
||||
return Ok(GenericBorderImageWidthSide::Length(len));
|
||||
}
|
||||
|
||||
let num = Number::parse_non_negative(context, input)?;
|
||||
Ok(GenericBorderImageWidthSide::Number(num))
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ use values::specified::calc::CalcNode;
|
|||
#[cfg(feature = "gecko")]
|
||||
pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, JustifyItems};
|
||||
pub use self::rect::LengthOrNumberRect;
|
||||
pub use self::border::{BorderImageWidth, BorderImageWidthSide};
|
||||
pub use self::color::Color;
|
||||
pub use super::generics::grid::GridLine;
|
||||
pub use self::image::{ColorStop, EndingShape as GradientEndingShape, Gradient};
|
||||
|
@ -45,6 +46,7 @@ pub use self::position::{Position, PositionComponent};
|
|||
#[cfg(feature = "gecko")]
|
||||
pub mod align;
|
||||
pub mod basic_shape;
|
||||
pub mod border;
|
||||
pub mod calc;
|
||||
pub mod color;
|
||||
pub mod grid;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue