fix(compiler): fix support for html-like text in translatable attributes (#23053)

PR Close #23053
This commit is contained in:
Victor Berchet
2018-03-28 22:10:08 -07:00
committed by Alex Rickabaugh
parent 7de13b60d6
commit 28058b784b
7 changed files with 55 additions and 8 deletions

View File

@ -54,7 +54,7 @@ export class Declaration implements Node {
constructor(unescapedAttrs: {[k: string]: string}) {
Object.keys(unescapedAttrs).forEach((k: string) => {
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
this.attrs[k] = escapeXml(unescapedAttrs[k]);
});
}
@ -74,7 +74,7 @@ export class Tag implements Node {
public name: string, unescapedAttrs: {[k: string]: string} = {},
public children: Node[] = []) {
Object.keys(unescapedAttrs).forEach((k: string) => {
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
this.attrs[k] = escapeXml(unescapedAttrs[k]);
});
}
@ -83,7 +83,7 @@ export class Tag implements Node {
export class Text implements Node {
value: string;
constructor(unescapedValue: string) { this.value = _escapeXml(unescapedValue); }
constructor(unescapedValue: string) { this.value = escapeXml(unescapedValue); }
visit(visitor: IVisitor): any { return visitor.visitText(this); }
}
@ -100,7 +100,8 @@ const _ESCAPED_CHARS: [RegExp, string][] = [
[/>/g, '>'],
];
function _escapeXml(text: string): string {
// Escape `_ESCAPED_CHARS` characters in the given text with encoded entities
export function escapeXml(text: string): string {
return _ESCAPED_CHARS.reduce(
(text: string, entry: [RegExp, string]) => text.replace(entry[0], entry[1]), text);
}

View File

@ -14,6 +14,7 @@ import {Console} from '../util';
import * as i18n from './i18n_ast';
import {I18nError} from './parse_util';
import {PlaceholderMapper, Serializer} from './serializers/serializer';
import {escapeXml} from './serializers/xml_helper';
/**
@ -88,7 +89,11 @@ class I18nToHtmlVisitor implements i18n.Visitor {
};
}
visitText(text: i18n.Text, context?: any): string { return text.value; }
visitText(text: i18n.Text, context?: any): string {
// `convert()` uses an `HtmlParser` to return `html.Node`s
// we should then make sure that any special characters are escaped
return escapeXml(text.value);
}
visitContainer(container: i18n.Container, context?: any): any {
return container.children.map(n => n.visit(this)).join('');