Skip to content

Commit 1709d44

Browse files
authored
Merge pull request #5020 from wavetermdev/escape-html
Escape Unsafe HTML Characters in addon-serialize
2 parents 499afa1 + 4310456 commit 1709d44

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

addons/addon-serialize/src/SerializeAddon.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ describe('SerializeAddon', () => {
138138
assert.equal((output.match(/<div><span>terminal<\/span><\/div>/g) || []).length, 1, output);
139139
});
140140

141+
it('basic terminal with html unsafe chars', async () => {
142+
await writeP(terminal, ' <a>&pi; ');
143+
terminal.select(1, 0, 7);
144+
145+
const output = serializeAddon.serializeAsHTML({
146+
onlySelection: true
147+
});
148+
assert.equal((output.match(/<div><span>&lt;a>&amp;pi;<\/span><\/div>/g) || []).length, 1, output);
149+
});
150+
141151
it('cells with bold styling', async () => {
142152
await writeP(terminal, ' ' + sgr('1') + 'terminal' + sgr('22') + ' ');
143153

addons/addon-serialize/src/SerializeAddon.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ function constrain(value: number, low: number, high: number): number {
1414
return Math.max(low, Math.min(value, high));
1515
}
1616

17+
function escapeHTMLChar(c: string): string {
18+
switch (c) {
19+
case '&': return '&amp;';
20+
case '<': return '&lt;';
21+
}
22+
return c;
23+
}
24+
1725
// TODO: Refine this template class later
1826
abstract class BaseSerializeHandler {
1927
constructor(
@@ -669,7 +677,7 @@ export class HTMLSerializeHandler extends BaseSerializeHandler {
669677
if (isEmptyCell) {
670678
this._currentRow += ' ';
671679
} else {
672-
this._currentRow += cell.getChars();
680+
this._currentRow += escapeHTMLChar(cell.getChars());
673681
}
674682
}
675683

0 commit comments

Comments
 (0)