From ee0b9e597a2205c43c85ec46d429385f428628e7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 20 Jan 2025 12:53:52 -0500 Subject: [PATCH] feat: support inspector on ws swapped elems --- index.js | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index a55b06d..c035ca2 100644 --- a/index.js +++ b/index.js @@ -304,13 +304,33 @@ function bootstrapTemplateWithHTMXetc( htmlString = $.html(); } else if (htmxRequest && blissRoute) { const $ = cheerio.load(htmlString, null, false); - $.root().children().attr("data-bliss-route", blissRoute); - $.root().children().attr("data-bliss-clone", blissClone); - if (blissCopy) $.root().children().attr("data-bliss-copy", blissCopy); + const targets = []; + + // when we return an oob thing from htmx, we'll lose all the attrs when it swaps + // so this adds those attrs to the children of the swap if possible + for (let child of $.root().children()) { + if (child.attribs && child.attribs["hx-swap-oob"]) { + for (let childchild of child.children) { + if (childchild.attribs) { + targets.push(childchild); + } + } + } else { + targets.push(child); + } + } + + // Directly update attributes without wrapping in Cheerio + for (let target of targets) { + target.attribs["data-bliss-route"] = blissRoute; + target.attribs["data-bliss-clone"] = blissClone; + if (blissCopy) target.attribs["data-bliss-copy"] = blissCopy; + } + htmlString = $.html(); if (htmxRequest) { - htmlString += ``; + htmlString += `${headInjection}`; } }