[4.0.0] Settle removes attributes set on swapped-in elements during the settle delay

Chiusa Adatta ai principianti
#4,167 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript
Ambito
frontend

Direzione di ricerca

L’issue indica __startCSSTransitions, il relativo task di ripristino e __copyAttributes; inizia a individuarli nel codice sorgente di htmx e confronta il comportamento di ripristino con la restrizione a quattro attributi aggiunta da #4056. Riproduci il problema con i due file HTML forniti e defaultSettleDelay al suo valore predefinito. Il lavoro è completato quando gli attributi impostati da MutationObserver rimangono dopo il settle e il comportamento di transizione esistente continua a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

An attribute that a script sets on a swapped-in element between the swap and the end of the settle delay is removed
when settle finishes. This breaks libraries that initialise new elements from a MutationObserver (Alpine.js, for
example), when the element has an id that the old content also had.

Reproduce

Serve these two files (python3 -m http.server), open index.html and click Reload menu.

index.html

<!doctype html>
<html>
<head><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/htmx.js"></script></head>
<body>
<button hx-get="menu.html" hx-target="#menu">Reload menu</button>
<div id="menu"><button id="trigger">Options</button></div>
<script>
  // Stands in for a component library that initialises new elements from a MutationObserver
  const init = () => document.getElementById('trigger').setAttribute('aria-expanded', 'false');
  new MutationObserver(init).observe(document.body, {childList: true, subtree: true});
  init();
</script>
</body>
</html>

menu.html

<button id="trigger">Options</button>
  • Expected: <button id="trigger" aria-expanded="false">, as in htmx 2.
  • Actual: <button id="trigger">

What happens to #trigger

Step htmx 2.0.11 htmx four-dev (08c9c82d, includes #4056)
before the request <button id="trigger" aria-expanded="false"> <button id="trigger" aria-expanded="false">
swapped in <button id="trigger" class="htmx-added"> <button id="trigger">
MutationObserver sets the attribute <button id="trigger" class="htmx-added" aria-expanded="false"> <button id="trigger" class="htmx-added" aria-expanded="false">
after settle <button id="trigger" aria-expanded="false"> <button id="trigger">

htmx 4.0.0 ends the same way. There, the swap first copies aria-expanded over from the old element.

Cause

The restore task in __startCSSTransitions calls __copyAttributes(elt, clone), which removes every attribute the
response's markup doesn't have. htmx 2's cloneAttributes only touched class, style, width and height.

#4056 limited the first copy to those four attributes; the restore task needs the same restriction. Setting
defaultSettleDelay: 0 works around it.

Lingua principale
JavaScript
Stelle
49.6k
Fork
1.8k
Merge medio
3g 9h
PR unite (30g)
8

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bigskysoftware/htmx

Tutte le issue di bigskysoftware/htmx

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.