Skip to content

Commit bf1bfcb

Browse files
committed
Fixed html escaping in execute code output
1 parent c6ebec6 commit bf1bfcb

4 files changed

Lines changed: 49 additions & 39 deletions

File tree

‎main.js‎

Lines changed: 35 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/ExecuteCode.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Notice, setIcon } from "obsidian";
22

33
import { CBCParameters } from "./Parsing";
44
import CodeBlockCustomizerPlugin from "./main";
5-
import { renderCodeBlockLines } from "./ReadingView/ReadingViewUtils";
5+
import { escapeHtml, renderCodeBlockLines } from "./ReadingView/ReadingViewUtils";
66

77
export function addAndObserveExecuteCodeButtons(frag: DocumentFragment, targetPreElement: HTMLElement | undefined, parameters: CBCParameters, plugin: CodeBlockCustomizerPlugin): MutationObserver | null {
88
const executeButton = createExecuteCodeRunButton();
@@ -306,7 +306,7 @@ async function processExecuteCodeOutput(outputElement: HTMLElement, observer: Mu
306306
container.appendChild(lineWrapper);
307307
} else {
308308
const { fragment } = await renderCodeBlockLines({
309-
htmlLines: lines,
309+
htmlLines: lines.map(escapeHtml), // the output is plain text, not HTML
310310
textLines: lines,
311311
lineCount: lines.length,
312312
parameters,

‎src/ReadingView/ReadingViewUtils.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,16 @@ function addIndentLine(inputString: string, insertCollapse = false, logicalInden
372372
return inputString.replace(indentRegex, finalReplacement);
373373
}// addIndentLine
374374

375+
export function escapeHtml(text: string): string {
376+
return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\u00a0/g, '&nbsp;');
377+
}// escapeHtml
378+
375379
export function extractLinesFromHTML(container: HTMLElement): { htmlLines: string[]; textLines: string[] } {
376380
const lines: string[] = [''];
377381
const openTags: HTMLElement[] = [];
378382

379383
// a " in an attribute value (possible in HTML code blocks) would otherwise end the attribute and inject new ones
380384
const escapeAttribute = (value: string): string => value.replace(/&/g, '&amp;').replace(/"/g, '&quot;');
381-
const escapeHtml = (text: string): string => text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\u00a0/g, '&nbsp;');
382385

383386
const walk = (node: Node) => {
384387
if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).tagName.toLowerCase() === 'br') {

‎tests/readingViewBlocks.test.cjs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { test } = require('node:test');
33
const assert = require('node:assert/strict');
44
const { load } = require('./helpers/load.cjs');
55

6-
const { extractCodeBlocksFromSection, getBlockLine, getHtmlCodeBlockData, findHtmlCodeBlocks } = load('src/ReadingView/ReadingViewUtils.ts', 'src/HtmlCodeBlocks.ts');
6+
const { extractCodeBlocksFromSection, getBlockLine, getHtmlCodeBlockData, findHtmlCodeBlocks, escapeHtml } = load('src/ReadingView/ReadingViewUtils.ts', 'src/HtmlCodeBlocks.ts');
77

88
test('blocks of a list section get their own line, not the section start', () => {
99
const section = [
@@ -62,3 +62,10 @@ test('HTML blocks use the line of the opening <code> tag, like the editor', () =
6262
assert.equal(data.firstLineOffset, 1);
6363
assert.equal(getBlockLine(20, data), 21);
6464
});
65+
66+
test('escapeHtml: plain text (e.g. Execute Code output) stays text when parsed as HTML', () => {
67+
assert.equal(escapeHtml('a<b & c>d'), 'a&lt;b &amp; c&gt;d');
68+
assert.equal(escapeHtml('<img src=x onerror=alert(1)>'), '&lt;img src=x onerror=alert(1)&gt;');
69+
assert.equal(escapeHtml('&lt;'), '&amp;lt;', 'an entity in the output is shown as typed');
70+
assert.equal(escapeHtml('a b'), 'a&nbsp;b');
71+
});

0 commit comments

Comments
 (0)