list.mako.rs (5863B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 <%namespace name="helpers" file="/helpers.mako.rs" /> 6 7 <%helpers:shorthand name="list-style" 8 engines="gecko servo" 9 sub_properties="list-style-position list-style-image list-style-type" 10 spec="https://drafts.csswg.org/css-lists/#propdef-list-style"> 11 use crate::properties::longhands::{list_style_image, list_style_position, list_style_type}; 12 use crate::values::specified::Image; 13 14 pub fn parse_value<'i, 't>( 15 context: &ParserContext, 16 input: &mut Parser<'i, 't>, 17 ) -> Result<Longhands, ParseError<'i>> { 18 // `none` is ambiguous until we've finished parsing the shorthands, so we count the number 19 // of times we see it. 20 let mut nones = 0u8; 21 let (mut image, mut position, mut list_style_type, mut any) = (None, None, None, false); 22 loop { 23 if input.try_parse(|input| input.expect_ident_matching("none")).is_ok() { 24 nones = nones + 1; 25 if nones > 2 { 26 return Err(input.new_custom_error(SelectorParseErrorKind::UnexpectedIdent("none".into()))) 27 } 28 any = true; 29 continue 30 } 31 32 if image.is_none() { 33 if let Ok(value) = input.try_parse(|input| list_style_image::parse(context, input)) { 34 image = Some(value); 35 any = true; 36 continue 37 } 38 } 39 40 if position.is_none() { 41 if let Ok(value) = input.try_parse(|input| list_style_position::parse(context, input)) { 42 position = Some(value); 43 any = true; 44 continue 45 } 46 } 47 48 // list-style-type must be checked the last, because it accepts 49 // arbitrary identifier for custom counter style, and thus may 50 // affect values of list-style-position. 51 if list_style_type.is_none() { 52 if let Ok(value) = input.try_parse(|input| list_style_type::parse(context, input)) { 53 list_style_type = Some(value); 54 any = true; 55 continue 56 } 57 } 58 break 59 } 60 61 let position = unwrap_or_initial!(list_style_position, position); 62 63 // If there are two `none`s, then we can't have a type or image; if there is one `none`, 64 // then we can't have both a type *and* an image; if there is no `none` then we're fine as 65 // long as we parsed something. 66 use self::list_style_type::SpecifiedValue as ListStyleType; 67 match (any, nones, list_style_type, image) { 68 (true, 2, None, None) => { 69 Ok(expanded! { 70 list_style_position: position, 71 list_style_image: Image::None, 72 list_style_type: ListStyleType::none(), 73 }) 74 } 75 (true, 1, None, Some(image)) => { 76 Ok(expanded! { 77 list_style_position: position, 78 list_style_image: image, 79 list_style_type: ListStyleType::none(), 80 }) 81 } 82 (true, 1, Some(list_style_type), None) => { 83 Ok(expanded! { 84 list_style_position: position, 85 list_style_image: Image::None, 86 list_style_type: list_style_type, 87 }) 88 } 89 (true, 1, None, None) => { 90 Ok(expanded! { 91 list_style_position: position, 92 list_style_image: Image::None, 93 list_style_type: ListStyleType::none(), 94 }) 95 } 96 (true, 0, list_style_type, image) => { 97 Ok(expanded! { 98 list_style_position: position, 99 list_style_image: unwrap_or_initial!(list_style_image, image), 100 list_style_type: unwrap_or_initial!(list_style_type), 101 }) 102 } 103 _ => Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError)), 104 } 105 } 106 107 impl<'a> ToCss for LonghandsToSerialize<'a> { 108 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write { 109 use longhands::list_style_position::SpecifiedValue as ListStylePosition; 110 use longhands::list_style_type::SpecifiedValue as ListStyleType; 111 use longhands::list_style_image::SpecifiedValue as ListStyleImage; 112 let mut have_one_non_initial_value = false; 113 let position_is_initial = self.list_style_position == &ListStylePosition::Outside; 114 if !position_is_initial { 115 self.list_style_position.to_css(dest)?; 116 have_one_non_initial_value = true; 117 } 118 if self.list_style_image != &ListStyleImage::None { 119 if have_one_non_initial_value { 120 dest.write_char(' ')?; 121 } 122 self.list_style_image.to_css(dest)?; 123 have_one_non_initial_value = true; 124 } 125 if self.list_style_type != &ListStyleType::disc() { 126 if have_one_non_initial_value { 127 dest.write_char(' ')?; 128 } 129 self.list_style_type.to_css(dest)?; 130 have_one_non_initial_value = true; 131 } 132 if !have_one_non_initial_value { 133 self.list_style_position.to_css(dest)?; 134 } 135 Ok(()) 136 } 137 } 138 </%helpers:shorthand>