Skip to content

Commit 2bc6e1b

Browse files
authored
Merge pull request #634 from prettier/fixes
Fix link to plugin.js and embed handling tags with no type attribute
2 parents c4abf07 + 8525740 commit 2bc6e1b

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The `prettier` executable is now installed and ready for use:
4444

4545
## Configuration
4646

47-
Below are the options (from [`src/plugin.ts`](src/plugin.ts)) that `@prettier/plugin-xml` currently supports:
47+
Below are the options (from [`src/plugin.js`](src/plugin.js)) that `@prettier/plugin-xml` currently supports:
4848

4949
| API Option | CLI Option | Default | Description |
5050
| -------------------------- | ------------------------------ | :--------: | ------------------------------------------------------------------------------------------------------------- |

src/embed.js

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ function getElementTags(path, opts, print) {
3535
};
3636
}
3737

38+
// Returns the value of the type tag if there is one, otherwise returns null.
39+
function getTagType(attributes) {
40+
for (const attribute of attributes) {
41+
if (attribute.children.Name[0].image === "type") {
42+
const value = attribute.children.STRING[0].image;
43+
44+
if (value.startsWith('"text/') && value.endsWith('"')) {
45+
return value.slice(6, -1);
46+
}
47+
}
48+
}
49+
50+
return null;
51+
}
52+
3853
// Get the name of the parser that is represented by the given element node,
3954
// return null if a matching parser cannot be found
4055
function getParser(node, opts) {
@@ -47,30 +62,10 @@ function getParser(node, opts) {
4762
return null;
4863
}
4964

50-
// If this is a style tag with a text/xxx type then we will use xxx as the
51-
// name of the parser
52-
if (parser === "style" && attribute) {
53-
const typeAttr = attribute.find(
54-
(attr) => attr.children.Name[0].image === "type"
55-
);
56-
const typeValue = typeAttr && typeAttr.children.STRING[0].image;
57-
58-
if (typeValue.startsWith('"text/') && typeValue.endsWith('"')) {
59-
parser = typeValue.slice(6, -1);
60-
}
61-
}
62-
63-
// If this is a script tag with text/xxx then we will use xxx as the name of
64-
// the parser
65-
if (parser === "script" && attribute) {
66-
const typeAttr = attribute.find(
67-
(attr) => attr.children.Name[0].image === "type"
68-
);
69-
const typeValue = typeAttr && typeAttr.children.STRING[0].image;
70-
71-
if (typeValue.startsWith('"text/') && typeValue.endsWith('"')) {
72-
parser = typeValue.slice(6, -1);
73-
}
65+
// If this is a style tag or a script tag with a text/xxx type then we will
66+
// use xxx as the name of the parser
67+
if ((parser === "style" || parser === "script") && attribute) {
68+
parser = getTagType(attribute);
7469
}
7570

7671
// If the name of the parser is "javascript", then we're going to switch over

test/__snapshots__/format.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use {
3636
fill: orange;
3737
}
3838
</style>
39+
<script value="lint" />
3940
4041
<yaml
4142
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue">
@@ -112,6 +113,7 @@ use {
112113
fill: orange;
113114
}
114115
</style>
116+
<script value="lint"/>
115117
116118
<yaml
117119
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue">
@@ -189,6 +191,7 @@ use {
189191
fill: orange;
190192
}
191193
</style>
194+
<script value="lint" />
192195
193196
<yaml
194197
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue"
@@ -264,6 +267,7 @@ use {
264267
fill: orange;
265268
}
266269
</style>
270+
<script value="lint" />
267271
268272
<yaml
269273
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue"
@@ -354,6 +358,7 @@ use {
354358
fill: orange;
355359
}
356360
</style>
361+
<script value="lint"/>
357362
358363
<yaml
359364
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue"
@@ -432,6 +437,7 @@ use {
432437
fill: orange;
433438
}
434439
</style>
440+
<script value="lint" />
435441
436442
<yaml
437443
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue"

test/fixture.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use { stroke: purple;
2929
fill: orange; }
3030
</style>
31+
<script value="lint" />
3132

3233
<yaml myveryveryveryverylongattributename="myveryveryveryverylongattributevalue">
3334
- 1

0 commit comments

Comments
 (0)