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 += `