diff --git a/CHANGELOG.md b/CHANGELOG.md index 12dbab9..fb13c55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,72 @@ ## unreleased +### Bugfixes +- Properly implement `bracketSameLine` options for html element tag + +__Input__ +```twig + + +some description that should be enough to break this into multiline + +some text + +
+``` + +__bracketSameLine: `true`__ +```twig + + +some description that should be enough to break this into multiline + + + some text + + +
+``` + +__bracketSameLine: `false`__ +```twig + + +some description that should be enough to break this into multiline + + + some text + + +
+``` + ### Internals - Add option to add prefix/suffix for test snapshot output. This will allow to reuse single input file to produce several snapshot output with different configuration diff --git a/src/print/Element.js b/src/print/Element.js index 94f90ce..2b69c80 100644 --- a/src/print/Element.js +++ b/src/print/Element.js @@ -9,26 +9,46 @@ import { const { group, line, hardline, softline, indent, join } = doc.builders; -const printOpeningTag = (node, path, print) => { +const printOpeningTag = (node, path, print, options) => { + const groupId = Symbol("opening-tag"); + const opener = "<" + node.name; const printedAttributes = printSeparatedList(path, print, "", "attributes"); - const openingTagEnd = node.selfClosing ? " />" : ">"; - const hasAttributes = node.attributes && node.attributes.length > 0; + const openingTagEnd = []; + if (node.selfClosing) { + if (!options.bracketSameLine) { + openingTagEnd.push(line); + } else { + openingTagEnd.push(" "); + } + openingTagEnd.push("/>"); + } else { + if (!options.bracketSameLine) { + openingTagEnd.push(softline); + } + openingTagEnd.push(">"); + } + + const hasAttributes = node.attributes && node.attributes.length > 0; if (hasAttributes) { - return [opener, indent([line, printedAttributes]), openingTagEnd]; + return group( + [opener, indent([line, printedAttributes]), openingTagEnd], + { id: groupId } + ); } - return [opener, openingTagEnd]; + + return group([opener, openingTagEnd], { id: groupId }); }; const printSeparatedList = (path, print, separator, attrName) => { return join([separator, line], path.map(print, attrName)); }; -const p = (node, path, print) => { +const p = (node, path, print, options) => { // Set a flag in case attributes contain, e.g., a FilterExpression node[EXPRESSION_NEEDED] = true; - const openingGroup = group(printOpeningTag(node, path, print)); + const openingGroup = group(printOpeningTag(node, path, print, options)); node[EXPRESSION_NEEDED] = false; node[STRING_NEEDS_QUOTES] = false; diff --git a/tests/ControlStructures/__snapshots__/forInclude.snap.twig b/tests/ControlStructures/__snapshots__/forInclude.snap.twig index 80bc11f..8d40fb2 100644 --- a/tests/ControlStructures/__snapshots__/forInclude.snap.twig +++ b/tests/ControlStructures/__snapshots__/forInclude.snap.twig @@ -1,7 +1,8 @@ {% for foo in range(1, category) %} + class="qtp-item__star icon-ic icon-icn_star--white {{ foo }}" + > {% include './Star.twig' only %} {% endfor %} diff --git a/tests/ControlStructures/__snapshots__/if.snap.twig b/tests/ControlStructures/__snapshots__/if.snap.twig index 16c0576..e6ffde2 100644 --- a/tests/ControlStructures/__snapshots__/if.snap.twig +++ b/tests/ControlStructures/__snapshots__/if.snap.twig @@ -13,7 +13,8 @@ (css.logoWider): useWiderItems }|classes }}" srcABC="{{ partner.logoUrl }}" - alt="{{ partner.name }}" /> + alt="{{ partner.name }}" + /> {%- elseif partnerName %} {{ partnerName }} {% elseif partnerImg -%} diff --git a/tests/Element/__snapshots__/attributes.snap.twig b/tests/Element/__snapshots__/attributes.snap.twig index 9d45bb4..7c986db 100644 --- a/tests/Element/__snapshots__/attributes.snap.twig +++ b/tests/Element/__snapshots__/attributes.snap.twig @@ -6,7 +6,8 @@ ref: intersectionObserver|default, class: 'hotel-item item-order__list-item js_co_item', 'data-co_alt_htl': isAlternative ? '1' - }|attrs }}> + }|attrs }} +> Test diff --git a/tests/Element/__snapshots__/manyAttributes.snap.twig b/tests/Element/__snapshots__/manyAttributes.snap.twig index 00eb740..5ba74a4 100644 --- a/tests/Element/__snapshots__/manyAttributes.snap.twig +++ b/tests/Element/__snapshots__/manyAttributes.snap.twig @@ -6,6 +6,7 @@ attr5="five" attr6="six" attr7="seven" - attr8="eight"> + attr8="eight" +> Text diff --git a/tests/Element/__snapshots__/selfClosing.snap.twig b/tests/Element/__snapshots__/selfClosing.snap.twig index 629f016..f61e06e 100644 --- a/tests/Element/__snapshots__/selfClosing.snap.twig +++ b/tests/Element/__snapshots__/selfClosing.snap.twig @@ -8,7 +8,8 @@ attr5="five" attr6="six" attr7="seven" - attr8="eight" /> + attr8="eight" +/> diff --git a/tests/Expressions/__snapshots__/mappingExpression.snap.twig b/tests/Expressions/__snapshots__/mappingExpression.snap.twig index 31afb31..9689ec9 100644 --- a/tests/Expressions/__snapshots__/mappingExpression.snap.twig +++ b/tests/Expressions/__snapshots__/mappingExpression.snap.twig @@ -68,4 +68,5 @@ theme }) }) - }}> + }} +> diff --git a/tests/Expressions/__snapshots__/objectExpression.snap.twig b/tests/Expressions/__snapshots__/objectExpression.snap.twig index 8d19ef3..d9c8efc 100644 --- a/tests/Expressions/__snapshots__/objectExpression.snap.twig +++ b/tests/Expressions/__snapshots__/objectExpression.snap.twig @@ -10,6 +10,7 @@

+ }|classes }}" +> Heading

diff --git a/tests/Failing/__snapshots__/controversial.snap.twig b/tests/Failing/__snapshots__/controversial.snap.twig index cbc005f..227d96e 100644 --- a/tests/Failing/__snapshots__/controversial.snap.twig +++ b/tests/Failing/__snapshots__/controversial.snap.twig @@ -10,7 +10,8 @@
+ } | classes }}" +>