Skip to content

Commit

Permalink
Escape CSS identifiers correctly
Browse files Browse the repository at this point in the history
Fixes #2169

Was allowing ElementSelectorChars instead of CssIdentifierChars to pass unescaped.
  • Loading branch information
jhy committed Jul 16, 2024
1 parent 56a09ca commit 69d2e43
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
* The form associated elements returned by `FormElement.elements()` now reflect changes made to the DOM,
subsequently to the original parse. [2140](https://github.com/jhy/jsoup/issues/2140)

### Bug Fixes

* `Element.cssSelector()` would fail if the element's class contained a `*`
character. [2169](https://github.com/jhy/jsoup/issues/2169)

## 1.18.1 (2024-Jul-10)

### Improvements
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jsoup/parser/TokenQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static String escapeCssIdentifier(String in) {
StringBuilder out = StringUtil.borrowBuilder();
TokenQueue q = new TokenQueue(in);
while (!q.isEmpty()) {
if (q.matchesCssIdentifier(ElementSelectorChars)) {
if (q.matchesCssIdentifier(CssIdentifierChars)) {
out.append(q.consume());
} else {
out.append(ESC).append(q.consume());
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/jsoup/nodes/ElementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2636,6 +2636,18 @@ void prettySerializationRoundTrips(Document.OutputSettings settings) {
assertEquals(selected.first(), div);
}

@Test void cssSelectorWithAstrix() {
// https://github.com/jhy/jsoup/issues/2169
Document doc = Jsoup.parse("<div class='vds-items_flex-end [&amp;_>_*:first-child]:vds-pt_0'>One</div><div class='vds-items_flex-end'>Two</div>");
Element div = doc.expectFirst("div");
String selector = div.cssSelector();
assertEquals("html > body > div.vds-items_flex-end.\\[\\&_\\>_\\*\\:first-child\\]\\:vds-pt_0", selector);

Elements selected = doc.select(selector);
assertEquals(1, selected.size());
assertEquals(selected.first(), div);
}

@Test void orphanSiblings() {
Element el = new Element("div");
assertEquals(0, el.siblingElements().size());
Expand Down

0 comments on commit 69d2e43

Please sign in to comment.