diff --git a/src/builder.spec.ts b/src/builder.spec.ts index ca6a068..c82d03c 100644 --- a/src/builder.spec.ts +++ b/src/builder.spec.ts @@ -1169,7 +1169,7 @@ describe('Builder', () => { ' \n' + ' \n' + ' source val2\n' + - ' \n' + + ' \n' + ' \n' + ' \n' + ' \n' + diff --git a/src/model/translationFileSerialization.spec.ts b/src/model/translationFileSerialization.spec.ts index 146db65..bda57a6 100644 --- a/src/model/translationFileSerialization.spec.ts +++ b/src/model/translationFileSerialization.spec.ts @@ -242,7 +242,25 @@ describe('translationFileSerialization', () => { `); }); - + 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(` + + + + source val + + + + +`); + }); }); describe('toXlf1', () => { it('should not include state attribute if it is undefined', () => { @@ -334,7 +352,26 @@ describe('translationFileSerialization', () => { `); }); - + 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( + ` + + + + source val + + + + +`) + }); }); describe('fromXlf1', () => { it('should parse xml with placeholder', () => { diff --git a/src/model/translationFileSerialization.ts b/src/model/translationFileSerialization.ts index e8d36c5..f5c68ed 100644 --- a/src/model/translationFileSerialization.ts +++ b/src/model/translationFileSerialization.ts @@ -235,7 +235,12 @@ function removeWhitespace(node: T): void { function pretty(doc: XmlDocument, options: Pick) { 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 (
->
): +function expandSelfClosingTags(xml: string): string { + return xml.replace(/<(target)([^>]*)\/>/g, '<$1$2>'); } function indentChildren(doc: XmlElement, indent: number) {