phoenix_live_view icon indicating copy to clipboard operation
phoenix_live_view copied to clipboard

Invalid querySelector during reconnect

Open chadwick- opened this issue 3 years ago • 0 comments

Environment

  • Elixir version (elixir -v): 1.13.4
  • Phoenix version (mix deps): 1.6.12
  • Phoenix LiveView version (mix deps): 0.17.11
  • Operating system: MacOS
  • Browsers you attempted to reproduce this bug on (the more the merrier): Chrome, Safari
  • Does the problem persist after removing "assets/node_modules" and trying again? Yes/no: Yes

Actual behavior

the .js method formsForRecovery(html) tries to select existing form elements to recover them after a disconnect.

let newForm = template.content.querySelector(form[id="${form.id}"][${phxChange}="${form.getAttribute(phxChange)}"])

this results in something like: document.querySelector('form[id="myformID"][phx-change="[["push",{"event":"update","target":1}]]"]'); which is an invalid selector. The special characters need escaping. This works... document.querySelector('form[id="myformID"][phx-change="\\[\\[\\"push\\"\\,\\{\\"event\\"\\:\\"update\\"\\,\\"target\\"\\:1\\}\\]\\]"]');

Of note, I have several forms on the page - not sure if a single form would suffer from this but I think so?

Expected behavior

When there is a connection issue, I would expect to see the WebSocket errors, but not the querySelector problem.

I think this would work (it's quieting errors for me, at least): let newForm = template.content.querySelector(form[id="${form.id}"][${phxChange}="${CSS.escape(form.getAttribute(phxChange))}"])

chadwick- avatar Oct 03 '22 14:10 chadwick-