1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::dependencies::{Dependency, Location, UrlDependency};
use crate::error::{ParserError, PrinterError};
use crate::printer::Printer;
use crate::traits::{Parse, ToCss};
use crate::values::string::CowArcStr;
use cssparser::*;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Url<'i> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub url: CowArcStr<'i>,
pub loc: Location,
}
impl<'i> PartialEq for Url<'i> {
fn eq(&self, other: &Self) -> bool {
self.url == other.url
}
}
impl<'i> Parse<'i> for Url<'i> {
fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
let loc = input.current_source_location();
let url = input.expect_url()?.into();
Ok(Url { url, loc: loc.into() })
}
}
impl<'i> ToCss for Url<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
let dep = if dest.dependencies.is_some() {
Some(UrlDependency::new(self, dest.filename()))
} else {
None
};
if let Some(dep) = dep {
dest.write_str("url(")?;
serialize_string(&dep.placeholder, dest)?;
dest.write_char(')')?;
if let Some(dependencies) = &mut dest.dependencies {
dependencies.push(Dependency::Url(dep))
}
return Ok(());
}
use cssparser::ToCss;
if dest.minify {
let mut buf = String::new();
Token::UnquotedUrl(CowRcStr::from(self.url.as_ref())).to_css(&mut buf)?;
if buf.len() > self.url.len() + 7 {
let mut buf2 = String::new();
serialize_string(&self.url, &mut buf2)?;
if buf2.len() + 5 < buf.len() {
dest.write_str("url(")?;
dest.write_str(&buf2)?;
return dest.write_char(')');
}
}
dest.write_str(&buf)?;
} else {
dest.write_str("url(")?;
serialize_string(&self.url, dest)?;
dest.write_char(')')?;
}
Ok(())
}
}
impl<'i> Url<'i> {
pub fn is_absolute(&self) -> bool {
let url = self.url.as_ref();
if url.starts_with('.') {
return false;
}
if url.starts_with('/') {
return true;
}
if url.starts_with('#') {
return true;
}
if !url.starts_with(|c| matches!(c, 'a'..='z' | 'A'..='Z')) {
return false;
}
for b in url.as_bytes() {
let c = *b as char;
match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {}
':' => return true,
_ => break,
}
}
false
}
}