fix: use single quotes for x-data attributes with tojson

The Jinja |tojson filter outputs JSON with double quotes. When used
inside a double-quoted HTML attribute, these quotes break the attribute
parsing causing "expected expression, got '}'" errors.

Solution: Use single quotes for x-data attributes so JSON double quotes
don't conflict:
  <div x-data='languageSelector("fr", {{ langs|tojson }})'>

Updated:
- language_selector.html macro (all 3 variants)
- shop/base.html language selector
- LANG-002 and LANG-003 architecture rules documentation
- Validator to detect double-quoted x-data with tojson

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-13 23:05:09 +01:00
parent 9920430b9e
commit ea64ff8eae
4 changed files with 29 additions and 27 deletions

View File

@@ -48,12 +48,12 @@ language_rules:
...
}">
RIGHT (function with tojson - NO safe in HTML attributes):
<div x-data="languageSelector('{{ lang }}', {{ langs|tojson }})">
RIGHT (function call with single quotes for x-data attribute):
<div x-data='languageSelector("{{ lang }}", {{ langs|tojson }})'>
NOTE: Use |tojson (without |safe) in HTML attributes so quotes
become &quot; which the browser correctly decodes. Using |safe
would output raw quotes that break the attribute parsing.
IMPORTANT: Use SINGLE QUOTES for the x-data attribute value because
|tojson outputs double quotes in JSON arrays. Single quotes on the
outside prevent the JSON double quotes from breaking HTML parsing.
The languageSelector function must be defined in:
- static/shop/js/shop-layout.js (for storefront)
@@ -75,9 +75,12 @@ language_rules:
When passing Python lists to JavaScript, use tojson appropriately:
IN HTML ATTRIBUTES (x-data, data-*, etc.):
Use |tojson WITHOUT |safe - quotes become &quot; which browsers decode:
<div x-data="languageSelector('fr', {{ langs|tojson }})">
<!-- Renders: x-data="languageSelector('fr', [&quot;fr&quot;, &quot;de&quot;])" -->
Use SINGLE QUOTES for the attribute and |tojson for the array:
<div x-data='languageSelector("fr", {{ langs|tojson }})'>
<!-- Renders: x-data='languageSelector("fr", ["fr", "de"])' -->
The single quotes on x-data prevent JSON double quotes from
breaking the HTML attribute parsing.
IN <script> BLOCKS:
Use |tojson|safe - raw JSON is valid inside script tags:
@@ -85,13 +88,9 @@ language_rules:
const languages = {{ vendor.storefront_languages|tojson|safe }};
</script>
WRONG (raw output - Python syntax not valid JS):
languages: {{ vendor.storefront_languages }}
<!-- Outputs: ['fr', 'de'] - single quotes invalid in JSON -->
WRONG (|safe in HTML attributes - breaks attribute parsing):
<div x-data="init({{ data|tojson|safe }})">
<!-- Raw quotes break the attribute -->
WRONG (double quotes with tojson - JSON quotes break the attribute):
<div x-data="init({{ data|tojson }})">
<!-- Outputs: x-data="init(["a", "b"])" - breaks at first [ -->
pattern:
file_pattern: "app/templates/**/*.html"
required_with_jinja_array: