lightningcss/values/
url.rs1use 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#[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 #[cfg_attr(feature = "serde", serde(borrow))]
22 pub url: CowArcStr<'i>,
23 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 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 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 pub fn is_absolute(&self) -> bool {
97 let url = self.url.as_ref();
98
99 if url.starts_with('.') {
101 return false;
102 }
103
104 if url.starts_with('/') {
106 return true;
107 }
108
109 if url.starts_with('#') {
113 return true;
114 }
115
116 if !url.starts_with(|c| matches!(c, 'a'..='z' | 'A'..='Z')) {
119 return false;
120 }
121
122 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}