pub const INNOCUOUS_PROPERTY_VALUE: &str = "zSafezInvalidPropertyValue";
pub fn is_safe_style_property_name(name: &str) -> bool {
!name.is_empty() && name.chars().all(|c| c.is_ascii_alphabetic() || c == '-')
}
pub fn is_safe_style_property_value(value: &str) -> bool {
let mut prev_was_star_or_slash = false;
for c in value.chars() {
if c.is_ascii_alphanumeric()
|| matches!(c, '+' | '-' | '.' | '!' | '#' | '%' | '_' | ' ' | '\t')
{
prev_was_star_or_slash = false;
} else if c == '*' || c == '/' {
if prev_was_star_or_slash {
return false;
}
prev_was_star_or_slash = true;
} else {
return false;
}
}
true
}
pub fn is_safe_css_selector(selector: &str) -> bool {
if selector.is_empty() {
return false;
}
let mut paren_depth: u32 = 0;
let mut bracket_depth: u32 = 0;
let mut in_quote: Option<char> = None;
for c in selector.chars() {
if let Some(q) = in_quote {
if c == q {
in_quote = None;
} else if matches!(c, '<' | '>' | '{' | '}' | '\\') {
return false;
}
continue;
}
match c {
'"' | '\'' => in_quote = Some(c),
'(' => paren_depth += 1,
')' => {
if paren_depth == 0 {
return false;
}
paren_depth -= 1;
}
'[' => bracket_depth += 1,
']' => {
if bracket_depth == 0 {
return false;
}
bracket_depth -= 1;
}
c if c.is_ascii_alphanumeric() => {}
'-' | '_' | '#' | '.' | ':' | '*' | ' ' | '\t' | '\n' | '\r' | ',' | '>' | '+'
| '~' | '=' | '^' | '$' | '|' => {}
_ => return false,
}
}
paren_depth == 0 && bracket_depth == 0 && in_quote.is_none()
}