Skip to content

Commit 4409a0e

Browse files
sbeckerivStranger6667
authored andcommitted
Dedup stylesheet href
Dearest Maintainer, First thank you for your work on this project. I am comparing this to the Roadie gem for ruby. It inlines css into html emails. I have a really fun email that keeps including a script tag. The same dup script tag. I have updated your code to dedup the hrefs you are loading. Thanks again. Your command line tool takes 3.1 seconds where the Roadie gem takes about 2 minutes. Dictated but not reviewed, Becker
1 parent 265a1fc commit 4409a0e

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/lib.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,26 @@ impl<'a> CSSInliner<'a> {
285285
}
286286
}
287287
if self.options.load_remote_stylesheets {
288-
for link_tag in document
288+
let mut links = document
289289
.select("link[rel~=stylesheet]")
290290
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
291-
{
292-
if let Some(href) = link_tag.attributes.borrow().get("href") {
293-
let url = self.get_full_url(href);
294-
let css = self.load_external(url.as_ref())?;
295-
process_css(&document, css.as_str())?;
296-
}
291+
.map(|link_tag| {
292+
link_tag
293+
.attributes
294+
.borrow()
295+
.get("href")
296+
.as_ref()
297+
.unwrap()
298+
.to_string()
299+
.clone()
300+
})
301+
.collect::<Vec<String>>();
302+
links.sort();
303+
links.dedup();
304+
for href in links.iter() {
305+
let url = self.get_full_url(href);
306+
let css = self.load_external(url.as_ref())?;
307+
process_css(&document, css.as_str())?;
297308
}
298309
}
299310
if let Some(extra_css) = &self.options.extra_css {

0 commit comments

Comments
 (0)