2
0
mirror of https://github.com/Y2Z/monolith synced 2024-11-10 19:10:29 +00:00

Merge pull request #163 from snshn/proper-css-ident-escaping

Escape all special chars within #id and .class CSS selectors
This commit is contained in:
Sunshine 2020-04-11 18:33:41 -04:00 committed by GitHub
commit cd505ddb6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -24,6 +24,7 @@ const CSS_PROPS_WITH_IMAGE_URLS: &[&str] = &[
"suffix", "suffix",
"symbols", "symbols",
]; ];
const CSS_SPECIAL_CHARS: &str = "~!@$%^&*()+=,./'\";:?><[]{}|`#";
pub fn is_image_url_prop(prop_name: &str) -> bool { pub fn is_image_url_prop(prop_name: &str) -> bool {
CSS_PROPS_WITH_IMAGE_URLS CSS_PROPS_WITH_IMAGE_URLS
@ -40,6 +41,18 @@ pub fn enquote(input: String, double: bool) -> String {
} }
} }
pub fn escape(value: &str) -> String {
let mut res = str!(&value);
res = res.replace("\\", "\\\\");
for c in CSS_SPECIAL_CHARS.chars() {
res = res.replace(c, format!("\\{}", c).as_str());
}
res
}
pub fn process_css<'a>( pub fn process_css<'a>(
cache: &mut HashMap<String, String>, cache: &mut HashMap<String, String>,
client: &Client, client: &Client,
@ -122,7 +135,7 @@ pub fn process_css<'a>(
} }
Token::Ident(ref value) => { Token::Ident(ref value) => {
curr_prop = str!(value); curr_prop = str!(value);
result.push_str(&value.replace(":", "\\:")); result.push_str(&escape(value));
} }
Token::AtKeyword(ref value) => { Token::AtKeyword(ref value) => {
curr_rule = str!(value); curr_rule = str!(value);
@ -243,7 +256,7 @@ pub fn process_css<'a>(
} }
Token::IDHash(ref value) => { Token::IDHash(ref value) => {
result.push_str("#"); result.push_str("#");
result.push_str(value); result.push_str(&escape(value));
} }
Token::UnquotedUrl(ref value) => { Token::UnquotedUrl(ref value) => {
let is_import: bool = curr_rule == "import"; let is_import: bool = curr_rule == "import";

View File

@ -225,7 +225,7 @@ div {\n \
} }
#[test] #[test]
fn passing_colons_in_class_names() { fn passing_unusual_indents() {
let cache = &mut HashMap::new(); let cache = &mut HashMap::new();
let client = Client::new(); let client = Client::new();
@ -233,6 +233,10 @@ fn passing_colons_in_class_names() {
.is\\:good:hover {\n \ .is\\:good:hover {\n \
color: green\n\ color: green\n\
}\n\ }\n\
\n\
#\\~\\!\\@\\$\\%\\^\\&\\*\\(\\)\\+\\=\\,\\.\\/\\\\\\'\\\"\\;\\:\\?\\>\\<\\[\\]\\{\\}\\|\\`\\# {\n \
color: black\n\
}\n\
"; ";
assert_eq!( assert_eq!(