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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/htmx.js
Expand Up @@ -799,7 +799,7 @@ return (function () {
* @returns
*/
function oobSwap(oobValue, oobElement, settleInfo) {
var selector = "#" + getRawAttribute(oobElement, "id");
var selector = '[id="' + getRawAttribute(oobElement, "id") + '"]';
var swapStyle = "outerHTML";
if (oobValue === "true") {
// do nothing
Expand Down
23 changes: 23 additions & 0 deletions test/attributes/hx-swap-oob.js
Expand Up @@ -128,5 +128,28 @@ describe("hx-swap-oob attribute", function () {
this.server.respond();
should.equal(byId("d1"), null);
});

it('handles elements with IDs containing special characters properly', function () {
this.server.respondWith("GET", "/test", "<div id='foo-/bar/' hx-swap-oob='innerHTML'>Swapped10</div>");
var div = make('<div hx-get="/test">click me</div>');
make('<div id="foo-/bar/">Existing Content</div>');
div.click();
this.server.respond();
var swappedElement = document.querySelector('[id="foo-/bar/"]');
swappedElement.innerHTML.should.equal("Swapped10");
})

it('handles one swap into multiple elements with the same ID properly', function () {
this.server.respondWith("GET", "/test", "<div id='foo-/bar/' hx-swap-oob='innerHTML'>Swapped11</div>");
var div = make('<div hx-get="/test">click me</div>');
make('<div id="foo-/bar/">Existing Content 1</div>');
make('<div id="foo-/bar/">Existing Content 2</div>');
div.click();
this.server.respond();
var swappedElements = document.querySelectorAll('[id="foo-/bar/"]');
swappedElements.forEach(function(element) {
element.innerHTML.should.equal("Swapped11");
});
})
});