Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Issue with special message rendering #19817

Merged
merged 1 commit into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/message-attachments/client/renderField.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Template } from 'meteor/templating';
import { Blaze } from 'meteor/blaze';

import { escapeHTML } from '../../../lib/escapeHTML';

const renderers = {};

/**
Expand Down Expand Up @@ -49,7 +51,7 @@ Template.renderField.helpers({
html = Blaze.toHTMLWithData(Template[renderers[field.type]], { field, message });
} else {
// consider the value already formatted as html
html = field.value;
html = escapeHTML(field.value);
}
return `<div class="${ field.type }">${ html }</div>`;
},
Expand Down
19 changes: 19 additions & 0 deletions lib/escapeHTML.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import assert from 'assert';

import { describe, it } from 'mocha';

import { escapeHTML } from './escapeHTML';

describe('escapeHTML', () => {
it('works', () => {
assert.strictEqual(escapeHTML('<div>Blah & "blah" & \'blah\'</div>'), '&lt;div&gt;Blah &amp; &quot;blah&quot; &amp; &#39;blah&#39;&lt;/div&gt;');
assert.strictEqual(escapeHTML('&lt;'), '&amp;lt;');
assert.strictEqual(escapeHTML(' '), ' ');
assert.strictEqual(escapeHTML('¢'), '&cent;');
assert.strictEqual(escapeHTML('¢ £ ¥ € © ®'), '&cent; &pound; &yen; &euro; &copy; &reg;');
assert.strictEqual(escapeHTML(5 as unknown as string), '5');
assert.strictEqual(escapeHTML(''), '');
assert.strictEqual(escapeHTML(null as unknown as string), '');
assert.strictEqual(escapeHTML(undefined as unknown as string), '');
});
});
27 changes: 27 additions & 0 deletions lib/escapeHTML.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const characterToHtmlEntityCode = {
'¢': 'cent',
'£': 'pound',
'¥': 'yen',
'€': 'euro',
'©': 'copy',
'®': 'reg',
'<': 'lt',
'>': 'gt',
'"': 'quot',
'&': 'amp',
'\'': '#39',
} as const;

const regex = new RegExp(`[${ Object.keys(characterToHtmlEntityCode).join('') }]`, 'g');

const toString = (object: unknown): string =>
(object ? `${ object }` : '');

const isEscapable = (char: string): char is keyof typeof characterToHtmlEntityCode =>
char in characterToHtmlEntityCode;

const escapeChar = (char: string): string =>
(isEscapable(char) ? `&${ characterToHtmlEntityCode[char] };` : '');

export const escapeHTML = (str: string): string =>
toString(str).replace(regex, escapeChar);