bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.19k stars 1.3k forks source link

htmx won't insert <style> tag as main node #1771

Closed jsonberry closed 1 year ago

jsonberry commented 1 year ago

htmx version: 1.9.5

Here's a barebones example for when I attempt to get a style tag as the main node it does not appear in the DOM.

I've tried various techniques and nothing has worked so far. There is one workaround listed below.

I suspect htmx treats style special because I've noticed that it will insert a style tag into the head on load, maybe there's something funky going on there.

Setup

  <button 
    hx-get="/styles"
    hx-swap="outerHTML"
  ></button>
// nestjs
@Get('/styles')
styles(@Res() res: Response) {
  res.setHeader('Content-Type', 'text/html');
  res.send('<style></style>');
}

Expected

<button> to be replaced with <style>

<style></style>

Actual

  <button 
    hx-get="/styles"
    hx-swap="outerHTML"
  ></button>

Workaround

Wrap the <style> in any other tag and the node will populate:

// nestjs
@Get('/styles')
styles(@Res() res: Response) {
  res.setHeader('Content-Type', 'text/html');
  res.send('<span><style></style></span>');
}

Results in:

<span><style></style></span>
jsonberry commented 1 year ago

Early investigation looks like this could be related to some checks on NODE_TYPE

Still very early on in the research, it's my first time checkin' out the source.

najamkhn commented 1 year ago

Just a guess here but changing the mime type to text/css and entirely skipping <style /> in response be any help if its checking for NODE_TYPE?

vdsabev commented 1 year ago

This bug also occurs if <style> is the first in a list of sibling nodes like this:

<style>.not-rendered {}</style>
<div></div>

But not if it's second or third or last:

<div></div>
<style>.rendered-just-fine {}</style>

Or if it's wrapped in another element:

<div>
  <style>.rendered-just-fine {}</style>
</div>
infogulch commented 1 year ago

Does this behave differently if htmx.config.useTemplateFragments config is true?

dmgd commented 1 year ago

Maybe related? https://github.com/bigskysoftware/htmx/pull/1768

dmgd commented 1 year ago

Yeah, #1768 fixes this from what I can see (I just deleted the stuff from the .html files, added a
to one, like in https://github.com/bigskysoftware/htmx/issues/1771#issuecomment-1711140665, and then tried it out by clicking around)

vtohr commented 1 year ago

Does this behave differently if htmx.config.useTemplateFragments config is true?

<script>htmx.config.useTemplateFragments = true; </script>

fixed it just like you suggested... thanks

infogulch commented 1 year ago

1768 is merged, is this fixed?

xhaggi commented 1 year ago

I think this issue will also benefit from https://github.com/bigskysoftware/htmx/pull/1794

alexpetros commented 1 year ago

Resolved by #1768