use std::num::NonZeroUsize;
use super::*;
pub(super) fn apply_columns(value: &str, style: &mut ComputedStyle) {
let Some(longhands) = crate::css::cascade::declarations::expand_columns_shorthand(value) else {
return;
};
for (name, value) in longhands {
match name {
"column-count" => {
if let Some(count) = parse_column_count(&value) {
style.column_count = count;
}
}
"column-width" => {
if let Some(width) = parse_column_width(&value, style.font_size) {
style.column_width = width;
}
}
"column-height" => {
if let Some(height) = parse_column_height(&value, style.font_size) {
style.column_height = height;
}
}
"column-wrap" => {
if let Some(wrap) = parse_column_wrap(&value) {
style.column_wrap = wrap;
}
}
_ => {}
}
}
}
pub(super) fn parse_column_count(value: &str) -> Option<ColumnCount> {
let value = trim_css_value(value);
if value.eq_ignore_ascii_case("auto") {
return Some(ColumnCount::Auto);
}
value
.parse::<u16>()
.ok()
.filter(|count| *count > 0)
.and_then(|count| NonZeroUsize::new(usize::from(count)))
.map(ColumnCount::Count)
}
pub(super) fn parse_column_width(value: &str, font_size: f32) -> Option<ComputedColumnWidth> {
let value = trim_css_value(value);
if value.eq_ignore_ascii_case("auto") {
Some(ComputedColumnWidth::Auto)
} else {
parse_computed_length_percentage(value, font_size)
.filter(|length| !length.contains_percentage())
.filter(|length| !length_percentage_is_definitely_negative(length))
.map(ComputedColumnWidth::Length)
}
}
pub(super) fn parse_column_height(value: &str, font_size: f32) -> Option<ComputedColumnHeight> {
let value = trim_css_value(value);
if value.eq_ignore_ascii_case("auto") {
Some(ComputedColumnHeight::Auto)
} else {
parse_computed_length_percentage(value, font_size)
.filter(|length| !length.contains_percentage())
.filter(|length| !length_percentage_is_definitely_negative(length))
.map(ComputedColumnHeight::Length)
}
}
pub(super) fn parse_column_wrap(value: &str) -> Option<ColumnWrap> {
match trim_css_value(value).to_ascii_lowercase().as_str() {
"auto" => Some(ColumnWrap::Auto),
"nowrap" => Some(ColumnWrap::Nowrap),
"wrap" => Some(ColumnWrap::Wrap),
_ => None,
}
}
pub(super) fn parse_column_gap(value: &str, font_size: f32) -> Option<ComputedGap> {
if trim_css_value(value).eq_ignore_ascii_case("normal") {
Some(ComputedGap::Normal)
} else {
let gap = parse_computed_border_width(value, font_size)
.or_else(|| parse_computed_length_percentage(value, font_size))?;
(!length_percentage_is_definitely_negative(&gap))
.then_some(ComputedGap::LengthPercentage(gap))
}
}
pub(super) fn parse_column_fill(value: &str) -> Option<ColumnFill> {
match trim_css_value(value).to_ascii_lowercase().as_str() {
"balance" => Some(ColumnFill::Balance),
"balance-all" => Some(ColumnFill::BalanceAll),
"auto" => Some(ColumnFill::Auto),
_ => None,
}
}
pub(super) fn parse_column_span(value: &str) -> Option<ColumnSpan> {
match trim_css_value(value).to_ascii_lowercase().as_str() {
"none" => Some(ColumnSpan::None),
"all" => Some(ColumnSpan::All),
_ => None,
}
}
pub(super) fn parse_gap(value: &str, font_size: f32) -> Option<ComputedGap> {
if trim_css_value(value).eq_ignore_ascii_case("normal") {
Some(ComputedGap::Normal)
} else {
let gap = parse_computed_border_width(value, font_size)
.or_else(|| parse_computed_length_percentage(value, font_size))?;
(!length_percentage_is_definitely_negative(&gap))
.then_some(ComputedGap::LengthPercentage(gap))
}
}
fn length_percentage_is_definitely_negative(value: &ComputedLengthPercentage) -> bool {
value.is_definitely_absolute() && value.length_points() < 0.0
}