Allow blockquotes with encoded tags (#4662) * Allow blockquotes with encoded tags Fixes #4634 * Cache canUseNegativeLookbehind result * Optimize regex allocation

d08f3a2eef34959307270b485c6c4d92e725ab43

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
3 files changed, +23 -11Showing whitespace changes
public/script.js+4 -1
@@ -179,6 +179,7 @@ import {
179179 importFromExternalUrl,
180180 shiftUpByOne,
181181 shiftDownByOne,
182+ canUseNegativeLookbehind,
182183} from './scripts/utils.js';
183184import { debounce_timeout, GENERATION_TYPE_TRIGGERS, IGNORE_SYMBOL, inject_ids } from './scripts/constants.js';
184185
@@ -1598,7 +1599,9 @@ export function messageFormatting(mes, ch_name, isSystem, isUser, messageId, san
15981599 }
15991600
16001601 if (!isSystem && power_user.encode_tags) {
1601- mes = mes.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
1602+ mes = canUseNegativeLookbehind()
1603+ ? mes.replaceAll('<', '&lt;').replace(new RegExp('(?<!^|\\n\\s*)>', 'g'), '&gt;')
1604+ : mes.replaceAll('<', '&lt;').replaceAll('>', '&gt;');
16021605 }
16031606
16041607 // Make sure reasoning strings are always shown, even if they include "<" or ">"
public/scripts/slash-commands.js+1 -10
@@ -1,5 +1,5 @@
11import { Fuse, DOMPurify } from '../lib.js';
22import { canUseNegativeLookbehind, copyText, flashHighlight } from './utils.js';
33
44import {
55 Generate,
@@ -5543,15 +5543,6 @@ async function executeSlashCommands(text, handleParserErrors = true, scope = nul
55435543 * @returns {Promise<AutoComplete>}
55445544 */
55455545export async function setSlashCommandAutoComplete(textarea, isFloating = false) {
5546- function canUseNegativeLookbehind() {
5547- try {
5548- new RegExp('(?<!_)');
5549- return true;
5550- } catch (e) {
5551- return false;
5552- }
5553- }
5554-
55555546 if (!canUseNegativeLookbehind()) {
55565547 console.warn('Cannot use negative lookbehind in this browser');
55575548 return;
public/scripts/utils.js+18 -0
@@ -35,6 +35,24 @@ export const localizePagination = function(container) {
3535};
3636
3737/**
38+ * Checks if the current environment supports negative lookbehind in regular expressions.
39+ * @returns {boolean} True if negative lookbehind is supported, false otherwise.
40+ */
41+export function canUseNegativeLookbehind() {
42+ let result = canUseNegativeLookbehind['result'];
43+ if (typeof result !== 'boolean') {
44+ try {
45+ new RegExp('(?<!_)');
46+ result = true;
47+ } catch (e) {
48+ result = false;
49+ }
50+ canUseNegativeLookbehind['result'] = result;
51+ }
52+ return result;
53+}
54+
55+/**
3856 * Renders a dropdown for selecting page size in pagination.
3957 * @param {number} pageSize Page size
4058 * @param {number[]} sizeChangerOptions Array of page size options