1use super::custom::UnparsedProperty;
4use super::{Property, PropertyId};
5use crate::context::PropertyHandlerContext;
6use crate::declaration::DeclarationList;
7use crate::error::{ParserError, PrinterError};
8use crate::macros::enum_property;
9use crate::prefixes::{is_flex_2009, Feature};
10use crate::traits::{Parse, PropertyHandler, ToCss};
11use crate::vendor_prefix::VendorPrefix;
12#[cfg(feature = "visitor")]
13use crate::visitor::Visit;
14use cssparser::*;
15
16enum_property! {
17 #[allow(missing_docs)]
19 pub enum DisplayOutside {
20 Block,
21 Inline,
22 RunIn,
23 }
24}
25
26#[derive(Debug, Clone, PartialEq)]
28#[cfg_attr(feature = "visitor", derive(Visit))]
29#[cfg_attr(
30 feature = "serde",
31 derive(serde::Serialize, serde::Deserialize),
32 serde(tag = "type", content = "vendorPrefix", rename_all = "kebab-case")
33)]
34#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
35#[allow(missing_docs)]
36pub enum DisplayInside {
37 Flow,
38 FlowRoot,
39 Table,
40 Flex(VendorPrefix),
41 Box(VendorPrefix),
42 Grid,
43 Ruby,
44}
45
46impl<'i> Parse<'i> for DisplayInside {
47 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
48 let location = input.current_source_location();
49 let ident = input.expect_ident()?;
50 match_ignore_ascii_case! { &*ident,
51 "flow" => Ok(DisplayInside::Flow),
52 "flow-root" => Ok(DisplayInside::FlowRoot),
53 "table" => Ok(DisplayInside::Table),
54 "flex" => Ok(DisplayInside::Flex(VendorPrefix::None)),
55 "-webkit-flex" => Ok(DisplayInside::Flex(VendorPrefix::WebKit)),
56 "-ms-flexbox" => Ok(DisplayInside::Flex(VendorPrefix::Ms)),
57 "-webkit-box" => Ok(DisplayInside::Box(VendorPrefix::WebKit)),
58 "-moz-box" => Ok(DisplayInside::Box(VendorPrefix::Moz)),
59 "grid" => Ok(DisplayInside::Grid),
60 "ruby" => Ok(DisplayInside::Ruby),
61 _ => Err(location.new_unexpected_token_error(
62 cssparser::Token::Ident(ident.clone())
63 ))
64 }
65 }
66}
67
68impl ToCss for DisplayInside {
69 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
70 match self {
71 DisplayInside::Flow => dest.write_str("flow")?,
72 DisplayInside::FlowRoot => dest.write_str("flow-root")?,
73 DisplayInside::Table => dest.write_str("table")?,
74 DisplayInside::Flex(prefix) => {
75 prefix.to_css(dest)?;
76 if *prefix == VendorPrefix::Ms {
77 dest.write_str("flexbox")?;
78 } else {
79 dest.write_str("flex")?;
80 }
81 }
82 DisplayInside::Box(prefix) => {
83 prefix.to_css(dest)?;
84 dest.write_str("box")?;
85 }
86 DisplayInside::Grid => dest.write_str("grid")?,
87 DisplayInside::Ruby => dest.write_str("ruby")?,
88 };
89 Ok(())
90 }
91}
92
93impl DisplayInside {
94 fn is_equivalent(&self, other: &DisplayInside) -> bool {
95 match (self, other) {
96 (DisplayInside::Flex(_), DisplayInside::Flex(_)) => true,
97 (DisplayInside::Box(_), DisplayInside::Box(_)) => true,
98 (DisplayInside::Flex(_), DisplayInside::Box(_)) => true,
99 (DisplayInside::Box(_), DisplayInside::Flex(_)) => true,
100 _ => self == other,
101 }
102 }
103}
104
105#[derive(Debug, Clone, PartialEq)]
109#[cfg_attr(feature = "visitor", derive(Visit))]
110#[cfg_attr(
111 feature = "serde",
112 derive(serde::Serialize, serde::Deserialize),
113 serde(rename_all = "camelCase")
114)]
115#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
116pub struct DisplayPair {
117 pub outside: DisplayOutside,
119 pub inside: DisplayInside,
121 pub is_list_item: bool,
123}
124
125impl<'i> Parse<'i> for DisplayPair {
126 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
127 let mut list_item = false;
128 let mut outside = None;
129 let mut inside = None;
130
131 loop {
132 if input.try_parse(|input| input.expect_ident_matching("list-item")).is_ok() {
133 list_item = true;
134 continue;
135 }
136
137 if outside.is_none() {
138 if let Ok(o) = input.try_parse(DisplayOutside::parse) {
139 outside = Some(o);
140 continue;
141 }
142 }
143
144 if inside.is_none() {
145 if let Ok(i) = input.try_parse(DisplayInside::parse) {
146 inside = Some(i);
147 continue;
148 }
149 }
150
151 break;
152 }
153
154 if list_item || inside.is_some() || outside.is_some() {
155 let inside = inside.unwrap_or(DisplayInside::Flow);
156 let outside = outside.unwrap_or(match inside {
157 DisplayInside::Ruby => DisplayOutside::Inline,
161 _ => DisplayOutside::Block,
162 });
163
164 if list_item && !matches!(inside, DisplayInside::Flow | DisplayInside::FlowRoot) {
165 return Err(input.new_custom_error(ParserError::InvalidDeclaration));
166 }
167
168 return Ok(DisplayPair {
169 outside,
170 inside,
171 is_list_item: list_item,
172 });
173 }
174
175 let location = input.current_source_location();
176 let ident = input.expect_ident()?;
177 match_ignore_ascii_case! { &*ident,
178 "inline-block" => Ok(DisplayPair {
179 outside: DisplayOutside::Inline,
180 inside: DisplayInside::FlowRoot,
181 is_list_item: false
182 }),
183 "inline-table" => Ok(DisplayPair {
184 outside: DisplayOutside::Inline,
185 inside: DisplayInside::Table,
186 is_list_item: false
187 }),
188 "inline-flex" => Ok(DisplayPair {
189 outside: DisplayOutside::Inline,
190 inside: DisplayInside::Flex(VendorPrefix::None),
191 is_list_item: false
192 }),
193 "-webkit-inline-flex" => Ok(DisplayPair {
194 outside: DisplayOutside::Inline,
195 inside: DisplayInside::Flex(VendorPrefix::WebKit),
196 is_list_item: false
197 }),
198 "-ms-inline-flexbox" => Ok(DisplayPair {
199 outside: DisplayOutside::Inline,
200 inside: DisplayInside::Flex(VendorPrefix::Ms),
201 is_list_item: false
202 }),
203 "-webkit-inline-box" => Ok(DisplayPair {
204 outside: DisplayOutside::Inline,
205 inside: DisplayInside::Box(VendorPrefix::WebKit),
206 is_list_item: false
207 }),
208 "-moz-inline-box" => Ok(DisplayPair {
209 outside: DisplayOutside::Inline,
210 inside: DisplayInside::Box(VendorPrefix::Moz),
211 is_list_item: false
212 }),
213 "inline-grid" => Ok(DisplayPair {
214 outside: DisplayOutside::Inline,
215 inside: DisplayInside::Grid,
216 is_list_item: false
217 }),
218 _ => Err(location.new_unexpected_token_error(
219 cssparser::Token::Ident(ident.clone())
220 ))
221 }
222 }
223}
224
225impl ToCss for DisplayPair {
226 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
227 match self {
228 DisplayPair {
229 outside: DisplayOutside::Inline,
230 inside: DisplayInside::FlowRoot,
231 is_list_item: false,
232 } => dest.write_str("inline-block")?,
233 DisplayPair {
234 outside: DisplayOutside::Inline,
235 inside: DisplayInside::Table,
236 is_list_item: false,
237 } => dest.write_str("inline-table")?,
238 DisplayPair {
239 outside: DisplayOutside::Inline,
240 inside: DisplayInside::Flex(prefix),
241 is_list_item: false,
242 } => {
243 prefix.to_css(dest)?;
244 if *prefix == VendorPrefix::Ms {
245 dest.write_str("inline-flexbox")?;
246 } else {
247 dest.write_str("inline-flex")?;
248 }
249 }
250 DisplayPair {
251 outside: DisplayOutside::Inline,
252 inside: DisplayInside::Box(prefix),
253 is_list_item: false,
254 } => {
255 prefix.to_css(dest)?;
256 dest.write_str("inline-box")?;
257 }
258 DisplayPair {
259 outside: DisplayOutside::Inline,
260 inside: DisplayInside::Grid,
261 is_list_item: false,
262 } => dest.write_str("inline-grid")?,
263 DisplayPair {
264 outside,
265 inside,
266 is_list_item,
267 } => {
268 let default_outside = match inside {
269 DisplayInside::Ruby => DisplayOutside::Inline,
270 _ => DisplayOutside::Block,
271 };
272
273 let mut needs_space = false;
274 if *outside != default_outside || (*inside == DisplayInside::Flow && !*is_list_item) {
275 outside.to_css(dest)?;
276 needs_space = true;
277 }
278
279 if *inside != DisplayInside::Flow {
280 if needs_space {
281 dest.write_char(' ')?;
282 }
283 inside.to_css(dest)?;
284 needs_space = true;
285 }
286
287 if *is_list_item {
288 if needs_space {
289 dest.write_char(' ')?;
290 }
291 dest.write_str("list-item")?;
292 }
293 }
294 };
295 Ok(())
296 }
297}
298
299enum_property! {
300 #[allow(missing_docs)]
304 pub enum DisplayKeyword {
305 None,
306 Contents,
307 TableRowGroup,
308 TableHeaderGroup,
309 TableFooterGroup,
310 TableRow,
311 TableCell,
312 TableColumnGroup,
313 TableColumn,
314 TableCaption,
315 RubyBase,
316 RubyText,
317 RubyBaseContainer,
318 RubyTextContainer,
319 }
320}
321
322#[derive(Debug, Clone, PartialEq, Parse, ToCss)]
324#[cfg_attr(feature = "visitor", derive(Visit))]
325#[cfg_attr(
326 feature = "serde",
327 derive(serde::Serialize, serde::Deserialize),
328 serde(tag = "type", rename_all = "kebab-case")
329)]
330#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
331#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
332pub enum Display {
333 #[cfg_attr(
335 feature = "serde",
336 serde(with = "crate::serialization::ValueWrapper::<DisplayKeyword>")
337 )]
338 Keyword(DisplayKeyword),
339 Pair(DisplayPair),
341}
342
343enum_property! {
344 pub enum Visibility {
346 Visible,
348 Hidden,
350 Collapse,
352 }
353}
354
355#[derive(Default)]
356pub(crate) struct DisplayHandler<'i> {
357 decls: Vec<Property<'i>>,
358 display: Option<Display>,
359}
360
361impl<'i> PropertyHandler<'i> for DisplayHandler<'i> {
362 fn handle_property(
363 &mut self,
364 property: &Property<'i>,
365 dest: &mut DeclarationList<'i>,
366 context: &mut PropertyHandlerContext<'i, '_>,
367 ) -> bool {
368 if let Property::Display(display) = property {
369 match (&self.display, display) {
370 (Some(Display::Pair(cur)), Display::Pair(new)) => {
371 if cur.outside == new.outside
374 && cur.is_list_item == new.is_list_item
375 && cur.inside != new.inside
376 && cur.inside.is_equivalent(&new.inside)
377 {
378 if context.targets.browsers.is_some() && new.inside == DisplayInside::Flex(VendorPrefix::None) {
382 self.decls.clear();
383 } else if context.targets.browsers.is_none() || cur.inside != DisplayInside::Flex(VendorPrefix::None) {
384 self.decls.push(Property::Display(self.display.clone().unwrap()));
385 }
386 }
387 }
388 _ => {}
389 }
390
391 self.display = Some(display.clone());
392 return true;
393 }
394
395 if matches!(
396 property,
397 Property::Unparsed(UnparsedProperty {
398 property_id: PropertyId::Display,
399 ..
400 })
401 ) {
402 self.finalize(dest, context);
403 dest.push(property.clone());
404 return true;
405 }
406
407 false
408 }
409
410 fn finalize(&mut self, dest: &mut DeclarationList<'i>, context: &mut PropertyHandlerContext<'i, '_>) {
411 if self.display.is_none() {
412 return;
413 }
414
415 dest.extend(self.decls.drain(..));
416
417 if let Some(display) = std::mem::take(&mut self.display) {
418 if let Display::Pair(DisplayPair {
420 inside: DisplayInside::Flex(VendorPrefix::None),
421 outside,
422 ..
423 }) = display
424 {
425 let prefixes = context.targets.prefixes(VendorPrefix::None, Feature::DisplayFlex);
426
427 if let Some(targets) = context.targets.browsers {
428 if is_flex_2009(targets) {
430 if prefixes.contains(VendorPrefix::WebKit) {
431 dest.push(Property::Display(Display::Pair(DisplayPair {
432 inside: DisplayInside::Box(VendorPrefix::WebKit),
433 outside: outside.clone(),
434 is_list_item: false,
435 })));
436 }
437
438 if prefixes.contains(VendorPrefix::Moz) {
439 dest.push(Property::Display(Display::Pair(DisplayPair {
440 inside: DisplayInside::Box(VendorPrefix::Moz),
441 outside: outside.clone(),
442 is_list_item: false,
443 })));
444 }
445 }
446 }
447
448 if prefixes.contains(VendorPrefix::WebKit) {
449 dest.push(Property::Display(Display::Pair(DisplayPair {
450 inside: DisplayInside::Flex(VendorPrefix::WebKit),
451 outside: outside.clone(),
452 is_list_item: false,
453 })));
454 }
455
456 if prefixes.contains(VendorPrefix::Ms) {
457 dest.push(Property::Display(Display::Pair(DisplayPair {
458 inside: DisplayInside::Flex(VendorPrefix::Ms),
459 outside: outside.clone(),
460 is_list_item: false,
461 })));
462 }
463 }
464
465 dest.push(Property::Display(display))
466 }
467 }
468}