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

Encode all HTML entities in serialized content #5039

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion addons/addon-serialize/src/SerializeAddon.test.ts
Expand Up @@ -145,7 +145,8 @@ describe('SerializeAddon', () => {
const output = serializeAddon.serializeAsHTML({
onlySelection: true
});
assert.equal((output.match(/<div><span>&lt;a>&amp;pi;<\/span><\/div>/g) || []).length, 1, output);
const spanContent = output.match(/<span>([^<]+)<\/span>/)![1];
assert.equal(spanContent, '&#60;a&#62;&#38;pi;');
});

it('cells with bold styling', async () => {
Expand Down
13 changes: 4 additions & 9 deletions addons/addon-serialize/src/SerializeAddon.ts
Expand Up @@ -14,14 +14,6 @@ function constrain(value: number, low: number, high: number): number {
return Math.max(low, Math.min(value, high));
}

function escapeHTMLChar(c: string): string {
switch (c) {
case '&': return '&amp;';
case '<': return '&lt;';
}
return c;
}

// TODO: Refine this template class later
abstract class BaseSerializeHandler {
constructor(
Expand Down Expand Up @@ -677,7 +669,10 @@ export class HTMLSerializeHandler extends BaseSerializeHandler {
if (isEmptyCell) {
this._currentRow += ' ';
} else {
this._currentRow += escapeHTMLChar(cell.getChars());
// Encode HTML entities
this._currentRow += cell.getChars().replace(/[\u00A0-\u9999<>\&]/gim, function(i) {
return '&#' + i.charCodeAt(0) + ';';
});
}
}

Expand Down