fix(compiler): remove i18n markup even if no translations (#17999)

Fixes #11042
This commit is contained in:
Victor Berchet
2017-07-07 16:16:49 -07:00
committed by Jason Aden
parent 2ba3ada27f
commit 9c3386b1b7
11 changed files with 149 additions and 31 deletions

View File

@ -7,6 +7,7 @@
*/
import {DEFAULT_INTERPOLATION_CONFIG, HtmlParser} from '@angular/compiler';
import {MissingTranslationStrategy} from '@angular/core';
import {digest, serializeNodes as serializeI18nNodes} from '../../src/i18n/digest';
import {extractMessages, mergeTranslations} from '../../src/i18n/extractor_merger';
@ -465,6 +466,31 @@ export function main() {
.toEqual(`<div title="">some element</div>`);
});
});
describe('no translations', () => {
it('should remove i18n attributes', () => {
const HTML = `<p i18n="m|d">foo</p>`;
expect(fakeNoTranslate(HTML)).toEqual('<p>foo</p>');
});
it('should remove i18n- attributes', () => {
const HTML = `<p i18n-title="m|d" title="foo"></p>`;
expect(fakeNoTranslate(HTML)).toEqual('<p title="foo"></p>');
});
it('should remove i18n comment blocks', () => {
const HTML = `before<!-- i18n --><p>foo</p><span><i>bar</i></span><!-- /i18n -->after`;
expect(fakeNoTranslate(HTML)).toEqual('before<p>foo</p><span><i>bar</i></span>after');
});
it('should remove nested i18n markup', () => {
const HTML =
`<!-- i18n --><span someAttr="ok">foo</span><div>{count, plural, =0 {<p i18n-title title="foo"></p>}}</div><!-- /i18n -->`;
expect(fakeNoTranslate(HTML))
.toEqual(
'<span someAttr="ok">foo</span><div>{count, plural, =0 {<p title="foo"></p>}}</div>');
});
})
});
}
@ -493,10 +519,22 @@ function fakeTranslate(
i18nMsgMap[id] = [new i18n.Text(`**${text}**`, null !)];
});
const translations = new TranslationBundle(i18nMsgMap, null, digest);
const translationBundle = new TranslationBundle(i18nMsgMap, null, digest);
const output = mergeTranslations(
htmlNodes, translations, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
htmlNodes, translationBundle, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
expect(output.errors).toEqual([]);
return serializeHtmlNodes(output.rootNodes).join('');
}
function fakeNoTranslate(
content: string, implicitTags: string[] = [],
implicitAttrs: {[k: string]: string[]} = {}): string {
const htmlNodes: html.Node[] = parseHtml(content);
const translationBundle = new TranslationBundle(
{}, null, digest, undefined, MissingTranslationStrategy.Ignore, console);
const output = mergeTranslations(
htmlNodes, translationBundle, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
expect(output.errors).toEqual([]);
return serializeHtmlNodes(output.rootNodes).join('');

View File

@ -13,22 +13,6 @@ import {ParseTreeResult} from '@angular/compiler/src/ml_parser/parser';
export function main() {
describe('I18N html parser', () => {
it('should return the html nodes when no translations are given', () => {
const htmlParser = new HtmlParser();
const i18nHtmlParser = new I18NHtmlParser(htmlParser);
const ptResult = new ParseTreeResult([], []);
spyOn(htmlParser, 'parse').and.returnValue(ptResult);
spyOn(i18nHtmlParser, 'parse').and.callThrough();
expect(i18nHtmlParser.parse('source', 'url')).toBe(ptResult);
expect(htmlParser.parse).toHaveBeenCalledTimes(1);
expect(htmlParser.parse)
.toHaveBeenCalledWith('source', 'url', jasmine.anything(), jasmine.anything());
});
// https://github.com/angular/angular/issues/14322
it('should parse the translations only once', () => {
const transBundle = new TranslationBundle({}, null, () => 'id');