Skip to content

Commit

Permalink
Normalize attribute names on creation
Browse files Browse the repository at this point in the history
Allows preserved case attributes in input to make it into the output, even if the safelist attribute name was set as lowercase).

Fixes attribute case in #2049
  • Loading branch information
jhy committed Nov 23, 2023
1 parent 94af4ec commit adce86e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/safety/Safelist.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ static class AttributeKey extends TypedValue {
}

static AttributeKey valueOf(String value) {
return new AttributeKey(value);
return new AttributeKey(Normalizer.lowerCase(value));
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/jsoup/safety/CleanerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,19 @@ public void bailsIfRemovingProtocolThatsNotSet() {
@ParameterizedTest @ValueSource(booleans = {true, false})
void cleansCaseSensitiveElements(boolean preserveCase) {
// https://github.com/jhy/jsoup/issues/2049
String html = "<svg><feMerge><feMergeNode kernelMatrix=1 /><feMergeNode><clipPath /></feMergeNode><feMergeNode />";
String html = "<svg><feMerge baseFrequency=2><feMergeNode kernelMatrix=1 /><feMergeNode><clipPath /></feMergeNode><feMergeNode />";
String[] tags = {"svg", "feMerge", "feMergeNode", "clipPath"};
String[] attrs = {"kernelMatrix"};
String[] attrs = {"kernelMatrix", "baseFrequency"};

if (!preserveCase) {
tags = Arrays.stream(tags).map(String::toLowerCase).toArray(String[]::new);
attrs = Arrays.stream(attrs).map(String::toLowerCase).toArray(String[]::new);
}

Safelist safelist = Safelist.none().addTags(tags).addAttributes(":all", attrs);
String clean = Jsoup.clean(html, safelist);
String expected = "<svg>\n" +
" <feMerge>\n" +
" <feMerge baseFrequency=\"2\">\n" +
" <feMergeNode kernelMatrix=\"1\" />\n" +
" <feMergeNode>\n" +
" <clipPath />\n" +
Expand Down

0 comments on commit adce86e

Please sign in to comment.