From 058732bb76ecfcee5c5967e8cd10e5c98d4459ef Mon Sep 17 00:00:00 2001 From: Jarkko Linnanvirta Date: Sun, 19 Mar 2023 07:22:10 +0200 Subject: [PATCH 1/4] Option for disabling escaping --- ansi_up.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ansi_up.ts b/ansi_up.ts index f2aaf8d..b0cccad 100644 --- a/ansi_up.ts +++ b/ansi_up.ts @@ -76,6 +76,7 @@ class AnsiUp private _osc_regex:RegExp; private _url_whitelist:{}; + private _escape_html:boolean; private _buffer:string; @@ -93,6 +94,7 @@ class AnsiUp this._buffer = ''; this._url_whitelist = { 'http':1, 'https':1 }; + this._escape_html = true; } set use_classes(arg:boolean) @@ -115,6 +117,16 @@ class AnsiUp return this._url_whitelist; } + set escape_html(arg:boolean) + { + this._escape_html = arg; + } + + get escape_html():boolean + { + return this._escape_html; + } + private setup_palettes():void { @@ -649,7 +661,8 @@ class AnsiUp if (txt.length === 0) return txt; - txt = this.escape_txt_for_html(txt); + if (this._escape_html) + txt = this.escape_txt_for_html(txt); // If colors not set, default style is used if (!fragment.bold && !fragment.italic && !fragment.underline && fragment.fg === null && fragment.bg === null) @@ -717,7 +730,9 @@ class AnsiUp if (! this._url_whitelist[parts[0]]) return ''; - let result = `${this.escape_txt_for_html(pkt.text)}`; + let result = `${this._escape_html ? this.escape_txt_for_html(pkt.text) : pkt.text}`; + // Escape href="" even if this._escape_html is false, to avoid breaking the in case pkt.url contains a double quote. + return result; } } From e99b6056f41728a7b0f9059eb39a66f13cc390a8 Mon Sep 17 00:00:00 2001 From: Jarkko Linnanvirta Date: Sun, 19 Mar 2023 08:18:12 +0200 Subject: [PATCH 2/4] Readme.md: Documentation for escape_html. --- Readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Readme.md b/Readme.md index 23f39fa..e739b67 100644 --- a/Readme.md +++ b/Readme.md @@ -120,6 +120,11 @@ See the examples directory for a complete CSS theme for these classes. ## Properties +#### escape_html +(default: true) + +By default, HTML's reserved characters `& < > " '` are replaced with HTML entities to make them appear as literal characters in your application, rather than being interpreted as HTML structure. If you prefer keeping HTML's reserved characters untouched, you can set this to false. + #### use_classes (default: false) From b51adf3292b5124cd960726bf03f2f6e7e2239a7 Mon Sep 17 00:00:00 2001 From: Jarkko Linnanvirta Date: Sun, 19 Mar 2023 08:20:38 +0200 Subject: [PATCH 3/4] Apply escape_html condition for all escape_txt_for_html() calls. --- ansi_up.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ansi_up.ts b/ansi_up.ts index b0cccad..28067d7 100644 --- a/ansi_up.ts +++ b/ansi_up.ts @@ -188,6 +188,8 @@ class AnsiUp private escape_txt_for_html(txt:string):string { + if (!this._escape_html) + return txt; return txt.replace(/[&<>"']/gm, (str) => { if (str === "&") return "&"; if (str === "<") return "<"; @@ -661,8 +663,7 @@ class AnsiUp if (txt.length === 0) return txt; - if (this._escape_html) - txt = this.escape_txt_for_html(txt); + txt = this.escape_txt_for_html(txt); // If colors not set, default style is used if (!fragment.bold && !fragment.italic && !fragment.underline && fragment.fg === null && fragment.bg === null) @@ -730,8 +731,7 @@ class AnsiUp if (! this._url_whitelist[parts[0]]) return ''; - let result = `${this._escape_html ? this.escape_txt_for_html(pkt.text) : pkt.text}`; - // Escape href="" even if this._escape_html is false, to avoid breaking the in case pkt.url contains a double quote. + let result = `${this.escape_txt_for_html(pkt.text)}`; return result; } From 88a83626817d0e1f31d8170070b529279b49d345 Mon Sep 17 00:00:00 2001 From: Jarkko Linnanvirta Date: Sun, 19 Mar 2023 08:23:00 +0200 Subject: [PATCH 4/4] Remove accidental newline. --- ansi_up.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ansi_up.ts b/ansi_up.ts index 28067d7..f2abf8b 100644 --- a/ansi_up.ts +++ b/ansi_up.ts @@ -732,7 +732,6 @@ class AnsiUp return ''; let result = `${this.escape_txt_for_html(pkt.text)}`; - return result; } }