Skip to content

Commit

Permalink
Normalize attribute names when testing if a boolean attribute
Browse files Browse the repository at this point in the history
Fixes #1656
  • Loading branch information
jhy committed Oct 20, 2021
1 parent 1e4d127 commit 7eb5a74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -17,6 +17,10 @@ jsoup changelog
element, e.g. ancestor-or-self::*.
<https://github.com/jhy/jsoup/issues/1652>

* Bugfix: boolean attribute names should be case-insensitive, but were not when the parser was configured to preserve
case.
<https://github.com/jhy/jsoup/issues/1656>

*** Release 1.14.3 [2021-Sep-30]
* Improvement: added native XPath support in Element#selectXpath(String)
<https://github.com/jhy/jsoup/pull/1629>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jsoup/nodes/Attribute.java
Expand Up @@ -2,6 +2,7 @@

import org.jsoup.SerializationException;
import org.jsoup.helper.Validate;
import org.jsoup.internal.Normalizer;
import org.jsoup.internal.StringUtil;
import org.jsoup.nodes.Document.OutputSettings.Syntax;

Expand Down Expand Up @@ -211,7 +212,7 @@ protected static boolean shouldCollapseAttribute(final String key, @Nullable fin
* Checks if this attribute name is defined as a boolean attribute in HTML5
*/
public static boolean isBooleanAttribute(final String key) {
return Arrays.binarySearch(booleanAttributes, key) >= 0;
return Arrays.binarySearch(booleanAttributes, Normalizer.lowerCase(key)) >= 0;
}

@Override
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/jsoup/nodes/AttributeTest.java
@@ -1,6 +1,8 @@
package org.jsoup.nodes;

import org.jsoup.Jsoup;
import org.jsoup.parser.ParseSettings;
import org.jsoup.parser.Parser;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -72,4 +74,19 @@ public void html() {
oldVal = attr.setValue("foo");
assertEquals("", oldVal); // string, not null
}

@Test void booleanAttributesAreNotCaseSensitive() {
// https://github.com/jhy/jsoup/issues/1656
assertTrue(Attribute.isBooleanAttribute("required"));
assertTrue(Attribute.isBooleanAttribute("REQUIRED"));
assertTrue(Attribute.isBooleanAttribute("rEQUIREd"));
assertFalse(Attribute.isBooleanAttribute("random string"));

String html = "<a href=autofocus REQUIRED>One</a>";
Document doc = Jsoup.parse(html);
assertEquals("<a href=\"autofocus\" required>One</a>", doc.selectFirst("a").outerHtml());

Document doc2 = Jsoup.parse(html, Parser.htmlParser().settings(ParseSettings.preserveCase));
assertEquals("<a href=\"autofocus\" REQUIRED>One</a>", doc2.selectFirst("a").outerHtml());
}
}

0 comments on commit 7eb5a74

Please sign in to comment.