Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue 1537 - OOB does not escape query selector #2319

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

FraserChapman
Copy link

@FraserChapman FraserChapman commented Feb 15, 2024

Description

This change alters how the oobSwap function builds the query selector string. Rather than simply prefixing it with a # it uses the '[id="raw_id"]' format. Where raw_id is the value obtained from getRawAttribute(oobElement, "id")

Ideally this could be done more cleanly with template literals (backtick strings) - but as I understand it you want to support IE11.

This fix allows the call querySelectorAll to return the nodelist of all elements with the ids such as "foo-/bar" - without getting "Failed to execute 'querySelectorAll' on 'Document': '#foo-/bar' is not a valid selector, and whilst not limiting the selection to the first matching id in the document.

Corresponding issue:

#1537

Testing

I ran the htmx.js test suite Version: 1.9.10 and got 0 failures
I also manually tested ids such as "foo-/bar" and it all seems to be working as expected.

Further to this I've added test cases for

'handles elements with IDs containing special characters properly'

and

'handles one swap into multiple elements with the same ID properly'

And re- ran the htmx.js test suite Version: 1.9.10 and got 0 failures

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@Telroshan Telroshan added the enhancement New feature or request label Mar 3, 2024
@@ -799,7 +799,7 @@ return (function () {
* @returns
*/
function oobSwap(oobValue, oobElement, settleInfo) {
var selector = "#" + getRawAttribute(oobElement, "id");
var selector = '[id="' + getRawAttribute(oobElement, "id") + '"]';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be nicer to use CSS.escape()?

let selector = '#' + CSS.escape(getRawAttribute(oobElement, 'id'))

Copy link
Author

@FraserChapman FraserChapman Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it would be nicer...but as I understood it htmx v1 maintains support for Internet Explorer 11 and CSS.escape isn't supported.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm I am curious how this fix will be propagated to v2... Contribution rules says PR only into dev.

Copy link
Author

@FraserChapman FraserChapman Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contribution rules says PR only into dev.

I'm not sure what you mean, this is a PR into bigskysoftware/htmx:dev

image

@Telroshan
Copy link
Collaborator

Hey @FraserChapman , following up late on this (sorry about that!)
If you are still interested in this change, could you resolve conflicts?

as I understood it htmx v1 maintains support for Internet Explorer 11 and CSS.escape isn't supported.

Since then, we released htmx 2 which dropped IE11 support, so feel free to use that method if you want 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants