Skip to content

Commit

Permalink
fix: prevent self-closing tags for empty targets - fixes #117
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-sc committed Jan 25, 2025
1 parent b04f00b commit 7440ef7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ describe('Builder', () => {
' </trans-unit>\n' +
' <trans-unit id="ID2" datatype="html">\n' +
' <source>source val2</source>\n' +
' <target state="new"/>\n' +
' <target state="new"></target>\n' +
' </trans-unit>\n' +
' </body>\n' +
' </file>\n' +
Expand Down
41 changes: 39 additions & 2 deletions src/model/translationFileSerialization.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,25 @@ describe('translationFileSerialization', () => {
</file>
</xliff>`);
});

it('should create new node without self closing tag', () => {
const input = new TranslationFile([{
id: 'ID1',
source: 'source val',
target: '',
state: 'new',
locations: []
}], 'de', undefined);
expect(toXlf2(input, {prettyNestedTags: false})).toEqual(`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="de">
<file id="ngi18n" original="ng.template">
<unit id="ID1">
<segment state="new">
<source>source val</source>
<target></target>
</segment>
</unit>
</file>
</xliff>`);
});
});
describe('toXlf1', () => {
it('should not include state attribute if it is undefined', () => {
Expand Down Expand Up @@ -334,7 +352,26 @@ describe('translationFileSerialization', () => {
</file>
</xliff>`);
});

it('should create new node without self closing tag', () => {
const input = new TranslationFile([{
id: 'ID1',
source: 'source val',
target: '',
state: 'new',
locations: []
}], 'de', undefined);
expect(toXlf1(input, {prettyNestedTags: false})).toEqual(
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="de" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="ID1" datatype="html">
<source>source val</source>
<target state="new"></target>
</trans-unit>
</body>
</file>
</xliff>`)
});
});
describe('fromXlf1', () => {
it('should parse xml with placeholder', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/model/translationFileSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,12 @@ function removeWhitespace<T extends XmlDocument | XmlElement>(node: T): void {
function pretty(doc: XmlDocument, options: Pick<Options, 'prettyNestedTags'>) {
removeWhitespace(doc);
addPrettyWhitespace(doc, 0, options);
return doc.toString({preserveWhitespace: true, compressed: true});
return expandSelfClosingTags(doc.toString({preserveWhitespace: true, compressed: true}));
}

// this only addresses 'target' nodes, to avoid breaking nested html tags (<hr/> -> <hr></hr>):
function expandSelfClosingTags(xml: string): string {
return xml.replace(/<(target)([^>]*)\/>/g, '<$1$2></$1>');
}

function indentChildren(doc: XmlElement, indent: number) {
Expand Down

0 comments on commit 7440ef7

Please sign in to comment.