1#![deny(missing_docs)]
10
11use crate::derives::*;
12use crate::parser::{Parse, ParserContext};
13use crate::typed_om::{KeywordValue, MathSum, NumericValue, ToTyped, TypedValue, UnitValue};
14use crate::values::distance::{ComputeSquaredDistance, SquaredDistance};
15use crate::values::generics::position::IsTreeScoped;
16use crate::Atom;
17pub use cssparser::{serialize_identifier, serialize_name, CowRcStr, Parser};
18pub use cssparser::{SourceLocation, Token};
19use precomputed_hash::PrecomputedHash;
20use selectors::parser::SelectorParseErrorKind;
21use std::fmt::{self, Debug, Write};
22use style_traits::{CssString, CssWriter, ParseError, StyleParseErrorKind, ToCss};
23use thin_vec::ThinVec;
24use to_shmem::impl_trivial_to_shmem;
25
26pub use crate::url::CssUrl;
27
28pub mod animated;
29pub mod computed;
30pub mod distance;
31pub mod generics;
32pub mod resolved;
33pub mod specified;
34pub mod tagged_numeric;
35
36pub type CSSFloat = f32;
38
39#[inline]
42pub fn normalize(v: CSSFloat) -> CSSFloat {
43 if v.is_nan() {
44 0.0
45 } else {
46 v
47 }
48}
49
50pub type CSSInteger = i32;
52
53#[cfg(feature = "gecko")]
55pub fn serialize_atom_identifier<W>(ident: &Atom, dest: &mut W) -> fmt::Result
56where
57 W: Write,
58{
59 ident.with_str(|s| serialize_identifier(s, dest))
60}
61
62#[cfg(feature = "servo")]
64pub fn serialize_atom_identifier<Static, W>(
65 ident: &::string_cache::Atom<Static>,
66 dest: &mut W,
67) -> fmt::Result
68where
69 Static: string_cache::StaticAtomSet,
70 W: Write,
71{
72 serialize_identifier(&ident, dest)
73}
74
75#[cfg(feature = "gecko")]
77pub fn serialize_atom_name<W>(ident: &Atom, dest: &mut W) -> fmt::Result
78where
79 W: Write,
80{
81 ident.with_str(|s| serialize_name(s, dest))
82}
83
84#[cfg(feature = "servo")]
86pub fn serialize_atom_name<Static, W>(
87 ident: &::string_cache::Atom<Static>,
88 dest: &mut W,
89) -> fmt::Result
90where
91 Static: string_cache::StaticAtomSet,
92 W: Write,
93{
94 serialize_name(&ident, dest)
95}
96
97pub fn serialize_number<W>(v: f32, dest: &mut CssWriter<W>) -> fmt::Result
99where
100 W: Write,
101{
102 serialize_specified_dimension(v, "", false, dest)
103}
104
105pub fn serialize_specified_dimension<W>(
107 v: f32,
108 unit: &str,
109 was_calc: bool,
110 dest: &mut CssWriter<W>,
111) -> fmt::Result
112where
113 W: Write,
114{
115 if was_calc {
116 dest.write_str("calc(")?;
117 }
118
119 if !v.is_finite() {
120 if v.is_nan() {
126 dest.write_str("NaN")?;
127 } else if v == f32::INFINITY {
128 dest.write_str("infinity")?;
129 } else if v == f32::NEG_INFINITY {
130 dest.write_str("-infinity")?;
131 }
132
133 if !unit.is_empty() {
134 dest.write_str(" * 1")?;
135 }
136 } else {
137 v.to_css(dest)?;
138 }
139
140 dest.write_str(unit)?;
141
142 if was_calc {
143 dest.write_char(')')?;
144 }
145 Ok(())
146}
147
148#[repr(transparent)]
150#[derive(
151 Clone,
152 Debug,
153 Default,
154 Deref,
155 Eq,
156 Hash,
157 MallocSizeOf,
158 PartialEq,
159 SpecifiedValueInfo,
160 ToComputedValue,
161 ToResolvedValue,
162 ToShmem,
163)]
164pub struct AtomString(pub Atom);
165
166#[cfg(feature = "servo")]
167impl AsRef<str> for AtomString {
168 fn as_ref(&self) -> &str {
169 &*self.0
170 }
171}
172
173impl Parse for AtomString {
174 fn parse<'i>(_: &ParserContext, input: &mut Parser<'i, '_>) -> Result<Self, ParseError<'i>> {
175 Ok(Self(Atom::from(input.expect_string()?.as_ref())))
176 }
177}
178
179impl cssparser::ToCss for AtomString {
180 fn to_css<W>(&self, dest: &mut W) -> fmt::Result
181 where
182 W: Write,
183 {
184 dest.write_char('"')?;
186 #[cfg(feature = "servo")]
187 {
188 cssparser::CssStringWriter::new(dest).write_str(self.as_ref())?;
189 }
190 #[cfg(feature = "gecko")]
191 {
192 self.0
193 .with_str(|s| cssparser::CssStringWriter::new(dest).write_str(s))?;
194 }
195 dest.write_char('"')
196 }
197}
198
199impl style_traits::ToCss for AtomString {
200 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
201 where
202 W: Write,
203 {
204 cssparser::ToCss::to_css(self, dest)
205 }
206}
207
208impl PrecomputedHash for AtomString {
209 #[inline]
210 fn precomputed_hash(&self) -> u32 {
211 self.0.precomputed_hash()
212 }
213}
214
215impl<'a> From<&'a str> for AtomString {
216 #[inline]
217 fn from(string: &str) -> Self {
218 Self(Atom::from(string))
219 }
220}
221
222#[cfg(feature = "servo")]
224#[repr(transparent)]
225#[derive(Deref)]
226pub struct GenericAtomIdent<Set>(pub string_cache::Atom<Set>)
227where
228 Set: string_cache::StaticAtomSet;
229
230#[cfg(feature = "servo")]
232pub type AtomIdent = GenericAtomIdent<stylo_atoms::AtomStaticSet>;
233
234#[cfg(feature = "servo")]
235impl<Set: string_cache::StaticAtomSet> style_traits::SpecifiedValueInfo for GenericAtomIdent<Set> {}
236
237#[cfg(feature = "servo")]
238impl<Set: string_cache::StaticAtomSet> Default for GenericAtomIdent<Set> {
239 fn default() -> Self {
240 Self(string_cache::Atom::default())
241 }
242}
243
244#[cfg(feature = "servo")]
245impl<Set: string_cache::StaticAtomSet> std::fmt::Debug for GenericAtomIdent<Set> {
246 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
247 self.0.fmt(f)
248 }
249}
250
251#[cfg(feature = "servo")]
252impl<Set: string_cache::StaticAtomSet> std::hash::Hash for GenericAtomIdent<Set> {
253 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
254 self.0.hash(state)
255 }
256}
257
258#[cfg(feature = "servo")]
259impl<Set: string_cache::StaticAtomSet> Eq for GenericAtomIdent<Set> {}
260
261#[cfg(feature = "servo")]
262impl<Set: string_cache::StaticAtomSet> PartialEq for GenericAtomIdent<Set> {
263 fn eq(&self, other: &Self) -> bool {
264 self.0 == other.0
265 }
266}
267
268#[cfg(feature = "servo")]
269impl<Set: string_cache::StaticAtomSet> Clone for GenericAtomIdent<Set> {
270 fn clone(&self) -> Self {
271 Self(self.0.clone())
272 }
273}
274
275#[cfg(feature = "servo")]
276impl<Set: string_cache::StaticAtomSet> to_shmem::ToShmem for GenericAtomIdent<Set> {
277 fn to_shmem(&self, builder: &mut to_shmem::SharedMemoryBuilder) -> to_shmem::Result<Self> {
278 use std::mem::ManuallyDrop;
279
280 let atom = self.0.to_shmem(builder)?;
281 Ok(ManuallyDrop::new(Self(ManuallyDrop::into_inner(atom))))
282 }
283}
284
285#[cfg(feature = "servo")]
286impl<Set: string_cache::StaticAtomSet> malloc_size_of::MallocSizeOf for GenericAtomIdent<Set> {
287 fn size_of(&self, ops: &mut malloc_size_of::MallocSizeOfOps) -> usize {
288 self.0.size_of(ops)
289 }
290}
291
292#[cfg(feature = "servo")]
293impl<Set: string_cache::StaticAtomSet> cssparser::ToCss for GenericAtomIdent<Set> {
294 fn to_css<W>(&self, dest: &mut W) -> fmt::Result
295 where
296 W: Write,
297 {
298 serialize_atom_identifier(&self.0, dest)
299 }
300}
301
302#[cfg(feature = "servo")]
303impl<Set: string_cache::StaticAtomSet> style_traits::ToCss for GenericAtomIdent<Set> {
304 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
305 where
306 W: Write,
307 {
308 serialize_atom_identifier(&self.0, dest)
309 }
310}
311
312#[cfg(feature = "servo")]
313impl<Set: string_cache::StaticAtomSet> PrecomputedHash for GenericAtomIdent<Set> {
314 #[inline]
315 fn precomputed_hash(&self) -> u32 {
316 self.0.precomputed_hash()
317 }
318}
319
320#[cfg(feature = "servo")]
321impl<'a, Set: string_cache::StaticAtomSet> From<&'a str> for GenericAtomIdent<Set> {
322 #[inline]
323 fn from(string: &str) -> Self {
324 Self(string_cache::Atom::from(string))
325 }
326}
327
328#[cfg(feature = "servo")]
329impl<Set: string_cache::StaticAtomSet> std::borrow::Borrow<string_cache::Atom<Set>>
330 for GenericAtomIdent<Set>
331{
332 #[inline]
333 fn borrow(&self) -> &string_cache::Atom<Set> {
334 &self.0
335 }
336}
337
338#[cfg(feature = "servo")]
339impl<Set: string_cache::StaticAtomSet> GenericAtomIdent<Set> {
340 #[inline]
342 pub fn new(atom: string_cache::Atom<Set>) -> Self {
343 Self(atom)
344 }
345
346 #[inline]
348 pub fn cast<'a>(atom: &'a string_cache::Atom<Set>) -> &'a Self {
349 let ptr = atom as *const _ as *const Self;
350 unsafe { &*ptr }
352 }
353}
354
355#[cfg(feature = "gecko")]
357#[repr(transparent)]
358#[derive(
359 Clone, Debug, Default, Deref, Eq, Hash, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToShmem,
360)]
361pub struct AtomIdent(pub Atom);
362
363#[cfg(feature = "gecko")]
364impl cssparser::ToCss for AtomIdent {
365 fn to_css<W>(&self, dest: &mut W) -> fmt::Result
366 where
367 W: Write,
368 {
369 serialize_atom_identifier(&self.0, dest)
370 }
371}
372
373#[cfg(feature = "gecko")]
374impl style_traits::ToCss for AtomIdent {
375 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
376 where
377 W: Write,
378 {
379 cssparser::ToCss::to_css(self, dest)
380 }
381}
382
383#[cfg(feature = "gecko")]
384impl PrecomputedHash for AtomIdent {
385 #[inline]
386 fn precomputed_hash(&self) -> u32 {
387 self.0.precomputed_hash()
388 }
389}
390
391#[cfg(feature = "gecko")]
392impl<'a> From<&'a str> for AtomIdent {
393 #[inline]
394 fn from(string: &str) -> Self {
395 Self(Atom::from(string))
396 }
397}
398
399#[cfg(feature = "gecko")]
400impl AtomIdent {
401 #[inline]
403 pub fn new(atom: Atom) -> Self {
404 Self(atom)
405 }
406
407 pub unsafe fn with<F, R>(ptr: *const crate::gecko_bindings::structs::nsAtom, callback: F) -> R
409 where
410 F: FnOnce(&Self) -> R,
411 {
412 Atom::with(ptr, |atom: &Atom| {
413 let atom = atom as *const Atom as *const AtomIdent;
415 callback(&*atom)
416 })
417 }
418
419 #[inline]
421 pub fn cast<'a>(atom: &'a Atom) -> &'a Self {
422 let ptr = atom as *const _ as *const Self;
423 unsafe { &*ptr }
425 }
426}
427
428#[cfg(feature = "gecko")]
429impl std::borrow::Borrow<crate::gecko_string_cache::WeakAtom> for AtomIdent {
430 #[inline]
431 fn borrow(&self) -> &crate::gecko_string_cache::WeakAtom {
432 self.0.borrow()
433 }
434}
435
436pub fn serialize_percentage<W>(value: CSSFloat, dest: &mut CssWriter<W>) -> fmt::Result
438where
439 W: Write,
440{
441 serialize_specified_dimension(value * 100., "%", false, dest)
442}
443
444pub fn serialize_normalized_percentage<W>(value: CSSFloat, dest: &mut CssWriter<W>) -> fmt::Result
446where
447 W: Write,
448{
449 (value * 100.).to_css(dest)?;
450 dest.write_char('%')
451}
452
453pub fn reify_percentage(
455 value: CSSFloat,
456 was_calc: bool,
457 dest: &mut ThinVec<TypedValue>,
458) -> Result<(), ()> {
459 let numeric_value = NumericValue::Unit(UnitValue {
460 value: value * 100.,
461 unit: CssString::from("percent"),
462 });
463
464 if was_calc {
466 dest.push(TypedValue::Numeric(NumericValue::Sum(MathSum {
467 values: ThinVec::from([numeric_value]),
468 })));
469 } else {
470 dest.push(TypedValue::Numeric(numeric_value));
471 }
472
473 Ok(())
474}
475
476#[cfg_attr(feature = "servo", derive(Deserialize, MallocSizeOf, Serialize))]
478#[derive(
479 Clone,
480 Copy,
481 Debug,
482 PartialEq,
483 SpecifiedValueInfo,
484 ToAnimatedValue,
485 ToComputedValue,
486 ToCss,
487 ToResolvedValue,
488)]
489pub enum Impossible {}
490
491impl ComputeSquaredDistance for Impossible {
494 #[inline]
495 fn compute_squared_distance(&self, _other: &Self) -> Result<SquaredDistance, ()> {
496 match *self {}
497 }
498}
499
500impl_trivial_to_shmem!(Impossible);
501
502impl Parse for Impossible {
503 fn parse<'i, 't>(
504 _context: &ParserContext,
505 input: &mut Parser<'i, 't>,
506 ) -> Result<Self, ParseError<'i>> {
507 Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
508 }
509}
510
511#[derive(
513 Animate,
514 Clone,
515 ComputeSquaredDistance,
516 Copy,
517 MallocSizeOf,
518 PartialEq,
519 Parse,
520 SpecifiedValueInfo,
521 ToAnimatedValue,
522 ToAnimatedZero,
523 ToComputedValue,
524 ToCss,
525 ToResolvedValue,
526 ToShmem,
527)]
528pub enum Either<A, B> {
529 First(A),
531 Second(B),
533}
534
535impl<A: Debug, B: Debug> Debug for Either<A, B> {
536 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
537 match *self {
538 Either::First(ref v) => v.fmt(f),
539 Either::Second(ref v) => v.fmt(f),
540 }
541 }
542}
543
544#[derive(
546 Clone,
547 Debug,
548 Default,
549 Eq,
550 Hash,
551 MallocSizeOf,
552 PartialEq,
553 SpecifiedValueInfo,
554 ToAnimatedValue,
555 ToComputedValue,
556 ToResolvedValue,
557 ToShmem,
558)]
559#[repr(C)]
560pub struct CustomIdent(pub Atom);
561
562impl CustomIdent {
563 pub fn parse<'i, 't>(
568 input: &mut Parser<'i, 't>,
569 invalid: &[&str],
570 ) -> Result<Self, ParseError<'i>> {
571 let location = input.current_source_location();
572 let ident = input.expect_ident()?;
573 CustomIdent::from_ident(location, ident, invalid)
574 }
575
576 pub fn from_ident<'i>(
578 location: SourceLocation,
579 ident: &CowRcStr<'i>,
580 excluding: &[&str],
581 ) -> Result<Self, ParseError<'i>> {
582 if !Self::is_valid(ident, excluding) {
583 return Err(
584 location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))
585 );
586 }
587 if excluding.iter().any(|s| ident.eq_ignore_ascii_case(s)) {
588 Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError))
589 } else {
590 Ok(CustomIdent(Atom::from(ident.as_ref())))
591 }
592 }
593
594 fn is_valid(ident: &str, excluding: &[&str]) -> bool {
595 use crate::properties::CSSWideKeyword;
596 if CSSWideKeyword::from_ident(ident).is_ok() || ident.eq_ignore_ascii_case("default") {
601 return false;
602 }
603
604 !excluding.iter().any(|s| ident.eq_ignore_ascii_case(s))
608 }
609}
610
611impl ToCss for CustomIdent {
612 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
613 where
614 W: Write,
615 {
616 serialize_atom_identifier(&self.0, dest)
617 }
618}
619
620impl ToTyped for CustomIdent {
621 fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
622 let s = ToCss::to_css_cssstring(self);
624 dest.push(TypedValue::Keyword(KeywordValue(s)));
625 Ok(())
626 }
627}
628
629#[repr(transparent)]
632#[derive(
633 Clone,
634 Debug,
635 Eq,
636 Hash,
637 MallocSizeOf,
638 PartialEq,
639 SpecifiedValueInfo,
640 ToAnimatedValue,
641 ToComputedValue,
642 ToResolvedValue,
643 ToShmem,
644 Serialize,
645 Deserialize,
646)]
647pub struct DashedIdent(pub Atom);
648
649impl DashedIdent {
650 pub fn from_ident<'i>(
652 location: SourceLocation,
653 ident: &CowRcStr<'i>,
654 ) -> Result<Self, ParseError<'i>> {
655 if !ident.starts_with("--") {
656 return Err(
657 location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone()))
658 );
659 }
660 Ok(Self(Atom::from(ident.as_ref())))
661 }
662
663 pub fn empty() -> Self {
665 Self(atom!(""))
666 }
667
668 pub fn is_empty(&self) -> bool {
670 self.0 == atom!("")
671 }
672
673 pub(crate) fn undashed(&self) -> Atom {
679 assert!(!self.is_empty(), "Can't undash the empty DashedIdent");
680 #[cfg(feature = "gecko")]
681 let name = &self.0.as_slice()[2..];
682 #[cfg(feature = "servo")]
683 let name = &self.0[2..];
684 Atom::from(name)
685 }
686}
687
688impl IsTreeScoped for DashedIdent {
689 fn is_tree_scoped(&self) -> bool {
690 !self.is_empty()
691 }
692}
693
694impl Parse for DashedIdent {
695 fn parse<'i, 't>(
696 _: &ParserContext,
697 input: &mut Parser<'i, 't>,
698 ) -> Result<Self, ParseError<'i>> {
699 let location = input.current_source_location();
700 let ident = input.expect_ident()?;
701 Self::from_ident(location, ident)
702 }
703}
704
705impl ToCss for DashedIdent {
706 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
707 where
708 W: Write,
709 {
710 serialize_atom_identifier(&self.0, dest)
711 }
712}
713
714#[repr(transparent)]
720#[derive(
721 Clone,
722 Debug,
723 Eq,
724 Hash,
725 PartialEq,
726 MallocSizeOf,
727 SpecifiedValueInfo,
728 ToComputedValue,
729 ToResolvedValue,
730 ToShmem,
731)]
732pub struct KeyframesName(Atom);
733
734impl KeyframesName {
735 pub fn from_ident(value: &str) -> Self {
737 Self(Atom::from(value))
738 }
739
740 pub fn none() -> Self {
742 Self(atom!(""))
743 }
744
745 pub fn is_none(&self) -> bool {
747 self.0 == atom!("")
748 }
749
750 #[cfg(feature = "gecko")]
752 pub fn from_atom(atom: Atom) -> Self {
753 Self(atom)
754 }
755
756 pub fn as_atom(&self) -> &Atom {
758 &self.0
759 }
760}
761
762impl Parse for KeyframesName {
763 fn parse<'i, 't>(
764 _: &ParserContext,
765 input: &mut Parser<'i, 't>,
766 ) -> Result<Self, ParseError<'i>> {
767 let location = input.current_source_location();
768 Ok(match *input.next()? {
769 Token::Ident(ref s) => Self(CustomIdent::from_ident(location, s, &["none"])?.0),
770 Token::QuotedString(ref s) if !s.as_ref().is_empty() => Self(Atom::from(s.as_ref())),
772 ref t => return Err(location.new_unexpected_token_error(t.clone())),
773 })
774 }
775}
776
777impl ToCss for KeyframesName {
778 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
779 where
780 W: Write,
781 {
782 if self.is_none() {
783 return dest.write_str("none");
784 }
785
786 fn serialize<W: Write>(string: &str, dest: &mut CssWriter<W>) -> fmt::Result {
787 if CustomIdent::is_valid(string, &["none"]) {
788 serialize_identifier(string, dest)
789 } else {
790 string.to_css(dest)
791 }
792 }
793
794 #[cfg(feature = "gecko")]
795 return self.0.with_str(|s| serialize(s, dest));
796
797 #[cfg(feature = "servo")]
798 return serialize(self.0.as_ref(), dest);
799 }
800}
801
802impl ToTyped for KeyframesName {
803 fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
804 let s = ToCss::to_css_cssstring(self);
805 dest.push(TypedValue::Keyword(KeywordValue(s)));
806 Ok(())
807 }
808}