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