Skip to content

Commit

Permalink
Fix up stable sorting for xmlns attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Oct 27, 2023
1 parent b0a1dd7 commit 7332d70
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ function printElement(path, opts, print) {

// Handle the 1 but not both being an xmlns
if (leftNS === "xmlns") return -1;
if (rightNS === "xlmns") return 1;
if (rightNS === "xmlns") return 1;

return leftNS.localeCompare(rightNS);
}
Expand Down
151 changes: 151 additions & 0 deletions test/__snapshots__/format.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ use {
otherTools:baz="bing"
ios:a="a" />
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -241,6 +243,8 @@ use {
otherTools:baz="bing"
ios:a="a"/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo"/>
<foo foo:foo="foo" xmlns:foo="foo"/>
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>
Expand Down Expand Up @@ -364,6 +368,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -518,6 +524,14 @@ use {
ios:a="a"
/>
</foo>
<foo
xmlns:foo="foo"
foo:foo="foo"
/>
<foo
foo:foo="foo"
xmlns:foo="foo"
/>
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -641,6 +655,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -764,6 +780,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -887,6 +905,8 @@ use {
ios:a='a'
/>
</foo>
<foo xmlns:foo='foo' foo:foo='foo' />
<foo foo:foo='foo' xmlns:foo='foo' />
</container>
<EscapeNeeded name='Pete &quot;Maverick&quot; Mitchell' />
Expand Down Expand Up @@ -1019,6 +1039,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo"/>
<foo foo:foo="foo" xmlns:foo="foo"/>
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>
Expand All @@ -1032,6 +1054,131 @@ use {
"
`;

exports[`xmlSortAttributesByKey => true 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"https://www.puppycrawl.com/dtds/configuration_1_3.dtd">
<?xml-model href="project.rnc" type="application/relax-ng-compact-syntax"?>
<!-- foo -->
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
height="100"
viewBox="0 0 200 100"
width="200"
>
<title>Style inheritance and the use element</title>
<desc _attr="attr">
&anp; &#12345;
<![CDATA[
foo
]]>
bar
</desc>
<?pagebreak?>
<style />
<style> </style>
<style type="text/css">
circle {
stroke-opacity: 0.7;
}
.special circle {
stroke: green;
}
use {
stroke: purple;
fill: orange;
}
</style>
<script value="lint" />
<yaml
myveryveryveryverylongattributename="myveryveryveryverylongattributevalue"
>
- 1
- 2
- 3
</yaml>
<!-- inner comment -->
<?pagebreak?>
<g class="special" style="fill: blue">
<circle cx="50" cy="50" id="c" r="40" stroke-width="20" />
</g>
<use xlink:href="#c" x="100" />
<ignored>
<!-- prettier-ignore-start -->
< ignored />
<!-- prettier-ignore-end -->
</ignored>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at est eget enim consectetur accumsan. Aliquam pretium sodales ipsum quis dignissim. Sed id sem vel diam luctus fringilla. Aliquam quis egestas magna. Curabitur molestie lorem et odio porta, et molestie libero laoreet. Morbi rhoncus sagittis cursus. Nullam vehicula pretium consequat. Praesent porta ante at posuere sollicitudin. Nullam commodo tempor arcu, at condimentum neque elementum ut.
</p>
<span>
content
</span>
<div> text with space<div><hr /></div> </div>
<div>
even more
<content />
</div>
<div xml:space="preserve">
<content />
</div>
<div
anotherverylongattribute="anotherverylongvalue"
verylongattribute="verylongvalue"
/>
<div att2="doubleQuotes" attr1='singleQuotes' />
<!-- Basic sorting of xml attributes -->
<div x="first" y="second" z="third" />
<container xmlns:android="androidURI" xmlns:ios="iosURI">
<foo
xmlns="highest priority"
xmlns:tools="toolsURI"
android:a="a"
android:b="b"
android:c="c"
android:d="d"
tools:other="thing"
here="!"
some="non-ns-ed-attributes"
>
<bar
xmlns:otherTools="otherToolsURI"
android:b="b"
ios:a="a"
ios:c="c"
ios:d="d"
otherTools:baz="bing"
tools:other="thing"
another="one"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo xmlns:foo="foo" foo:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
<NoEscapeNeeded>He said, "Don't quote me."</NoEscapeNeeded>
<NoEscapeNeeded name='Pete "Maverick" Mitchell' />
<NoEscapeNeeded name="Pete 'Maverick' Mitchell" />
</svg>
<!-- bar -->
"
`;

exports[`xmlWhitespaceSensitivity => ignore 1`] = `
"<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.3//EN"
Expand Down Expand Up @@ -1151,6 +1298,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down Expand Up @@ -1276,6 +1425,8 @@ use {
ios:a="a"
/>
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell" />
Expand Down
2 changes: 2 additions & 0 deletions test/fixture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
otherTools:baz="bing"
ios:a="a" />
</foo>
<foo xmlns:foo="foo" foo:foo="foo" />
<foo foo:foo="foo" xmlns:foo="foo" />
</container>
<EscapeNeeded name="Pete &quot;Maverick&quot; Mitchell"/>

Expand Down
3 changes: 3 additions & 0 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test("xmlWhitespaceSensitivity => ignore", async () => {
const formatted = await format(fixture, {
xmlWhitespaceSensitivity: "ignore"
});

expect(formatted).toMatchSnapshot();
});

Expand Down Expand Up @@ -76,6 +77,8 @@ test("xmlSortAttributesByKey => true", async () => {
const formatted = await format(fixture, {
xmlSortAttributesByKey: true
});

expect(formatted).toMatchSnapshot();
});

test("xmlQuoteAttributes => preserve", async () => {
Expand Down

0 comments on commit 7332d70

Please sign in to comment.