Problem/Motivation
We see a lot of javascript errors, thrown from the querySelectorAll call in active-link.js, when the URL contains characters that are not allowed by querySelector.
```
Uncaught DOMException: Failed to execute 'querySelectorAll' on 'Element': '[data-drupal-link-system-path="documenten"]:not([hreflang])[data-drupal-link-query='{"id":"848","scroll-to":"[data-document-id='848']"}'],[data-drupal-link-system-path="documenten"][hreflang="en"][data-drupal-link-query='{"id":"848","scroll-to":"[data-document-id='848']"}']' is not a valid selector.
```
Steps to reproduce
On a local Drupal 10.1 installation, navigate to this path:
/?scroll-to=%5Bdata-document-id%3D%27848%27%5D
Open the browser's dev tools, observe the errors.
Proposed resolution
Wrapping the selectors.join(',') call in CSS.escape() fixes the issue.
| Comment | File | Size | Author |
|---|---|---|---|
| #3 | 3396299-3.patch | 587 bytes | _utsavsharma |
| #3 | interdiff_2-3.txt | 430 bytes | _utsavsharma |
| #2 | active-link-css-escape.patch | 586 bytes | roderickgadellaabsl |
Issue fork drupal-3396299
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
roderickgadellaabsl commentedSee patch (attached) for 10.1.x
Comment #3
_utsavsharma commentedFixed failures in #2.
Comment #4
smustgrave commentedShould contain test coverage.
Comment #7
jrbI think this is a duplicate of #2902769: active-link.js throws JS error if query string parameter contains a single quote
Per that issue, it seems that the actual problem is the
'(single quote) in thequeryStringvariable that causes this line of JavaScript (prior to what the patch here changes) to produce what will be an invalid selector:In that issue, the solution is to escape the single quote. That fixes this issue as well. The use of
CSS.escapein the patch of this issue, prevents the errors from being thrown, but I don't think it actually fixes the problem. The resulting selector still won't be valid-- it will just be escaped. I think the solution in that issue is the way to go.Comment #8
jrbComment #9
roderickgadellaabsl commentedHmm it's not just `queryString`, any query parameter could contain a single quote (or other characters that would cause active-link to throw)?
Since active-link pipes content through `querySelector()` that it does not control, I think it's safer to just `CSS.escape()` it all?