Skip to content

Commit

Permalink
sort attributes of XML elements before converting them into a string
Browse files Browse the repository at this point in the history
  • Loading branch information
mbey-mw committed Feb 8, 2024
1 parent 8f3f417 commit d928301
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2006,7 +2006,7 @@ describe('Builder', () => {
' <source>Some text <ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' </trans-unit>\n' +
' <trans-unit id="ID2" datatype="html">\n' +
' <source>no-space<ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' <source>no-space<ph equiv="INTERPOLATION" id="0" disp="{{ myLabel }}"/> <ph id="1" disp="{{ myNumber }}" equiv="INTERPOLATION_1"/></source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -2026,10 +2026,10 @@ describe('Builder', () => {
' <file source-language="de" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>Some text <ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' <source>Some text <ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></source>\n' +
' </trans-unit>\n' +
' <trans-unit id="ID2" datatype="html">\n' +
' <source>no-space<ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' <source>no-space<ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></source>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand All @@ -2038,12 +2038,12 @@ describe('Builder', () => {
' <file source-language="de" target-language="fr-ch" datatype="plaintext" original="ng2.template">\n' +
' <body>\n' +
' <trans-unit id="ID1" datatype="html">\n' +
' <source>Some text <ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' <target state="new">Some text <ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></target>\n' +
' <source>Some text <ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></source>\n' +
' <target state="new">Some text <ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></target>\n' +
' </trans-unit>\n' +
' <trans-unit id="ID2" datatype="html">\n' +
' <source>no-space<ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></source>\n' +
' <target state="new">no-space<ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/> <ph id="1" equiv="INTERPOLATION_1" disp="{{ myNumber }}"/></target>\n' +
' <source>no-space<ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></source>\n' +
' <target state="new">no-space<ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/> <ph disp="{{ myNumber }}" equiv="INTERPOLATION_1" id="1"/></target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand Down
2 changes: 1 addition & 1 deletion src/model/translationFileSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ describe('translationFileSerialization', () => {
const translationFile = fromXlf1(xlf1);
expect(translationFile).toEqual(new TranslationFile([{
id: 'ID1',
source: 'Some text <ph id="0" equiv="INTERPOLATION" disp="{{ myLabel }}"/>',
source: 'Some text <ph disp="{{ myLabel }}" equiv="INTERPOLATION" id="0"/>',
locations: []
}], 'de', undefined, undefined));
});
Expand Down
8 changes: 7 additions & 1 deletion src/model/translationFileSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ export function fromXlf1(xlf1: string): TranslationFile {
}

function toString(...nodes: XmlNode[]): string {
return nodes.map(n => n.toString({preserveWhitespace: true, compressed: true})).join('');
return nodes.map(n => {
if (n instanceof XmlElement) {
const attr = Object.entries(n.attr).sort((a, b) => a[0].localeCompare(b[0]));
n.attr = Object.fromEntries(attr);
}
return n.toString({ preserveWhitespace: true, compressed: true });
}).join('');
}

function toStringOrUndefined(nodes: XmlNode[] | undefined): string | undefined {
Expand Down

0 comments on commit d928301

Please sign in to comment.