use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
use std::borrow::Cow;
const DATA_URI: &AsciiSet = &CONTROLS
.add(b'\r')
.add(b'\n')
.add(b'%')
.add(b'#')
.add(b'(')
.add(b')')
.add(b'<')
.add(b'>')
.add(b'?')
.add(b'[')
.add(b'\\')
.add(b']')
.add(b'^')
.add(b'`')
.add(b'{')
.add(b'|')
.add(b'}');
pub fn encode_svg(svg: &str) -> String {
let encoded = if !svg.contains("http://www.w3.org/2000/svg") {
regex!("<svg").replace(svg, "<svg xmlns='http://www.w3.org/2000/svg'")
} else {
svg.into()
};
let mut encoded = regex!("\"").replace_all(&encoded, "'");
if let Some(captures) = regex!("^[^>]+fill='?(none)'?").captures(&encoded) {
let index = captures.get(1).unwrap();
let mut result = String::new();
for (i, c) in encoded.chars().enumerate() {
if i < index.start() || i >= index.end() {
result.push(c);
}
}
encoded = Cow::from(result);
}
let encoded = regex!(r">\s{1,}<").replace_all(&encoded, "><");
let encoded = regex!(r"\s{2,}").replace_all(&encoded, " ");
let encoded = utf8_percent_encode(&encoded, DATA_URI);
format!("data:image/svg+xml,{}", encoded)
}