fix(XmbSerializer): add meaning attribute, escape attribute values
This commit is contained in:
@ -85,8 +85,9 @@ function _id(el: HtmlElementAst): string {
|
||||
}
|
||||
|
||||
function _serializeMessage(m: Message): string {
|
||||
let desc = isPresent(m.description) ? ` desc='${m.description}'` : '';
|
||||
return `<msg id='${id(m)}'${desc}>${m.content}</msg>`;
|
||||
const desc = isPresent(m.description) ? ` desc='${_escapeXml(m.description)}'` : '';
|
||||
const meaning = isPresent(m.meaning) ? ` meaning='${_escapeXml(m.meaning)}'` : '';
|
||||
return `<msg id='${id(m)}'${desc}${meaning}>${m.content}</msg>`;
|
||||
}
|
||||
|
||||
function _expandPlaceholder(input: string): string {
|
||||
@ -95,3 +96,15 @@ function _expandPlaceholder(input: string): string {
|
||||
return `<ph name=${nameWithQuotes}></ph>`;
|
||||
});
|
||||
}
|
||||
|
||||
const _XML_ESCAPED_CHARS: [RegExp, string][] = [
|
||||
[/&/g, '&'],
|
||||
[/"/g, '"'],
|
||||
[/'/g, '''],
|
||||
[/</g, '<'],
|
||||
[/>/g, '>'],
|
||||
];
|
||||
|
||||
function _escapeXml(value: string): string {
|
||||
return _XML_ESCAPED_CHARS.reduce((value, escape) => value.replace(escape[0], escape[1]), value);
|
||||
}
|
||||
|
@ -10,16 +10,23 @@ export function main() {
|
||||
it('should return an empty message bundle for an empty list of messages',
|
||||
() => { expect(serializeXmb([])).toEqual('<message-bundle></message-bundle>'); });
|
||||
|
||||
it('should serializeXmb messages without desc', () => {
|
||||
let m = new Message('content', 'meaning', null);
|
||||
it('should serialize messages without desc nor meaning', () => {
|
||||
let m = new Message('content', null, null);
|
||||
let expected = `<message-bundle><msg id='${id(m)}'>content</msg></message-bundle>`;
|
||||
expect(serializeXmb([m])).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should serializeXmb messages with desc', () => {
|
||||
it('should serialize messages with desc and meaning', () => {
|
||||
let m = new Message('content', 'meaning', 'description');
|
||||
let expected =
|
||||
`<message-bundle><msg id='${id(m)}' desc='description'>content</msg></message-bundle>`;
|
||||
`<message-bundle><msg id='${id(m)}' desc='description' meaning='meaning'>content</msg></message-bundle>`;
|
||||
expect(serializeXmb([m])).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should escape the desc and meaning', () => {
|
||||
let m = new Message('content', '"\'&<>"\'&<>', '"\'&<>"\'&<>');
|
||||
let expected =
|
||||
`<message-bundle><msg id='${id(m)}' desc='"'&<>"'&<>' meaning='"'&<>"'&<>'>content</msg></message-bundle>`;
|
||||
expect(serializeXmb([m])).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user