Skip to content

Commit

Permalink
Split class attribute by ASCII whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
KamilaBorowska committed Jan 19, 2022
1 parent 9d62f27 commit d83baf1
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2137,7 +2137,8 @@ impl<'a> Builder<'a> {
for attr in &mut *attrs.borrow_mut() {
if &attr.name.local == "class" {
let mut classes = vec![];
for class in attr.value.split(' ') {
// https://html.spec.whatwg.org/#global-attributes:classes-2
for class in attr.value.split_ascii_whitespace() {
if allowed_values.contains(class) {
classes.push(class.to_owned());
}
Expand Down Expand Up @@ -3137,6 +3138,20 @@ mod test {
);
}
#[test]
fn allowed_classes_ascii_whitespace() {
// According to https://infra.spec.whatwg.org/#ascii-whitespace,
// TAB (\t), LF (\n), FF (\x0C), CR (\x0D) and SPACE (\x20) are
// considered to be ASCII whitespace. Unicode whitespace characters
// and VT (\x0B) aren't ASCII whitespace.
let fragment = "<p class=\"a\tb\nc\x0Cd\re f\x0B g\u{2000}\">";
let result = Builder::new()
.allowed_classes(hashmap![
"p" => hashset!["a", "b", "c", "d", "e", "f", "g"],
])
.clean(fragment);
assert_eq!(result.to_string(), r#"<p class="a b c d e"></p>"#);
}
#[test]
fn remove_non_allowed_attributes_with_tag_attribute_values() {
let fragment = "<p data-label=\"baz\" name=\"foo\"></p>";
let result = Builder::new()
Expand Down

0 comments on commit d83baf1

Please sign in to comment.