Skip to content

Commit 5046a8e

Browse files
committed
Do not ignore whitespace in xsl:text nodes
1 parent 08bb1c0 commit 5046a8e

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
/yarn.lock
77
/LICENSE
88

9+
/.husky/
910
/test/fixture.xml
1011
/test/__snapshots__/*

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Below are the options (from [`src/plugin.js`](src/plugin.js)) that `@prettier/pl
4848

4949
| API Option | CLI Option | Default | Description |
5050
| -------------------------- | ------------------------------ | :----------: | ------------------------------------------------------------------------------------------------------------------------ |
51-
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-line)) |
51+
| `bracketSameLine` | `--bracket-same-line` | `true` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#bracket-line)) |
5252
| `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). |
5353
| `singleAttributePerLine` | `--single-attribute-per-line` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#single-attribute-per-line)) |
5454
| `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). |

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"scripts": {
88
"lint": "eslint --cache .",
99
"prepare": "node bin/languages.js && husky install",
10-
"print": "prettier --plugin=.",
10+
"print": "prettier --plugin=./src/plugin.js",
1111
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
1212
},
1313
"repository": {
@@ -61,10 +61,12 @@
6161
"transform": {}
6262
},
6363
"prettier": {
64+
"embeddedLanguageFormatting": "auto",
6465
"plugins": [
6566
"./src/plugin.js"
6667
],
67-
"trailingComma": "none"
68+
"trailingComma": "none",
69+
"xmlWhitespaceSensitivity": "ignore"
6870
},
6971
"lint-staged": {
7072
"*.js": [

src/printer.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,18 @@ function hasIgnoreRanges(comments) {
2626
return false;
2727
}
2828

29-
function isWhitespaceIgnorable(opts, attributes, content) {
29+
function isWhitespaceIgnorable(opts, name, attributes, content) {
3030
// If the whitespace sensitivity setting is "strict", then we can't ignore the
3131
// whitespace.
3232
if (opts.xmlWhitespaceSensitivity === "strict") {
3333
return false;
3434
}
3535

36+
// If we have an xsl:text element, then we cannot ignore the whitespace.
37+
if (name === "xsl:text") {
38+
return false;
39+
}
40+
3641
// If there is an xml:space attribute set to "preserve", then we can't ignore
3742
// the whitespace.
3843
if (
@@ -482,7 +487,7 @@ function printElement(path, opts, print) {
482487
END[0].image
483488
]);
484489

485-
if (isWhitespaceIgnorable(opts, attribute, content[0])) {
490+
if (isWhitespaceIgnorable(opts, Name[0].image, attribute, content[0])) {
486491
const fragments = path.call(
487492
(childrenPath) => printElementFragments(childrenPath, opts, print),
488493
"children",

test/__snapshots__/format.test.js.snap

+22
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ use {
124124
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
125125
126126
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
127+
128+
<xsl:text>slide </xsl:text>
127129
</svg>
128130
<!-- bar -->
129131
"
@@ -253,6 +255,8 @@ use {
253255
<NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
254256
255257
<NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
258+
259+
<xsl:text>slide </xsl:text>
256260
</svg>
257261
<!-- bar -->
258262
"
@@ -378,6 +382,8 @@ use {
378382
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
379383
380384
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
385+
386+
<xsl:text>slide </xsl:text>
381387
</svg>
382388
<!-- bar -->
383389
"
@@ -540,6 +546,8 @@ use {
540546
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
541547
542548
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
549+
550+
<xsl:text>slide </xsl:text>
543551
</svg>
544552
<!-- bar -->
545553
"
@@ -665,6 +673,8 @@ use {
665673
<NoEscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
666674
667675
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
676+
677+
<xsl:text>slide </xsl:text>
668678
</svg>
669679
<!-- bar -->
670680
"
@@ -790,6 +800,8 @@ use {
790800
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
791801
792802
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
803+
804+
<xsl:text>slide </xsl:text>
793805
</svg>
794806
<!-- bar -->
795807
"
@@ -915,6 +927,8 @@ use {
915927
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
916928
917929
<NoEscapeNeeded name='Pete &apos;Maverick&apos; Mitchell' />
930+
931+
<xsl:text>slide </xsl:text>
918932
</svg>
919933
<!-- bar -->
920934
"
@@ -1049,6 +1063,8 @@ use {
10491063
<NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
10501064
10511065
<NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
1066+
1067+
<xsl:text>slide </xsl:text>
10521068
</svg>
10531069
<!-- bar -->
10541070
"
@@ -1174,6 +1190,8 @@ use {
11741190
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
11751191
11761192
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
1193+
1194+
<xsl:text>slide </xsl:text>
11771195
</svg>
11781196
<!-- bar -->
11791197
"
@@ -1308,6 +1326,8 @@ use {
13081326
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
13091327
13101328
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
1329+
1330+
<xsl:text>slide </xsl:text>
13111331
</svg>
13121332
<!-- bar -->
13131333
"
@@ -1435,6 +1455,8 @@ use {
14351455
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
14361456
14371457
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
1458+
1459+
<xsl:text>slide </xsl:text>
14381460
</svg>
14391461
<!-- bar -->
14401462
"

test/fixture.xml

+2
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,7 @@
104104
<NoEscapeNeeded name='Pete "Maverick" Mitchell'/>
105105

106106
<NoEscapeNeeded name="Pete 'Maverick' Mitchell"/>
107+
108+
<xsl:text>slide </xsl:text>
107109
</svg>
108110
<!-- bar -->

0 commit comments

Comments
 (0)