Skip to content

Commit

Permalink
Fix bug in BodyObserver removing wrong elements on detach due to a …
Browse files Browse the repository at this point in the history
…wrong CSS attribute selector
  • Loading branch information
hpehl committed Sep 13, 2024
1 parent 510802e commit d4b5eaa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Fixed

- Fix bug in `BodyObserver` removing wrong elements on detach due to a wrong CSS attribute selector
- Fix bug in `BodyObserver` removing wrong elements on detach due to a wrong CSS attribute selector. See also https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors/Attribute_selectors
- Wrong selector: `<attribute>*=<id>`
- Correct selector: `<attribute>=<id>`
- Correct selector: `<attribute>~=<id>`

## [1.6.8] - 2024-08-21

Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/org/jboss/elemento/BodyObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ private static void onElementsRemoved(MutationRecord record) {

private static boolean isChildOfObservedElement(List<HTMLElement> elements, String attribute, String id) {
for (HTMLElement element : elements) {
if (element.querySelector("[" + attribute + "='" + id + "']") != null) {
// The use of the right attribute selector is important here!
// Multiple attach/detach IDs can be present on an element.

// The selector "~=" matches elements whose value is exactly the value
// or contains the value in its (space-separated) list of values.

// The selector "*=" must not be used!
// It matches elements whose value contains the value anywhere within the string.
if (element.querySelector("[" + attribute + "~='" + id + "']") != null) {
return true;
}
}
Expand Down

0 comments on commit d4b5eaa

Please sign in to comment.