Discovered with the help of an LLM.
This issue was discovered by @cosmicdreams with the help of an LLM while working on #3620412: PP-1 Add a render element for hx-partial. I have since verified it with a demo module. I also used an LLM to understand the refactoring choices but refactored by hand.
Problem/Motivation
htmx-assets.js uses parseHTMLUnsafe() to find and process the JS/CSS assets within the htmx response. It then removes those assets from the response and uses the altered markup for the remainder of the htmx processing.
The problem happens when is sent that contains markup that could be validly swapped somewhere but cannot exist on its own, such as a table row. When htmx parses the response it converts to , but when we parse the html first, any tags not permitted are dropped and <tr><td>A</td><td>B</td><td>C</td></tr> becomes ABC.
Steps to reproduce
Download and enable the linked module. Observe the differences in behavior between /hx-partial/explore which swaps a <p> tag and /hx-partial/table.
Proposed resolution
Continue to use response.text() as the pause mechanism
We want to pause execution in htmx and there are only two points that htmx awaits in response processing. The other is on the fetch but that's fragile as other extensions that load after us could blow away our process and it's more complex.
Continue to remove assets
We need to remove assets before the swap because htmx naturally executes the scripts. Rather than removing them early and returning an altered response, remove them before swap. If they are never inserted then we don't need to be concerned with them. If the swap task only contains assets because it originally only contained hx-partial or swap-oob, drop the task to prevent unintended clearing due to the resulting empty tag.
Remaining tasks
- Refactor htmx-assets.js
- move the asset collecting and asset stripping into helper functions
- collect the assets but return the original response
- perform the stripping in
htmx:before:swap - swap tasks that contain only assets because the partials have been removed are dropped to prevent clearing the swap target by swapping in empty content.
- Add functional js tests that verify hx-partial can swap in a stand-alone
<tr> - Document this flow and its interaction with htmx using inline comments
User interface changes
none
Introduced terminology
none
API changes
none
Data model changes
none
Release notes snippet
none
Issue fork drupal-3640307
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
fathershawnComment #3
fathershawnComment #4
fathershawnComment #5
fathershawn