1use crate::Zero;
8use crate::derives::*;
9use crate::logical_geometry::PhysicalSide;
10use crate::parser::{Parse, ParserContext};
11use crate::values::DashedIdent;
12use crate::values::computed::position::TryTacticAdjustment;
13use crate::values::generics::Optional;
14use crate::values::generics::box_::PositionProperty;
15use crate::values::generics::position::TreeScoped;
16use cssparser::Parser;
17use std::fmt::Write;
18use style_derive::Animate;
19use style_traits::ParseError;
20use style_traits::ToCss;
21use style_traits::{CssWriter, SpecifiedValueInfo};
22
23#[allow(missing_docs)]
25#[derive(
26 Animate,
27 Clone,
28 ComputeSquaredDistance,
29 Copy,
30 Debug,
31 Deserialize,
32 MallocSizeOf,
33 PartialEq,
34 Serialize,
35 SpecifiedValueInfo,
36 ToAnimatedValue,
37 ToAnimatedZero,
38 ToComputedValue,
39 ToCss,
40 ToResolvedValue,
41 ToShmem,
42 ToTyped,
43)]
44#[repr(C, u8)]
45pub enum GenericLengthPercentageOrAuto<LengthPercent> {
46 LengthPercentage(LengthPercent),
47 Auto,
48}
49
50pub use self::GenericLengthPercentageOrAuto as LengthPercentageOrAuto;
51
52impl<LengthPercentage> LengthPercentageOrAuto<LengthPercentage> {
53 #[inline]
55 pub fn auto() -> Self {
56 LengthPercentageOrAuto::Auto
57 }
58
59 #[inline]
61 pub fn is_auto(&self) -> bool {
62 matches!(*self, LengthPercentageOrAuto::Auto)
63 }
64
65 pub fn parse_with(
67 context: &ParserContext,
68 input: &mut Parser,
69 parser: impl FnOnce(&ParserContext, &mut Parser) -> Result<LengthPercentage, ParseError>,
70 ) -> Result<Self, ParseError> {
71 if input.try_parse(|i| i.expect_ident_matching("auto")).is_ok() {
72 return Ok(LengthPercentageOrAuto::Auto);
73 }
74
75 Ok(LengthPercentageOrAuto::LengthPercentage(parser(
76 context, input,
77 )?))
78 }
79}
80
81impl<LengthPercentage> LengthPercentageOrAuto<LengthPercentage>
82where
83 LengthPercentage: Clone,
84{
85 #[inline]
87 pub fn auto_is(&self, f: impl FnOnce() -> LengthPercentage) -> LengthPercentage {
88 match self {
89 LengthPercentageOrAuto::LengthPercentage(length) => length.clone(),
90 LengthPercentageOrAuto::Auto => f(),
91 }
92 }
93
94 #[inline]
96 pub fn non_auto(&self) -> Option<LengthPercentage> {
97 match self {
98 LengthPercentageOrAuto::LengthPercentage(length) => Some(length.clone()),
99 LengthPercentageOrAuto::Auto => None,
100 }
101 }
102
103 pub fn map<T>(&self, f: impl FnOnce(LengthPercentage) -> T) -> LengthPercentageOrAuto<T> {
105 match self {
106 LengthPercentageOrAuto::LengthPercentage(l) => {
107 LengthPercentageOrAuto::LengthPercentage(f(l.clone()))
108 },
109 LengthPercentageOrAuto::Auto => LengthPercentageOrAuto::Auto,
110 }
111 }
112}
113
114impl<LengthPercentage: Zero> Zero for LengthPercentageOrAuto<LengthPercentage> {
115 fn zero() -> Self {
116 LengthPercentageOrAuto::LengthPercentage(Zero::zero())
117 }
118
119 fn is_zero(&self) -> bool {
120 match *self {
121 LengthPercentageOrAuto::LengthPercentage(ref l) => l.is_zero(),
122 LengthPercentageOrAuto::Auto => false,
123 }
124 }
125}
126
127impl<LengthPercentage: Parse> Parse for LengthPercentageOrAuto<LengthPercentage> {
128 fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
129 Self::parse_with(context, input, LengthPercentage::parse)
130 }
131}
132
133#[allow(missing_docs)]
140#[derive(
141 Animate,
142 ComputeSquaredDistance,
143 Clone,
144 Debug,
145 MallocSizeOf,
146 PartialEq,
147 ToAnimatedValue,
148 ToAnimatedZero,
149 ToComputedValue,
150 ToCss,
151 ToResolvedValue,
152 ToShmem,
153 ToTyped,
154)]
155#[repr(C, u8)]
156pub enum GenericSize<LengthPercent> {
157 LengthPercentage(LengthPercent),
158 Auto,
159 #[animation(error)]
160 MaxContent,
161 #[animation(error)]
162 MinContent,
163 #[animation(error)]
164 FitContent,
165 #[cfg(feature = "gecko")]
166 #[animation(error)]
167 MozAvailable,
168 #[animation(error)]
169 WebkitFillAvailable,
170 #[animation(error)]
171 Stretch,
172 #[animation(error)]
173 #[css(function = "fit-content")]
174 FitContentFunction(LengthPercent),
175 AnchorSizeFunction(Box<GenericAnchorSizeFunction<Self>>),
176 AnchorContainingCalcFunction(LengthPercent),
177}
178
179impl<LengthPercent> SpecifiedValueInfo for GenericSize<LengthPercent>
180where
181 LengthPercent: SpecifiedValueInfo,
182{
183 fn collect_completion_keywords(f: style_traits::KeywordsCollectFn) {
184 LengthPercent::collect_completion_keywords(f);
185 f(&[
186 "auto",
187 "fit-content",
188 "max-content",
189 "min-content",
190 ]);
191 if crate::pref!("layout.css.anchor-positioning.enabled", gecko = true) {
192 f(&["anchor-size"]);
193 }
194 if cfg!(feature = "gecko") {
195 f(&["-moz-available"]);
196 }
197 if crate::pref!("layout.css.stretch-size-keyword.enabled") {
198 f(&["stretch"]);
199 }
200 if crate::pref!("layout.css.webkit-fill-available.enabled") {
201 f(&["-webkit-fill-available"]);
202 }
203 }
204}
205
206pub use self::GenericSize as Size;
207
208impl<LengthPercentage> Size<LengthPercentage> {
209 #[inline]
211 pub fn auto() -> Self {
212 Size::Auto
213 }
214
215 #[inline]
217 pub fn is_auto(&self) -> bool {
218 matches!(*self, Size::Auto)
219 }
220}
221
222#[allow(missing_docs)]
224#[derive(
225 Animate,
226 Clone,
227 ComputeSquaredDistance,
228 Debug,
229 MallocSizeOf,
230 PartialEq,
231 ToAnimatedValue,
232 ToAnimatedZero,
233 ToComputedValue,
234 ToCss,
235 ToResolvedValue,
236 ToShmem,
237 ToTyped,
238)]
239#[repr(C, u8)]
240pub enum GenericMaxSize<LengthPercent> {
241 LengthPercentage(LengthPercent),
242 None,
243 #[animation(error)]
244 MaxContent,
245 #[animation(error)]
246 MinContent,
247 #[animation(error)]
248 FitContent,
249 #[cfg(feature = "gecko")]
250 #[animation(error)]
251 MozAvailable,
252 #[animation(error)]
253 WebkitFillAvailable,
254 #[animation(error)]
255 Stretch,
256 #[animation(error)]
257 #[css(function = "fit-content")]
258 FitContentFunction(LengthPercent),
259 AnchorSizeFunction(Box<GenericAnchorSizeFunction<Self>>),
260 AnchorContainingCalcFunction(LengthPercent),
261}
262
263impl<LP> SpecifiedValueInfo for GenericMaxSize<LP>
264where
265 LP: SpecifiedValueInfo,
266{
267 fn collect_completion_keywords(f: style_traits::KeywordsCollectFn) {
268 LP::collect_completion_keywords(f);
269 f(&[
270 "none",
271 "fit-content",
272 "max-content",
273 "min-content",
274 "anchor-size",
275 ]);
276 if cfg!(feature = "gecko") {
277 f(&["-moz-available"]);
278 }
279 if crate::pref!("layout.css.stretch-size-keyword.enabled") {
280 f(&["stretch"]);
281 }
282 if crate::pref!("layout.css.webkit-fill-available.enabled") {
283 f(&["-webkit-fill-available"]);
284 }
285 }
286}
287
288pub use self::GenericMaxSize as MaxSize;
289
290impl<LengthPercentage> MaxSize<LengthPercentage> {
291 #[inline]
293 pub fn none() -> Self {
294 MaxSize::None
295 }
296}
297
298#[derive(
300 Animate,
301 Clone,
302 ComputeSquaredDistance,
303 Copy,
304 Debug,
305 MallocSizeOf,
306 Parse,
307 PartialEq,
308 SpecifiedValueInfo,
309 ToAnimatedValue,
310 ToAnimatedZero,
311 ToComputedValue,
312 ToCss,
313 ToResolvedValue,
314 ToShmem,
315 ToTyped,
316)]
317#[repr(C, u8)]
318pub enum GenericLengthOrNumber<L, N> {
319 Number(N),
324 Length(L),
326}
327
328pub use self::GenericLengthOrNumber as LengthOrNumber;
329
330impl<L, N: Zero> Zero for LengthOrNumber<L, N> {
331 fn zero() -> Self {
332 LengthOrNumber::Number(Zero::zero())
333 }
334
335 fn is_zero(&self) -> bool {
336 match *self {
337 LengthOrNumber::Number(ref n) => n.is_zero(),
338 LengthOrNumber::Length(..) => false,
339 }
340 }
341}
342
343#[derive(
345 Animate,
346 Clone,
347 ComputeSquaredDistance,
348 Copy,
349 Debug,
350 MallocSizeOf,
351 Parse,
352 PartialEq,
353 SpecifiedValueInfo,
354 ToAnimatedValue,
355 ToAnimatedZero,
356 ToComputedValue,
357 ToCss,
358 ToResolvedValue,
359 ToShmem,
360 ToTyped,
361)]
362#[repr(C, u8)]
363#[allow(missing_docs)]
364pub enum GenericLengthPercentageOrNormal<LengthPercent> {
365 LengthPercentage(LengthPercent),
366 Normal,
367}
368
369pub use self::GenericLengthPercentageOrNormal as LengthPercentageOrNormal;
370
371#[derive(
373 Animate,
374 Clone,
375 ComputeSquaredDistance,
376 Copy,
377 Debug,
378 MallocSizeOf,
379 Parse,
380 PartialEq,
381 SpecifiedValueInfo,
382 ToAnimatedValue,
383 ToAnimatedZero,
384 ToComputedValue,
385 ToCss,
386 ToResolvedValue,
387 ToShmem,
388 ToTyped,
389)]
390#[repr(C, u8)]
391#[allow(missing_docs)]
392pub enum GenericFitTolerance<LengthPercent> {
393 Normal,
394 LengthPercentage(LengthPercent),
395 Infinite,
396}
397
398pub use self::GenericFitTolerance as FitTolerance;
399
400impl<LengthPercent> FitTolerance<LengthPercent> {
401 #[inline]
403 pub fn normal() -> Self {
404 FitTolerance::Normal
405 }
406}
407
408impl<LengthPercent> LengthPercentageOrNormal<LengthPercent> {
409 #[inline]
411 pub fn normal() -> Self {
412 LengthPercentageOrNormal::Normal
413 }
414}
415
416#[derive(
421 Animate,
422 Clone,
423 ComputeSquaredDistance,
424 Debug,
425 MallocSizeOf,
426 PartialEq,
427 SpecifiedValueInfo,
428 ToShmem,
429 ToAnimatedValue,
430 ToAnimatedZero,
431 ToComputedValue,
432 ToResolvedValue,
433 Serialize,
434 Deserialize,
435 ToTyped,
436)]
437#[repr(C)]
438#[typed(todo_derive_fields)]
439pub struct GenericAnchorSizeFunction<Fallback> {
440 #[animation(constant)]
443 pub target_element: TreeScoped<DashedIdent>,
444 pub size: AnchorSizeKeyword,
447 pub fallback: Optional<Fallback>,
449}
450
451impl<Fallback: TryTacticAdjustment> TryTacticAdjustment for GenericAnchorSizeFunction<Fallback> {
452 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
453 self.size.try_tactic_adjustment(old_side, new_side);
454 if let Some(fallback) = self.fallback.as_mut() {
455 fallback.try_tactic_adjustment(old_side, new_side);
456 }
457 }
458}
459
460impl<Fallback> ToCss for GenericAnchorSizeFunction<Fallback>
461where
462 Fallback: ToCss,
463{
464 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> std::fmt::Result
465 where
466 W: Write,
467 {
468 dest.write_str("anchor-size(")?;
469 let mut previous_entry_printed = false;
470 if !self.target_element.value.0.is_empty() {
471 previous_entry_printed = true;
472 self.target_element.to_css(dest)?;
473 }
474 if self.size != AnchorSizeKeyword::None {
475 if previous_entry_printed {
476 dest.write_str(" ")?;
477 }
478 previous_entry_printed = true;
479 self.size.to_css(dest)?;
480 }
481 if let Some(f) = self.fallback.as_ref() {
482 if previous_entry_printed {
483 dest.write_str(", ")?;
484 }
485 f.to_css(dest)?;
486 }
487 dest.write_str(")")
488 }
489}
490
491impl<Fallback> Parse for GenericAnchorSizeFunction<Fallback>
492where
493 Fallback: Parse,
494{
495 fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
496 input.expect_function_matching("anchor-size")?;
497 Self::parse_inner(context, input, |i| Fallback::parse(context, i))
498 }
499}
500impl<Fallback> GenericAnchorSizeFunction<Fallback> {
501 pub fn valid_for(&self, position_property: PositionProperty) -> bool {
503 position_property.is_absolutely_positioned()
504 }
505}
506
507pub enum AnchorResolutionResult<'a, LengthPercentage> {
509 Resolved(LengthPercentage),
511 Fallback(&'a LengthPercentage),
513 Invalid,
515}
516
517impl<'a, LengthPercentage> AnchorResolutionResult<'a, LengthPercentage> {
518 pub fn new_anchor_invalid(fallback: Option<&'a LengthPercentage>) -> Self {
520 if let Some(fb) = fallback {
521 return Self::Fallback(fb);
522 }
523 Self::Invalid
524 }
525}
526
527impl<LengthPercentage> GenericAnchorSizeFunction<LengthPercentage> {
528 pub fn parse_inner<F>(
530 context: &ParserContext,
531 input: &mut Parser,
532 f: F,
533 ) -> Result<Self, ParseError>
534 where
535 F: FnOnce(&mut Parser) -> Result<LengthPercentage, ParseError>,
536 {
537 input.parse_nested_block(|i| {
538 let mut target_element = i
539 .try_parse(|i| DashedIdent::parse(context, i))
540 .unwrap_or(DashedIdent::empty());
541 let size = i
542 .try_parse(AnchorSizeKeyword::parse)
543 .unwrap_or(AnchorSizeKeyword::None);
544 if target_element.is_empty() {
545 target_element = i
546 .try_parse(|i| DashedIdent::parse(context, i))
547 .unwrap_or(DashedIdent::empty());
548 }
549 let previous_parsed = !target_element.is_empty() || size != AnchorSizeKeyword::None;
550 let fallback = i
551 .try_parse(|i| {
552 if previous_parsed {
553 i.expect_comma()?;
554 }
555 f(i)
556 })
557 .ok();
558 Ok(GenericAnchorSizeFunction {
559 target_element: TreeScoped::with_default_level(target_element),
560 size,
561 fallback: fallback.into(),
562 })
563 })
564 }
565}
566
567#[derive(
569 Animate,
570 Clone,
571 ComputeSquaredDistance,
572 Copy,
573 Debug,
574 MallocSizeOf,
575 PartialEq,
576 Parse,
577 SpecifiedValueInfo,
578 ToCss,
579 ToShmem,
580 ToAnimatedValue,
581 ToAnimatedZero,
582 ToComputedValue,
583 ToResolvedValue,
584 Serialize,
585 Deserialize,
586)]
587#[repr(u8)]
588pub enum AnchorSizeKeyword {
589 #[css(skip)]
591 None,
592 Width,
594 Height,
596 Block,
598 Inline,
600 SelfBlock,
602 SelfInline,
604}
605
606impl TryTacticAdjustment for AnchorSizeKeyword {
607 fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
608 if old_side.parallel_to(new_side) {
609 return;
610 }
611 *self = match *self {
612 Self::None => Self::None,
613 Self::Width => Self::Height,
614 Self::Height => Self::Width,
615 Self::Block => Self::Inline,
616 Self::Inline => Self::Block,
617 Self::SelfBlock => Self::SelfInline,
618 Self::SelfInline => Self::SelfBlock,
619 }
620 }
621}
622
623#[derive(
626 Animate,
627 Clone,
628 ComputeSquaredDistance,
629 Debug,
630 MallocSizeOf,
631 PartialEq,
632 ToCss,
633 ToShmem,
634 ToAnimatedValue,
635 ToAnimatedZero,
636 ToComputedValue,
637 ToResolvedValue,
638 ToTyped,
639)]
640#[repr(C)]
641pub enum GenericMargin<LP> {
642 LengthPercentage(LP),
644 Auto,
646 AnchorSizeFunction(Box<GenericAnchorSizeFunction<Self>>),
650 AnchorContainingCalcFunction(LP),
653}
654
655#[cfg(feature = "servo")]
656impl<LP> GenericMargin<LP> {
657 #[inline]
659 pub fn is_auto(&self) -> bool {
660 matches!(self, Self::Auto)
661 }
662}
663
664impl<LP> SpecifiedValueInfo for GenericMargin<LP>
665where
666 LP: SpecifiedValueInfo,
667{
668 fn collect_completion_keywords(f: style_traits::KeywordsCollectFn) {
669 LP::collect_completion_keywords(f);
670 f(&["auto"]);
671 if crate::pref!("layout.css.anchor-positioning.enabled", gecko = true) {
672 f(&["anchor-size"]);
673 }
674 }
675}
676
677impl<LP> Zero for GenericMargin<LP>
678where
679 LP: Zero,
680{
681 fn is_zero(&self) -> bool {
682 match self {
683 Self::LengthPercentage(l) => l.is_zero(),
684 Self::Auto | Self::AnchorSizeFunction(_) | Self::AnchorContainingCalcFunction(_) => {
685 false
686 },
687 }
688 }
689
690 fn zero() -> Self {
691 Self::LengthPercentage(LP::zero())
692 }
693}