From 272090d4e7e184af57893ac6f274b4fb268ca951 Mon Sep 17 00:00:00 2001 From: Daniel Schreiber Date: Mon, 4 Sep 2023 13:44:27 +0200 Subject: [PATCH] fix: prevent generation of target node for updated source texts when newTranslationTargetsBlank=omit/true --- __tests__/merge.test.ts | 111 +++++++++++++++++++++++++++++++++++----- src/merge.ts | 8 +-- 2 files changed, 103 insertions(+), 16 deletions(-) diff --git a/__tests__/merge.test.ts b/__tests__/merge.test.ts index 18f5528..de39d0c 100644 --- a/__tests__/merge.test.ts +++ b/__tests__/merge.test.ts @@ -5,13 +5,16 @@ describe('merge', () => { describe('xliff 1.2', () => { test('should handle many units with fuzzy match performantly', () => { const count = 1000; + function generateSourceUnit(n: number): string { return `source val ${n}`; } + function generateTargetUnit(n: number): string { return `target source val ${n}target val`; } + const sourceFileContent = '\n' + ' \n' + ' \n' + @@ -490,15 +493,15 @@ describe('merge', () => { ' \n' + ''; const excludeFileContent = ['\n' + - ' \n' + - ' \n' + - ' \n' + - ' source val that is long enough\n' + - ' target val\n' + - ' \n' + - ' \n' + - ' \n' + - '']; + ' \n' + + ' \n' + + ' \n' + + ' source val that is long enough\n' + + ' target val\n' + + ' \n' + + ' \n' + + ' \n' + + '']; const result = merge(sourceFileContent, destFileContent, {excludeFiles: excludeFileContent}); @@ -1090,7 +1093,7 @@ describe('merge', () => { ' \n' + '')); }); - test('should add final node with filled target when newTranslationTargetsBlank=true and destination file is source language', () => { + test('should add final node with filled target when newTranslationTargetsBlank=true and destination file is source language', () => { const sourceFileContent = '\n' + ' \n' + ' \n' + @@ -1114,7 +1117,10 @@ describe('merge', () => { ' \n' + ''; - const result = merge(sourceFileContent, destFileContent, { newTranslationTargetsBlank: true, sourceLanguage: true }); + const result = merge(sourceFileContent, destFileContent, { + newTranslationTargetsBlank: true, + sourceLanguage: true + }); expect(norm(result)).toEqual(norm('\n' + ' \n' + @@ -1131,6 +1137,82 @@ describe('merge', () => { ' \n' + '')); }); + test('should trim existing without adding target when newTranslationTargetsBlank="omit"', () => { + const sourceFileContent = '\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + ''; + const destFileContent = '\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val \n' + + ' \n' + + ' \n' + + ' \n' + + ''; + + + const result = merge(sourceFileContent, destFileContent, { + newTranslationTargetsBlank: 'omit', + syncTargetsWithInitialState: true, + sourceLanguage: false + }); + + expect(norm(result)).toEqual(norm('\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + '')); + }); + test('should trim existing without adding target when newTranslationTargetsBlank=true', () => { + const sourceFileContent = '\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + ''; + const destFileContent = '\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val \n' + + ' \n' + + ' \n' + + ' \n' + + ' \n' + + ''; + + + const result = merge(sourceFileContent, destFileContent, { + newTranslationTargetsBlank: true, + syncTargetsWithInitialState: true, + sourceLanguage: false + }); + + expect(norm(result)).toEqual(norm('\n' + + ' \n' + + ' \n' + + ' \n' + + ' source val\n' + + ' \n' + + ' \n' + + ' \n' + + ' \n' + + '')); + }); }); describe('xliff 2.0', () => { @@ -1439,7 +1521,7 @@ describe('merge', () => { '')); }); - test('should add final node with filled target when newTranslationTargetsBlank=true and destination file is source language', () => { + test('should add final node with filled target when newTranslationTargetsBlank=true and destination file is source language', () => { const sourceFileContent = '\n' + ' \n' + ' \n' + @@ -1465,7 +1547,10 @@ describe('merge', () => { ' \n' + ''; - const result = merge(sourceFileContent, destFileContent, { newTranslationTargetsBlank: true, sourceLanguage: true }); + const result = merge(sourceFileContent, destFileContent, { + newTranslationTargetsBlank: true, + sourceLanguage: true + }); expect(norm(result)).toEqual(norm('\n' + ' \n' + diff --git a/src/merge.ts b/src/merge.ts index a0cc5ab..db5ff30 100644 --- a/src/merge.ts +++ b/src/merge.ts @@ -9,7 +9,7 @@ type MergeOptions = { sourceLanguage?: boolean, replaceApostrophe?: boolean, newTranslationTargetsBlank?: boolean | 'omit', - /** For untranslated units with initial state (state="initial" / state="new"), a updated source will be copied into the target */ + /** For untranslated units with initial state (state="initial" / state="new"), an updated source will be copied into the target (unless `newTranslationTargetsBlank='omit'/true`) */ syncTargetsWithInitialState?: boolean, }; @@ -223,8 +223,10 @@ export function mergeWithMapping(inFilesContent: string | string[], destFileCont if (options?.collapseWhitespace ?? true ? collapseWhitespace(destSourceText) !== collapseWhitespace(unitSourceText) : destSourceText !== unitSourceText) { destSource.children = unitSource.children; if (options?.sourceLanguage || (options?.syncTargetsWithInitialState === true && isUntranslated(destUnit, xliffVersion, destSourceText))) { - const targetElement = destTarget ?? createTargetElement(destUnit, xliffVersion); - targetElement!.children = unitSource.children; + if (destTarget || options?.newTranslationTargetsBlank !== 'omit') { + const targetElement = destTarget ?? createTargetElement(destUnit, xliffVersion); + targetElement!.children = unitSource.children; + } } updateFirstAndLastChild(destSource); resetTranslationState(destUnit, xliffVersion, options);