Skip to content

Commit

Permalink
Escape OOB query selector
Browse files Browse the repository at this point in the history
Escape OOB query selector to support special characters in element ids.

Refs bigskysoftware#1537
  • Loading branch information
imankulov committed Oct 6, 2023
1 parent 590fdda commit 0618b20
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/htmx.js
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ return (function () {
* @returns
*/
function oobSwap(oobValue, oobElement, settleInfo) {
var selector = "#" + getRawAttribute(oobElement, "id");
var selector = "#" + CSS.escape(getRawAttribute(oobElement, "id"));
var swapStyle = "outerHTML";
if (oobValue === "true") {
// do nothing
Expand Down
10 changes: 10 additions & 0 deletions test/attributes/hx-swap-oob.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ describe("hx-swap-oob attribute", function () {
byId("d1").innerHTML.should.equal("Swapped0");
})

it('handles special characters in id properly', function () {
this.server.respondWith("GET", "/test", "Clicked<div id='special-chars/.:,!' hx-swap-oob='true'>SwappedSpecialChars</div>");
var div = make('<div hx-get="/test">click me</div>');
make('<div id="special-chars/.:,!"></div>');
div.click();
this.server.respond();
div.innerHTML.should.equal("Clicked");
byId("special-chars/.:,!").innerHTML.should.equal("SwappedSpecialChars");
})

it('handles more than one oob swap properly', function () {
this.server.respondWith("GET", "/test", "Clicked<div id='d1' hx-swap-oob='true'>Swapped1</div><div id='d2' hx-swap-oob='true'>Swapped2</div>");
var div = make('<div hx-get="/test">click me</div>');
Expand Down

0 comments on commit 0618b20

Please sign in to comment.