From 65a60b745691951e87c0f1366214427efb2ad318 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 30 Sep 2016 14:52:12 -0700 Subject: [PATCH] style(I18N): Carriage returns in serialized files --- .../compiler/src/i18n/serializers/xliff.ts | 24 +++--- .../compiler/src/i18n/serializers/xmb.ts | 11 +-- .../src/i18n/serializers/xml_helper.ts | 4 + .../test/i18n/serializers/xliff_spec.ts | 78 ++++++++++--------- .../test/i18n/serializers/xmb_spec.ts | 11 ++- 5 files changed, 68 insertions(+), 60 deletions(-) diff --git a/modules/@angular/compiler/src/i18n/serializers/xliff.ts b/modules/@angular/compiler/src/i18n/serializers/xliff.ts index c1fc9540bc..08b0c84fa8 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xliff.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xliff.ts @@ -27,7 +27,6 @@ const _PLACEHOLDER_TAG = 'x'; const _SOURCE_TAG = 'source'; const _TARGET_TAG = 'target'; const _UNIT_TAG = 'trans-unit'; -const _CR = (ws: number = 0) => new xml.Text(`\n${new Array(ws).join(' ')}`); // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html // http://docs.oasis-open.org/xliff/v1.2/xliff-profile-html/xliff-profile-html-1.2.html @@ -44,34 +43,37 @@ export class Xliff implements Serializer { let transUnit = new xml.Tag(_UNIT_TAG, {id: id, datatype: 'html'}); transUnit.children.push( - _CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), _CR(8), - new xml.Tag(_TARGET_TAG)); + new xml.CR(8), new xml.Tag(_SOURCE_TAG, {}, visitor.serialize(message.nodes)), + new xml.CR(8), new xml.Tag(_TARGET_TAG)); if (message.description) { transUnit.children.push( - _CR(8), + new xml.CR(8), new xml.Tag( 'note', {priority: '1', from: 'description'}, [new xml.Text(message.description)])); } if (message.meaning) { transUnit.children.push( - _CR(8), + new xml.CR(8), new xml.Tag('note', {priority: '1', from: 'meaning'}, [new xml.Text(message.meaning)])); } - transUnit.children.push(_CR(6)); + transUnit.children.push(new xml.CR(6)); - transUnits.push(_CR(6), transUnit); + transUnits.push(new xml.CR(6), transUnit); }); - const body = new xml.Tag('body', {}, [...transUnits, _CR(4)]); + const body = new xml.Tag('body', {}, [...transUnits, new xml.CR(4)]); const file = new xml.Tag( 'file', {'source-language': _SOURCE_LANG, datatype: 'plaintext', original: 'ng2.template'}, - [_CR(4), body, _CR(2)]); - const xliff = new xml.Tag('xliff', {version: _VERSION, xmlns: _XMLNS}, [_CR(2), file, _CR()]); + [new xml.CR(4), body, new xml.CR(2)]); + const xliff = new xml.Tag( + 'xliff', {version: _VERSION, xmlns: _XMLNS}, [new xml.CR(2), file, new xml.CR()]); - return xml.serialize([new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), _CR(), xliff]); + return xml.serialize([ + new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), new xml.CR(), xliff, new xml.CR() + ]); } load(content: string, url: string, messageBundle: MessageBundle): {[id: string]: ml.Node[]} { diff --git a/modules/@angular/compiler/src/i18n/serializers/xmb.ts b/modules/@angular/compiler/src/i18n/serializers/xmb.ts index e19391fd4b..d582ef569b 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xmb.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xmb.ts @@ -43,7 +43,6 @@ export class Xmb implements Serializer { write(messageMap: {[k: string]: i18n.Message}): string { const visitor = new _Visitor(); let rootNode = new xml.Tag(_MESSAGES_TAG); - rootNode.children.push(new xml.Text('\n')); Object.keys(messageMap).forEach((id) => { const message = messageMap[id]; @@ -58,16 +57,18 @@ export class Xmb implements Serializer { } rootNode.children.push( - new xml.Text(' '), new xml.Tag(_MESSAGE_TAG, attrs, visitor.serialize(message.nodes)), - new xml.Text('\n')); + new xml.CR(2), new xml.Tag(_MESSAGE_TAG, attrs, visitor.serialize(message.nodes))); }); + rootNode.children.push(new xml.CR()); + return xml.serialize([ new xml.Declaration({version: '1.0', encoding: 'UTF-8'}), - new xml.Text('\n'), + new xml.CR(), new xml.Doctype(_MESSAGES_TAG, _DOCTYPE), - new xml.Text('\n'), + new xml.CR(), rootNode, + new xml.CR(), ]); } diff --git a/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts b/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts index d5d142581d..e7cbc8bbcb 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts @@ -88,6 +88,10 @@ export class Text implements Node { visit(visitor: IVisitor): any { return visitor.visitText(this); } } +export class CR extends Text { + constructor(ws: number = 0) { super(`\n${new Array(ws + 1).join(' ')}`); } +} + const _ESCAPED_CHARS: [RegExp, string][] = [ [/&/g, '&'], [/"/g, '"'], diff --git a/modules/@angular/compiler/test/i18n/serializers/xliff_spec.ts b/modules/@angular/compiler/test/i18n/serializers/xliff_spec.ts index 48f376b23b..5d9ea96a3b 100644 --- a/modules/@angular/compiler/test/i18n/serializers/xliff_spec.ts +++ b/modules/@angular/compiler/test/i18n/serializers/xliff_spec.ts @@ -21,47 +21,49 @@ const HTML = ` const WRITE_XLIFF = ` - - - - translatable attribute - - - - translatable element with placeholders - - - - foo - - d - m - - - -`; + + + + translatable attribute + + + + translatable element with placeholders + + + + foo + + d + m + + + + +`; const LOAD_XLIFF = ` - - - - translatable attribute - etubirtta elbatalsnart - - - translatable element with placeholders - footnemele elbatalsnart sredlohecalp htiw - - - foo - oof - d - m - - - -`; + + + + translatable attribute + etubirtta elbatalsnart + + + translatable element with placeholders + footnemele elbatalsnart sredlohecalp htiw + + + foo + oof + d + m + + + + +`; export function main(): void { let serializer: Xliff; diff --git a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts index 9243474d4c..89a5daa75c 100644 --- a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts +++ b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts @@ -6,12 +6,10 @@ * found in the LICENSE file at https://angular.io/license */ +import {MessageBundle} from '@angular/compiler/src/i18n/message_bundle'; import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb'; -import {describe, expect, it} from '@angular/core/testing/testing_internal'; - -import {MessageBundle} from '../../../src/i18n/message_bundle'; -import {HtmlParser} from '../../../src/ml_parser/html_parser'; -import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config'; +import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser'; +import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config'; export function main(): void { describe('XMB serializer', () => { @@ -49,7 +47,8 @@ export function main(): void { { count, plural, =0 {<p>test</p>} } foo { count, plural, =0 {{ sex, gender, other {<p>deeply nested</p>} } } } -`; + +`; it('should write a valid xmb file', () => { expect(toXmb(HTML)).toEqual(XMB); });