Switch column-count to use predefined integer type

This commit is contained in:
Xidorn Quan 2017-03-17 17:52:45 +11:00
parent 9233882d49
commit ab0e5c9d99
5 changed files with 22 additions and 93 deletions

View file

@ -106,11 +106,14 @@ impl Flow for MulticolFlow {
if let Either::First(column_width) = column_style.column_width {
column_count =
max(1, (content_inline_size + column_gap).0 / (column_width + column_gap).0);
if let Some(specified_column_count) = column_style.column_count.0 {
if let Either::First(specified_column_count) = column_style.column_count {
column_count = min(column_count, specified_column_count as i32);
}
} else {
column_count = column_style.column_count.0.unwrap() as i32;
column_count = match column_style.column_count {
Either::First(n) => n,
_ => unreachable!(),
}
}
column_width =
max(Au(0), (content_inline_size + column_gap) / column_count - column_gap);

View file

@ -3189,11 +3189,11 @@ clip-path
pub fn set_column_count(&mut self, v: longhands::column_count::computed_value::T) {
use gecko_bindings::structs::{NS_STYLE_COLUMN_COUNT_AUTO, nsStyleColumn_kMaxColumnCount};
self.gecko.mColumnCount = match v.0 {
Some(number) => unsafe {
cmp::min(number, nsStyleColumn_kMaxColumnCount)
self.gecko.mColumnCount = match v {
Either::First(number) => unsafe {
cmp::min(number as u32, nsStyleColumn_kMaxColumnCount)
},
None => NS_STYLE_COLUMN_COUNT_AUTO
Either::Second(Auto) => NS_STYLE_COLUMN_COUNT_AUTO
};
}

View file

@ -19,90 +19,14 @@ ${helpers.predefined_type("column-width",
// FIXME: This prop should be animatable.
<%helpers:longhand name="column-count" experimental="True" animatable="False" extra_prefixes="moz"
spec="https://drafts.csswg.org/css-multicol/#propdef-column-count">
use std::fmt;
use style_traits::ToCss;
use values::HasViewportPercentage;
no_viewport_percentage!(SpecifiedValue);
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum SpecifiedValue {
Auto,
Specified(u32),
}
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SpecifiedValue::Auto => dest.write_str("auto"),
SpecifiedValue::Specified(count) => write!(dest, "{}", count),
}
}
}
pub mod computed_value {
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct T(pub Option<u32>);
}
impl ToCss for computed_value::T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match self.0 {
None => dest.write_str("auto"),
Some(count) => write!(dest, "{}", count),
}
}
}
#[inline]
pub fn get_initial_value() -> computed_value::T {
computed_value::T(None)
}
#[inline]
pub fn get_initial_specified_value() -> SpecifiedValue {
SpecifiedValue::Auto
}
impl ToComputedValue for SpecifiedValue {
type ComputedValue = computed_value::T;
#[inline]
fn to_computed_value(&self, _context: &Context) -> computed_value::T {
match *self {
SpecifiedValue::Auto => computed_value::T(None),
SpecifiedValue::Specified(count) =>
computed_value::T(Some(count))
}
}
#[inline]
fn from_computed_value(computed: &computed_value::T) -> Self {
match *computed {
computed_value::T(None) => SpecifiedValue::Auto,
computed_value::T(Some(count)) =>
SpecifiedValue::Specified(count)
}
}
}
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("auto")).is_ok() {
Ok(SpecifiedValue::Auto)
} else {
let count = try!(specified::parse_integer(input));
// Zero is invalid
if count <= 0 {
return Err(())
}
Ok(SpecifiedValue::Specified(count as u32))
}
}
</%helpers:longhand>
${helpers.predefined_type("column-count", "IntegerOrAuto",
"Either::Second(Auto)",
parse_method="parse_positive",
initial_specified_value="Either::Second(Auto)",
experimental="True",
animatable="False",
extra_prefixes="moz",
spec="https://drafts.csswg.org/css-multicol/#propdef-column-count")}
// FIXME: This prop should be animatable.
${helpers.predefined_type("column-gap",

View file

@ -1536,7 +1536,10 @@ impl ComputedValues {
let style = self.get_column();
match style.column_width {
Either::First(_width) => true,
Either::Second(_auto) => style.column_count.0.is_some(),
Either::Second(_auto) => match style.column_count {
Either::First(_n) => true,
Either::Second(_auto) => false,
}
}
}

View file

@ -557,13 +557,12 @@ mod shorthand_serialization {
#[test]
fn columns_should_serialize_correctly() {
use style::properties::longhands::column_count::SpecifiedValue as ColumnCount;
use style::values::{Auto, Either};
let mut properties = Vec::new();
let width = Either::Second(Auto);
let count = ColumnCount::Auto;
let count = Either::Second(Auto);
properties.push(PropertyDeclaration::ColumnWidth(width));
properties.push(PropertyDeclaration::ColumnCount(count));