Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.

Home Page:https://shopify.github.io/liquid/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HTML / unicode encoding issue with line item properties

twilson90 opened this issue · comments

My products have color customization properties. The properties are stored in a product metafield as a json:

[
  {
    "name": "Branches & Birds Colour",
    "required": true,
    "type": "color",
    "colors": [
      {
        "name": "Bright Yellow",
        "value": "#f8dc0d"
      },
      {
        "name": "Light Orange",
        "value": "#f8971f"
      },
      {
        "name": "Orange",
        "value": "#f14a27"
      },
      ...
]

On the product page, the options are rendered as inputs with name="properties[Branches & Birds Colour]"

After adding an item to cart with properties, on the cart page if I print {{ cart.items.first.properties | json }} I get this:

[["Branches \u0026amp; Birds Colour","Leaf Green"],...]

The ampersand is converted into & then the & is unicode encoded... This appears to be the way all properties are encoded in the line item.
The equivalent of {{ string | escape | json }}.

But if I print {{ cart.items.first.product.metafields.namespace.custom_properties | json }} I get:

[{"name": "Branches \u0026 Birds Colour", ... }]

A problem then arises when I want to match the key in the cart to the metafield json property.

Practically, I want to show the color of each customization in the cart (using the hexadecimal value) by matching the property key with the metafield custom property definition, but the encoding messes this up.

I can't use url_decode on the line item property key, the unicode would need to be decoded first, and there appears to be no way in liquid to decode unicode.

So I'm stuck... unless I implement some hacky solution where I replace all instances of '\u0026amp;' with '\u0026', but this would only handle ampersand characters...

Is this a bug or am I missing something? Or is this just one more of Shopify's delightful quirks that I must learn to live with?
Either way, I'm welcome to any ideas on how to overcome this.

Just found another oddity.
If I add to cart an item with properties:
{ "<>":"something" }

In /cart.js the greater than and less than symbols are converted to unicode:
"\u003c\u003e":"something"

However in liquid, if I ever wanted to show the key of the property:

for prop in cart.items[0].properties
    echo prop[0]
endfor

Nothing is printed. Internally it appears that whenever I access a line_item property key (or value), anything resembling an html tag is stripped before it's given to me / printed.

And unless I'm mistaken this only appears to be with line_item properties. I can't recreate this behaviour elsewhere.

@hedgehog90 im curious if you found a solution to the unicode problem - im having the same issue with strings containing single quotes, theyre being converted automatically to & # 3 9 ; and there seems to be no way of filtering this natively in Shopify.

Only solution I think is to embed the json in a script tag of type "application/json" and process it client-side with Javascript.

@twilson90 did you found any solution?

Nope, see above ^