Skip to content

Commit 586e8f4

Browse files
committed
Merge branch 'GH-101/inline-html-element' into master
Close GH-101 Fix some html tags to be printed on their own line.
2 parents fd5b244 + e610abb commit 586e8f4

File tree

9 files changed

+103
-32
lines changed

9 files changed

+103
-32
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ __bracketSameLine: `false`__
6868
<br />
6969
```
7070

71+
- GH-101 Fix printing some html tags to be on their own line (heading and table cell)
7172
- GH-106 Fix handling string literal when using block shortcut syntax
7273

7374
### Internals

src/print/Element.js

+25-22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { doc } from "prettier";
22
import {
33
removeSurroundingWhitespace,
44
isInlineElement,
5+
isOwnlineElement,
56
printChildGroups,
67
EXPRESSION_NEEDED,
78
STRING_NEEDS_QUOTES
@@ -52,32 +53,34 @@ const p = (node, path, print, options) => {
5253
node[EXPRESSION_NEEDED] = false;
5354
node[STRING_NEEDS_QUOTES] = false;
5455

55-
if (!node.selfClosing) {
56-
node.children = removeSurroundingWhitespace(node.children);
56+
if (node.selfClosing) {
57+
return openingGroup;
58+
}
5759

58-
const childGroups = printChildGroups(node, path, print, "children");
59-
const closingTag = ["</", node.name, ">"];
60-
const result = [openingGroup];
61-
const joinedChildren = childGroups;
62-
if (isInlineElement(node)) {
63-
result.push(indent([softline, joinedChildren]), softline);
64-
} else {
65-
const childBlock = [];
66-
if (childGroups.length > 0) {
67-
childBlock.push(hardline);
68-
}
69-
childBlock.push(joinedChildren);
70-
result.push(indent(childBlock));
71-
if (childGroups.length > 0) {
72-
result.push(hardline);
73-
}
74-
}
75-
result.push(closingTag);
60+
const groupElement = Symbol("element");
61+
node.children = removeSurroundingWhitespace(node.children);
7662

77-
return isInlineElement(node) ? group(result) : result;
63+
const childGroups = printChildGroups(node, path, print, "children");
64+
const closingTag = ["</", node.name, ">"];
65+
const result = [openingGroup];
66+
const joinedChildren = childGroups;
67+
if (isOwnlineElement(node) || isInlineElement(node)) {
68+
const element = [indent([softline, joinedChildren]), softline];
69+
result.push(element);
70+
} else {
71+
const childBlock = [];
72+
if (childGroups.length > 0) {
73+
childBlock.push(hardline);
74+
}
75+
childBlock.push(joinedChildren);
76+
result.push(indent(childBlock));
77+
if (childGroups.length > 0) {
78+
result.push(hardline);
79+
}
7880
}
81+
result.push(closingTag);
7982

80-
return openingGroup;
83+
return group(result, { id: groupElement });
8184
};
8285

8386
export { p as printElement };

src/util/publicFunctions.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const INLINE_HTML_ELEMENTS = [
2828
"img",
2929
"kbd",
3030
"label",
31-
"li",
3231
"mark",
3332
"q",
3433
"s",
@@ -43,6 +42,22 @@ const INLINE_HTML_ELEMENTS = [
4342
"var"
4443
];
4544

45+
/**
46+
* Introducing new "type" html element that should be printed on their own line
47+
* @type {string[]}
48+
*/
49+
const OWNLINE_HTML_ELEMENTS = [
50+
"h1",
51+
"h2",
52+
"h3",
53+
"h4",
54+
"h5",
55+
"h6",
56+
"li",
57+
"td",
58+
"th"
59+
];
60+
4661
/**
4762
* Node types around which we avoid an extra line break.
4863
* Example:
@@ -478,6 +493,12 @@ const isInlineElement = node => {
478493
);
479494
};
480495

496+
export const isOwnlineElement = node => {
497+
return (
498+
Node.isElement(node) && OWNLINE_HTML_ELEMENTS.indexOf(node.name) >= 0
499+
);
500+
};
501+
481502
const isCommentNode = node =>
482503
Node.isTwigComment(node) || Node.isHtmlComment(node);
483504

tests/ControlStructures/__snapshots__/forWithBlock.snap.twig

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<h1>
2-
{{ title|title }}
3-
</h1>
1+
<h1>{{ title|title }}</h1>
42
<ul>
53
{% for item in items %}
64
<li class="{{ loop.last ? 'last' : '' }}">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<ul>
2+
<li>a</li>
3+
<li>b</li>
4+
</ul>
5+
6+
<thead>
7+
<tr>
8+
<th>1</th>
9+
<th>2</th>
10+
<th>3</th>
11+
</tr>
12+
</thead>
13+
14+
<tbody>
15+
<tr>
16+
<td>1</td>
17+
<td>2</td>
18+
<td>3</td>
19+
</tr>
20+
</tbody>
21+
22+
<main>
23+
<h1>1</h1>
24+
<h2>2</h2>
25+
<h3>3</h3>
26+
<h4>4</h4>
27+
<h5>5</h5>
28+
<h6>6</h6>
29+
</main>
30+
31+
<h1
32+
class="Lorem ipsum dolor sit amet."
33+
aria-label="Lorem ipsum dolor sit amet, consectetur adipisicing elit. In, repellat."
34+
>
35+
helloM
36+
</h1>

tests/Element/jsfmt.spec.js

+7
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,11 @@ describe("Elements", () => {
6868
});
6969
expect(actual).toMatchFileSnapshot(snapshotFile);
7070
});
71+
72+
it("should handle ownline html element", async () => {
73+
const { actual, snapshotFile } = await run_spec(import.meta.url, {
74+
source: "ownline_html_element.twig"
75+
});
76+
expect(actual).toMatchFileSnapshot(snapshotFile);
77+
});
7178
});
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ul><li>a</li><li>b</li></ul>
2+
3+
<thead><tr><th>1</th><th>2</th><th>3</th></tr></thead>
4+
5+
<tbody><tr><td>1</td><td>2</td><td>3</td></tr></tbody>
6+
7+
<main><h1>1</h1><h2>2</h2><h3>3</h3><h4>4</h4><h5>5</h5><h6>6</h6></main>
8+
9+
<h1 class="Lorem ipsum dolor sit amet." aria-label="Lorem ipsum dolor sit amet, consectetur adipisicing elit. In, repellat.">helloM</h1>

tests/Expressions/__snapshots__/operators.snap.twig

+1-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@
6666
or element is instance of(
6767
constant('Namespace\\Classname::CONSTANT_NAME')
6868
) %}
69-
<h1>
70-
{{ entry.title }}
71-
</h1>
69+
<h1>{{ entry.title }}</h1>
7270
{% endif %}
7371

7472
{{ dump(test) }}

tests/smoke-test.twig

+1-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
{% endfor %}
1919
</ul>
2020

21-
<h1>
22-
My Webpage
23-
</h1>
21+
<h1>My Webpage</h1>
2422
{{ a_variable }}
2523
{% set name = 'Fabien' %}
2624
{% set numbers = [1, 2] %}

0 commit comments

Comments
 (0)