spreadAttributes()/optionalAttribute(): update() passes HTML encoded values to setAttribute(), client attributes differ from the server rendered ones

Aperta Adatta ai principianti
#194 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia con SpreadAttributesDirective.update() e OptionalAttributeDirective.update() nei file sorgente delle direttive, poi leggi src/util/AttributeParser.js e il test indicato test/unit/directive-spread-attributes.test.js. Verifica entrambe le direttive con valori stringa e JSON, inclusi i casi di accettazione. Il lavoro è completo quando gli attributi client corrispondono all'output analizzato di toString() e l'output di toString() rimane invariato.

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

Descrizione

Goal

An attribute set by spreadAttributes() or optionalAttribute() has the same value after a client render as after parsing the toString() output: the value as passed, JSON for non-strings, without character references.

Context

Found while looking at #192. update() runs the value through encodeAttribute() and passes the result to setAttribute(). setAttribute() takes its argument as the value and does not parse character references, so they end up in the attribute literally. In stringify() the same encoding is right, there the HTML parser decodes it.

const el = document.createElement('a');
new SpreadAttributesDirective(el).update({ href: '/a?b=1&c=2', title: 'a "b" <c>', list: [1, '2'] });

el.getAttribute('href');  // 1.3.0: '/a?b=1&amp;c=2'             expected: '/a?b=1&c=2'
el.getAttribute('title'); // 1.3.0: 'a &quot;b&quot; &lt;c&gt;'   expected: 'a "b" <c>'
el.getAttribute('list');  // 1.3.0: '[1,&quot;2&quot;]'           expected: '[1,"2"]'

new OptionalAttributeDirective(el).update(true, 'href', '/a?b=1&c=2') sets /a?b=1&amp;c=2 the same way.

What follows from it:

  • A spread href with more than one query parameter points to a different URL on the client, the second parameter is named amp;c.
  • Text attributes (title, aria-label, alt, placeholder) carry the character references as text.
  • A JSON value is no JSON anymore: parseAttribute('[1,&quot;2&quot;]') returns the string, parseAttribute('[1,"2"]') returns the array.
  • The toString() output of the same template parses to the unencoded values, so the server rendered attributes and the client rendered ones disagree.

Plain attribute bindings are not affected, AttributePart passes the value to setAttribute() as it is. decodeAttribute() exists in src/util/AttributeParser.js, nothing calls it.

test/unit/directive-spread-attributes.test.js asserts the encoded value on the client (el.getAttribute('list') equals '[1,&quot;2&quot;,3]'), the current behaviour is pinned by that test.

Acceptance criteria

  • After new SpreadAttributesDirective(el).update({ href: '/a?b=1&c=2' }), el.getAttribute('href') is /a?b=1&c=2.
  • After update({ list: [1, '2'] }), JSON.parse(el.getAttribute('list')) deep equals [1, '2'].
  • Same for OptionalAttributeDirective.update(true, name, value).
  • For a template with either directive, the attribute values after a client render equal the ones after parsing its toString() output.
  • The toString() output does not change.

Proposal (non-binding)

Drop encodeAttribute() from both update() methods and pass the string, or the JSON string for non-strings, to setAttribute(). The client assertions in the directive tests change with it. The changelog names it, the attribute values on the client change for anyone who reads them.

Out of scope

  • Quoting in stringify(), that is #193.
  • Omitting undefined/null/NaN values in stringify(), that is #192.
Lingua principale
JavaScript
Stelle
30
Fork
5
Merge medio
1h 57m
PR unite (30g)
10

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 webtides/element-js

Tutte le issue di webtides/element-js

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.