|
| 1 | +use cssparser::*; |
| 2 | +use crate::traits::{Parse, ToCss, PropertyHandler}; |
| 3 | +use crate::printer::Printer; |
| 4 | +use crate::values::length::LengthPercentage; |
| 5 | +use crate::macros::enum_property; |
| 6 | +use crate::error::{ParserError, PrinterError}; |
| 7 | +use crate::properties::{Property, PropertyId}; |
| 8 | +use crate::declaration::DeclarationList; |
| 9 | +use crate::logical::LogicalProperties; |
| 10 | +use crate::compat::Feature; |
| 11 | + |
| 12 | +/// https://drafts.csswg.org/css-sizing-3/#specifying-sizes |
| 13 | +
|
| 14 | +/// https://drafts.csswg.org/css-sizing-3/#preferred-size-properties |
| 15 | +#[derive(Debug, Clone, PartialEq)] |
| 16 | +pub enum Size { |
| 17 | + Auto, |
| 18 | + LengthPercentage(LengthPercentage), |
| 19 | + MinContent, |
| 20 | + MaxContent, |
| 21 | + FitContent(LengthPercentage) |
| 22 | +} |
| 23 | + |
| 24 | +impl Parse for Size { |
| 25 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
| 26 | + if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() { |
| 27 | + return Ok(Size::Auto); |
| 28 | + } |
| 29 | + |
| 30 | + if input.try_parse(|i| i.expect_ident_matching("min-content")).is_ok() { |
| 31 | + return Ok(Size::MinContent); |
| 32 | + } |
| 33 | + |
| 34 | + if input.try_parse(|i| i.expect_ident_matching("max-content")).is_ok() { |
| 35 | + return Ok(Size::MaxContent); |
| 36 | + } |
| 37 | + |
| 38 | + if let Ok(l) = input.try_parse(|input| LengthPercentage::parse(input)) { |
| 39 | + return Ok(Size::LengthPercentage(l)) |
| 40 | + } |
| 41 | + |
| 42 | + if let Ok(l) = parse_fit_content(input) { |
| 43 | + return Ok(Size::FitContent(l)) |
| 44 | + } |
| 45 | + |
| 46 | + Err(input.new_error_for_next_token()) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl ToCss for Size { |
| 51 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write { |
| 52 | + use Size::*; |
| 53 | + match self { |
| 54 | + Auto => dest.write_str("auto"), |
| 55 | + MinContent => dest.write_str("min-content"), |
| 56 | + MaxContent => dest.write_str("max-content"), |
| 57 | + FitContent(l) => { |
| 58 | + dest.write_str("fit-content(")?; |
| 59 | + l.to_css(dest)?; |
| 60 | + dest.write_str(")") |
| 61 | + } |
| 62 | + LengthPercentage(l) => l.to_css(dest) |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// https://drafts.csswg.org/css-sizing-3/#min-size-properties |
| 68 | +/// https://drafts.csswg.org/css-sizing-3/#max-size-properties |
| 69 | +#[derive(Debug, Clone, PartialEq)] |
| 70 | +pub enum MinMaxSize { |
| 71 | + None, |
| 72 | + LengthPercentage(LengthPercentage), |
| 73 | + MinContent, |
| 74 | + MaxContent, |
| 75 | + FitContent(LengthPercentage) |
| 76 | +} |
| 77 | + |
| 78 | +impl Parse for MinMaxSize { |
| 79 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> { |
| 80 | + if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() { |
| 81 | + return Ok(MinMaxSize::None); |
| 82 | + } |
| 83 | + |
| 84 | + if input.try_parse(|i| i.expect_ident_matching("min-content")).is_ok() { |
| 85 | + return Ok(MinMaxSize::MinContent); |
| 86 | + } |
| 87 | + |
| 88 | + if input.try_parse(|i| i.expect_ident_matching("max-content")).is_ok() { |
| 89 | + return Ok(MinMaxSize::MaxContent); |
| 90 | + } |
| 91 | + |
| 92 | + if let Ok(percent) = input.try_parse(|input| LengthPercentage::parse(input)) { |
| 93 | + return Ok(MinMaxSize::LengthPercentage(percent)) |
| 94 | + } |
| 95 | + |
| 96 | + if let Ok(l) = parse_fit_content(input) { |
| 97 | + return Ok(MinMaxSize::FitContent(l)) |
| 98 | + } |
| 99 | + |
| 100 | + Err(input.new_error_for_next_token()) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl ToCss for MinMaxSize { |
| 105 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write { |
| 106 | + use MinMaxSize::*; |
| 107 | + match self { |
| 108 | + None => dest.write_str("none"), |
| 109 | + MinContent => dest.write_str("min-content"), |
| 110 | + MaxContent => dest.write_str("max-content"), |
| 111 | + FitContent(l) => { |
| 112 | + dest.write_str("fit-content(")?; |
| 113 | + l.to_css(dest)?; |
| 114 | + dest.write_str(")") |
| 115 | + } |
| 116 | + LengthPercentage(l) => l.to_css(dest) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +fn parse_fit_content<'i, 't>(input: &mut Parser<'i, 't>) -> Result<LengthPercentage, ParseError<'i, ParserError<'i>>> { |
| 122 | + input.expect_function_matching("fit-content")?; |
| 123 | + input.parse_nested_block(|input| LengthPercentage::parse(input)) |
| 124 | +} |
| 125 | + |
| 126 | +// https://drafts.csswg.org/css-sizing-3/#box-sizing |
| 127 | +enum_property!(BoxSizing, |
| 128 | + ("content-box", ContentBox), |
| 129 | + ("border-box", BorderBox) |
| 130 | +); |
| 131 | + |
| 132 | +#[derive(Default)] |
| 133 | +pub(crate) struct SizeHandler; |
| 134 | + |
| 135 | +impl PropertyHandler for SizeHandler { |
| 136 | + fn handle_property(&mut self, property: &Property, dest: &mut DeclarationList, logical: &mut LogicalProperties) -> bool { |
| 137 | + let logical_supported = logical.is_supported(Feature::LogicalSize); |
| 138 | + |
| 139 | + macro_rules! logical { |
| 140 | + ($prop: ident, $val: ident, $physical: ident) => { |
| 141 | + if logical_supported { |
| 142 | + dest.push(Property::$prop($val.clone())); |
| 143 | + } else { |
| 144 | + dest.push(Property::$physical($val.clone())); |
| 145 | + } |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + match property { |
| 150 | + Property::Width(_) | |
| 151 | + Property::Height(_) | |
| 152 | + Property::MinWidth(_) | |
| 153 | + Property::MaxWidth(_) | |
| 154 | + Property::MinHeight(_) | |
| 155 | + Property::MaxHeight(_) => { |
| 156 | + dest.push(property.clone()); |
| 157 | + } |
| 158 | + Property::BlockSize(size) => logical!(BlockSize, size, Height), |
| 159 | + Property::MinBlockSize(size) => logical!(MinBlockSize, size, MinHeight), |
| 160 | + Property::MaxBlockSize(size) => logical!(MaxBlockSize, size, MaxHeight), |
| 161 | + Property::InlineSize(size) => logical!(InlineSize, size, Width), |
| 162 | + Property::MinInlineSize(size) => logical!(MinInlineSize, size, MinWidth), |
| 163 | + Property::MaxInlineSize(size) => logical!(MaxInlineSize, size, MaxWidth), |
| 164 | + Property::Unparsed(unparsed) => { |
| 165 | + macro_rules! logical_unparsed { |
| 166 | + ($physical: ident) => { |
| 167 | + if logical_supported { |
| 168 | + dest.push(property.clone()); |
| 169 | + } else { |
| 170 | + dest.push(Property::Unparsed(unparsed.with_property_id(PropertyId::$physical))); |
| 171 | + } |
| 172 | + }; |
| 173 | + } |
| 174 | + |
| 175 | + match &unparsed.property_id { |
| 176 | + PropertyId::Width | |
| 177 | + PropertyId::Height | |
| 178 | + PropertyId::MinWidth | |
| 179 | + PropertyId::MaxWidth | |
| 180 | + PropertyId::MinHeight | |
| 181 | + PropertyId::MaxHeight => { |
| 182 | + dest.push(property.clone()); |
| 183 | + } |
| 184 | + PropertyId::BlockSize => logical_unparsed!(Height), |
| 185 | + PropertyId::MinBlockSize => logical_unparsed!(MinHeight), |
| 186 | + PropertyId::MaxBlockSize => logical_unparsed!(MaxHeight), |
| 187 | + PropertyId::InlineSize => logical_unparsed!(Width), |
| 188 | + PropertyId::MinInlineSize => logical_unparsed!(MinWidth), |
| 189 | + PropertyId::MaxInlineSize => logical_unparsed!(MaxWidth), |
| 190 | + _ => return false |
| 191 | + } |
| 192 | + } |
| 193 | + _ => return false |
| 194 | + } |
| 195 | + |
| 196 | + true |
| 197 | + } |
| 198 | + |
| 199 | + fn finalize(&mut self, _: &mut DeclarationList, _: &mut LogicalProperties) {} |
| 200 | +} |
0 commit comments