🧰 ユーティリティ上級
HTML属性のフォーマット規則テスト
Prettier Liquid プラグインの HTML 属性フォーマット仕様をテストするコード集。属性値のクォート形式、改行位置、Liquid タグの混在時の動作を検証する。
用途
Liquid テンプレート内の HTML タグ属性を自動整形するツール開発時、またはプラグインの品質確認で使用。属性が長い場合の折り返し、ブール属性、Liquid の条件分岐を含む属性の表現ルールを確認したい場合。
設置場所
このファイルは Prettier Liquid プラグインのテストケースとして packages/prettier-plugin-liquid/src/test/html-attributes/ に保持する。テストランナーが固定ケースに対して出力を検証し、フォーマッタの動作を保証する。
注意点
このテストケースは Prettier のフォーマッタ挙動を定義した規約であり、手動で Liquid を書く際の「正解」ではない。printWidth(1 や 30 など)の値はテスト条件ごとに異なるため、実運用での設定値を確認すること。singleQuote オプションと属性値内のクォート衝突時は、クォート形式を自動切り替えする仕様のため、手作業で修正する必要はない。
コード
166 行 / liquidIt should use double quotes for all attributes, unless boolean attribute
<div
id="1234"
class="long string of classes"
style="color: blue;"
content=""
disabled
></div>
It should use single quotes for all attributes, unless boolean attribute when singleQuote is true
singleQuote: true
<div
id='1234'
class='long string of classes'
style='color: blue;'
content=''
disabled
></div>
It should break LiquidTags attribute nodes and consider the insides _not_ whitespace sensitive
<div
src="https://content.instructables.com/ORIG/F23/YMO0/FPIUQOJF/F23YMO0FPIUQOJF.jpg"
width="320"
height="240"
loading="lazy"
{{ block.shopify_attributes }}
{% if A %}
disabled
{% else %}
checked
{% endif %}
></div>
It should not try to format HTML attributes (yet) (1)
<img
class="image {% if sticky %}image--sticky{% endif %} {% if wrapped %}image--wrapped{% endif %}"
id="img--{{ product.id }}"
src="{{ product.featured_image }}"
width="{{ product.featured_image.width }}"
height="{{ product.featured_image.height }}"
loading="lazy"
>
It should break and indent attributes that are long af
<img
srcset="
{% if media.preview_image.width >= 493 %}{{ media.preview_image | image_url: width: 493 }} 493w,{% endif %}
{% if media.preview_image.width >= 600 %}{{ media.preview_image | image_url: width: 600 }} 600w,{% endif %}
{% if media.preview_image.width >= 713 %}{{ media.preview_image | image_url: width: 713 }} 713w,{% endif %}
{% if media.preview_image.width >= 823 %}{{ media.preview_image | image_url: width: 823 }} 823w,{% endif %}
{% if media.preview_image.width >= 990 %}{{ media.preview_image | image_url: width: 990 }} 990w,{% endif %}
{% if media.preview_image.width >= 1100 %}{{ media.preview_image | image_url: width: 1100 }} 1100w,{% endif %}
{% if media.preview_image.width >= 1206 %}{{ media.preview_image | image_url: width: 1206 }} 1206w,{% endif %}
{% if media.preview_image.width >= 1346 %}{{ media.preview_image | image_url: width: 1346 }} 1346w,{% endif %}
{% if media.preview_image.width >= 1426 %}{{ media.preview_image | image_url: width: 1426 }} 1426w,{% endif %}
{% if media.preview_image.width >= 1646 %}{{ media.preview_image | image_url: width: 1646 }} 1646w,{% endif %}
{% if media.preview_image.width >= 1946 %}{{ media.preview_image | image_url: width: 1946 }} 1946w,{% endif %}
{{ media.preview_image | image_url }} {{ media.preview_image.width }}w
"
src="{{ media | image_url: width: 1946 }}"
sizes="(min-width: {{ settings.page_width }}px) {{ settings.page_width | minus: 100 | times: media_width | round }}px, (min-width: 990px) calc({{ media_width | times: 100 }}vw - 10rem), (min-width: 750px) calc((100vw - 11.5rem) / 2), calc(100vw - 4rem)"
{% unless lazy_load == false %}
loading="lazy"
{% endunless %}
width="973"
height="{{ 973 | divided_by: media.preview_image.aspect_ratio | ceil }}"
alt="{{ media.preview_image.alt | escape }}"
>
If there's only one attribute and self-closing, don't break when the line is too long.
<img class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet">
If there's only one attribute and not self-closing, break the attr and close the tag on a different line.
<div
class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet"
></div>
If there's only one attribute and not self-closing, but with children, do not break the attr and close the tag on a different line.
<div class="Lorem ipsum dolor sit amet consectetur adipiscing elit Sed feugiat semper sem eu ultrices nisl interdum sit amet">
Hello
</div>
If there's only one attribute, not self-closing, with children, do not break the LiquidTag attr and close the tag on a different line.
<div {% echo 'disabled' %}>
Hello
</div>
If there's only one attribute, not self-closing, with children, do not break the LiquidNode attr and close the tag on a different line.
<div {{ block_attributes }}>
Hello
</div>
If there's only one attribute, not self closing, with children, but the attribute is a liquid tag that has children, break it normally.
printWidth: 30
<div
{% if some_condition %}
attribute="value"
{% endif %}
>
hello world
</div>
If there's only one attribute, not self closing, without children, but the attribute is a liquid tag that has children, break it normally.
printWidth: 30
<div
{% if some_condition %}
attribute="value"
{% endif %}
></div>
If there's multiple attributes inside an if tag attribute node, print until print width
<div
{% if some_condition %}
attribute="abstractNode" ok="value"
{% endif %}
></div>
If there's multiple attributes inside an if tag attribute node, print until print width
printWidth: 1
<div
{% if some_condition %}
attribute="abstractNode"
ok="value"
{% endif %}
></div>
If there's multiple attributes inside an if tag attribute node with a newline, maintain newline
<div
{% if some_condition %}
attribute="abstractNode"
ok="value"
{% endif %}
></div>
If singleQuote is true, and the attribute value contained single quotes, use double quotes
singleQuote: true
<link onload="this.media='all'">
If singleQuote is false, and the attribute value contained double quotes, use single quotes
singleQuote: false
<link onload='this.media="all"'>
If singleQuote is true, and the attribute value has liquid tags that contain single quotes, don't care.
singleQuote: true
<script src='{{ 'styles.css' | asset_url }}'></script>
If singleQuote is false, and the attribute value has liquid tags that contain double quotes, don't care.
singleQuote: false
<script src="{{ "styles.css" | asset_url }}"></script>
It should pretty print attribute names with Liquid in them
singleQuote: true
<div data-popup-{{ section.id }}='size-{{ section.id }}'></div>
It should pretty print attribute names with Liquid in them without new lines
printWidth: 1
<div
data-popup-{{ section.id | upcase }}="size-{{ section.id }}"
></div>
It should pretty unquoted liquid drop attributes
<div id="{{ section.id }}--omg"></div>
It should pretty print smart quotes into dumb quotes to prevent copy pasting errors
<h1 class="section-header__title h2" id="abc"></h1>
出典・ライセンス
- Repository:
- https://github.com/Shopify/theme-tools
- License:
- MIT
このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。
関連項目
🧰 ユーティリティ初級
コメント後の空白フォーマット
Liquid コメントタグの直後に改行や空白がある場合の正しいフォーマット例。prettier-plugin-liquid によるコード整形テストケース。
📁 theme-tools·MIT·6 行
🧰 ユーティリティ初級
Liquidコメントのフォーマット
Liquid のコメントタグ直後に改行がある場合の正しいフォーマット例。prettier-plugin-liquid による自動整形テストケース。
📁 theme-tools·MIT·6 行
🧰 ユーティリティ初級
セクション呼び出しの改行制御
{% sections %} タグの前後の空白制御(ホワイトスペース削除演算子 `{%-` と `-%}` の使い方)を示すサンプル。Prettier による Liquid コードフォーマットで改行が崩れないことを確認するテストケース。
📁 theme-tools·MIT·7 行
🧰 ユーティリティ初級
Prettier フォーマッタの HTML リスト整形テスト
Prettier Liquid プラグインが HTML のリスト要素(ul / ol)をどのように整形・改行するかをテストするサンプルコード。短い行は1行、長い行は複数行に自動で分割される動作を検証する。
📁 theme-tools·MIT·8 行
🧰 ユーティリティ上級
Vue.js属性のLiquid整形
Vue.js のディレクティブ(@click、:class など)を含む Liquid コードを Prettier で正しく整形するテストケース。spec 準拠の属性名を保持したまま、改行・インデント処理を行う。
📁 theme-tools·MIT·9 行
🧰 ユーティリティ上級
sections タグのフォーマット
Liquid の sections タグ構文と、Prettier によるフォーマット時の動作を示すテストファイル。タグ名の直前・直後の空白や改行が保持される仕様を確認するサンプル。
📁 theme-tools·MIT·9 行