lightningcss/values/
url.rs

1//! CSS url() values.
2
3use crate::dependencies::{Dependency, Location, UrlDependency};
4use crate::error::{ParserError, PrinterError};
5use crate::printer::Printer;
6use crate::traits::{Parse, ToCss};
7use crate::values::string::CowArcStr;
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10use cssparser::*;
11
12/// A CSS [url()](https://www.w3.org/TR/css-values-4/#urls) value and its source location.
13#[derive(Debug, Clone)]
14#[cfg_attr(feature = "visitor", derive(Visit))]
15#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
16#[cfg_attr(feature = "visitor", visit(visit_url, URLS))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
19pub struct Url<'i> {
20  /// The url string.
21  #[cfg_attr(feature = "serde", serde(borrow))]
22  pub url: CowArcStr<'i>,
23  /// The location where the `url()` was seen in the CSS source file.
24  pub loc: Location,
25}
26
27impl<'i> PartialEq for Url<'i> {
28  fn eq(&self, other: &Self) -> bool {
29    self.url == other.url
30  }
31}
32
33impl<'i> Parse<'i> for Url<'i> {
34  fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
35    let loc = input.current_source_location();
36    let url = input.expect_url()?.into();
37    Ok(Url { url, loc: loc.into() })
38  }
39}
40
41impl<'i> ToCss for Url<'i> {
42  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
43  where
44    W: std::fmt::Write,
45  {
46    let dep = if dest.dependencies.is_some() {
47      Some(UrlDependency::new(self, dest.filename()))
48    } else {
49      None
50    };
51
52    // If adding dependencies, always write url() with quotes so that the placeholder can
53    // be replaced without escaping more easily. Quotes may be removed later during minification.
54    if let Some(dep) = dep {
55      dest.write_str("url(")?;
56      serialize_string(&dep.placeholder, dest)?;
57      dest.write_char(')')?;
58
59      if let Some(dependencies) = &mut dest.dependencies {
60        dependencies.push(Dependency::Url(dep))
61      }
62
63      return Ok(());
64    }
65
66    use cssparser::ToCss;
67    if dest.minify {
68      let mut buf = String::new();
69      Token::UnquotedUrl(CowRcStr::from(self.url.as_ref())).to_css(&mut buf)?;
70
71      // If the unquoted url is longer than it would be quoted (e.g. `url("...")`)
72      // then serialize as a string and choose the shorter version.
73      if buf.len() > self.url.len() + 7 {
74        let mut buf2 = String::new();
75        serialize_string(&self.url, &mut buf2)?;
76        if buf2.len() + 5 < buf.len() {
77          dest.write_str("url(")?;
78          dest.write_str(&buf2)?;
79          return dest.write_char(')');
80        }
81      }
82
83      dest.write_str(&buf)?;
84    } else {
85      dest.write_str("url(")?;
86      serialize_string(&self.url, dest)?;
87      dest.write_char(')')?;
88    }
89
90    Ok(())
91  }
92}
93
94impl<'i> Url<'i> {
95  /// Returns whether the URL is absolute, and not relative.
96  pub fn is_absolute(&self) -> bool {
97    let url = self.url.as_ref();
98
99    // Quick checks. If the url starts with '.', it is relative.
100    if url.starts_with('.') {
101      return false;
102    }
103
104    // If the url starts with '/' it is absolute.
105    if url.starts_with('/') {
106      return true;
107    }
108
109    // If the url starts with '#' we have a fragment URL.
110    // These are resolved relative to the document rather than the CSS file.
111    // https://drafts.csswg.org/css-values-4/#local-urls
112    if url.starts_with('#') {
113      return true;
114    }
115
116    // Otherwise, we might have a scheme. These must start with an ascii alpha character.
117    // https://url.spec.whatwg.org/#scheme-start-state
118    if !url.starts_with(|c| matches!(c, 'a'..='z' | 'A'..='Z')) {
119      return false;
120    }
121
122    // https://url.spec.whatwg.org/#scheme-state
123    for b in url.as_bytes() {
124      let c = *b as char;
125      match c {
126        'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {}
127        ':' => return true,
128        _ => break,
129      }
130    }
131
132    false
133  }
134}