prettier-plugin-liquid icon indicating copy to clipboard operation
prettier-plugin-liquid copied to clipboard

Adding quotes around output markup with `| json` is not a safe formatting operation

Open ChrisCrossCrash opened this issue 11 months ago • 2 comments

Describe the bug

Formatting the document causes quotation marks to be added around output markup that contains | json, which is not a safe formatting operation if the value being piped to the json filter is a string.

Unformatted source No quotes around the tag:

<div
  data-something={{ someStringVar | json }}
></div>

Expected output (no change)

<div
  data-something={{ someStringVar | json }}
></div>

Actual output Adds quotes around tag:

<div
  data-something='{{ someStringVar | json }}'
></div>

Which gets evaluated as:

<div
  data-something=""someValue""
></div>

(Or maybe that's just the way Chrome renders it on the 'Elements' tab. When I copy the outerHTML to a clipboard it looks like "&quot;someValue&quot;")

And then the element's dataset gets changed from:

{
  something: 'someValue'
}

to:

{
  something: '\"someValue\"'
}

Debugging information

  • Ubuntu 20.04
  • Prettier version 3.0.0
  • @shopify/prettier-plugin-liquid version 1.2.1

ChrisCrossCrash avatar Jul 16 '23 15:07 ChrisCrossCrash

Oof. Until this gets fixed, you should be able to use {% # prettier-ignore-attributes %} for this.

(See test case)

charlespwd avatar Jul 17 '23 12:07 charlespwd

I'm not entirely sure what our stance wants to be on this.

Because a variable could contain quotes in it, it would mean that we should never quote swap. If I turn that off, I'll get issues reporting that we don't quote swap :P and don't maintain a consistent style.

charlespwd avatar Jul 17 '23 12:07 charlespwd