From 6b5f0c1605b31aade0cd351c04079458051a8e79 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 17 Aug 2023 20:23:22 -0700 Subject: [PATCH] Allow disabling Escape Characters --- src/bbcode.ts | 20 +++++++++++++++++++- src/bbcodelexer.ts | 34 ++++++++++++++++++++++------------ tests/nbbc.test.ts | 14 +++++++++++--- 3 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/bbcode.ts b/src/bbcode.ts index 2ce4c97..6f02e03 100644 --- a/src/bbcode.ts +++ b/src/bbcode.ts @@ -189,6 +189,10 @@ export default class BBCode { * BBCodeLexer created when calling parse */ protected lexer: BBCodeLexer; + /** + * Whether to allow Escape Characters in parsing to prevent parsing specific tags or not + */ + protected allowEscape: boolean; /** * Initialize a new instance of the {@link BBCode} class. @@ -234,6 +238,7 @@ export default class BBCode { this.maxEmoji = -1; this.escapeContent = true; this.stack = []; + this.allowEscape = true; } //----------------------------------------------------------------------------- // State control. @@ -529,6 +534,19 @@ export default class BBCode { public getURLTemplate() { return this.urlTemplate; } + /** + * Whether to allow BBCode to be escaped or not. + */ + public setAllowEscape(bool: boolean) { + this.allowEscape = bool; + return this; + } + /** + * Get if escape characters are allowed + */ + public getAllowEscape() { + return this.allowEscape; + } /** * Set the template to use for quote tags * @param template @@ -2527,7 +2545,7 @@ export default class BBCode { // and not a character-by-character tokenizer, the structure of the input // must be known in advance, which is why the tag marker cannot be changed // during the parse. - this.lexer = new BBCodeLexer(string, this.tagMarker, this.debug); + this.lexer = new BBCodeLexer(string, this.tagMarker, this.debug, this.allowEscape); this.lexer.debug = this.debug; // If we're fuzzily limiting the text length, see if we need to actually // cut it off, or if it's close enough to not be worth the effort. diff --git a/src/bbcodelexer.ts b/src/bbcodelexer.ts index a3a37f6..eedbf51 100644 --- a/src/bbcodelexer.ts +++ b/src/bbcodelexer.ts @@ -87,6 +87,10 @@ export default class BBCodeLexer { * - Nothing, this will disable escape characters */ public escapeRegex: string; + /** + * Whether to allow Escape Characters or not + */ + public allowEscape: boolean; /** * Instantiate a new instance of the {@link BBCodeLexer} class. @@ -94,7 +98,7 @@ export default class BBCodeLexer { * @param string The string to be broken up into tokens. * @param tagMarker The BBCode tag marker. */ - public constructor(string: string, tagMarker = '[', debug: boolean = false) { + public constructor(string: string, tagMarker = '[', debug: boolean = false, allowEscape: boolean = true) { // First thing we do is to split the input string into tuples of // text and tags. This will make it easy to tokenize. We define a tag as // anything starting with a [, ending with a ], and containing no [ or ] in @@ -126,7 +130,11 @@ export default class BBCodeLexer { const start = regexBeginMarkers[tagMarker]; this.tagMarker = tagMarker; this.endTagMarker = endMarkers[tagMarker]; - this.escapeRegex = "(?test", + allowEscape: false } ], "Misc Tests": [ @@ -903,7 +909,7 @@ bbcode.addRule('border', { bbcode.setLocalImgDir("smileys"); bbcode.setLocalImgURL("smileys"); -console.log = function() {}; +//console.log = function() {}; console.info = function() {}; console.warn = function() {}; console.error = function() {}; @@ -930,9 +936,12 @@ for (const testcat in tests) { if (typeof test['urlforcetarget'] == "string") bbcode.setURLTarget(test['urlforcetarget']); else bbcode.setURLTarget(false); - if (test['plainmode']) + if (typeof test['plainmode'] !== "undefined") bbcode.setPlainMode(test['plainmode']); else bbcode.setPlainMode(false); + if (typeof test["allowEscape"] !== "undefined") + bbcode.setAllowEscape(test["allowEscape"]); + else bbcode.setAllowEscape(true); if (test['tag_marker'] == '<') { bbcode.setTagMarker('<'); bbcode.setAllowAmpersand(true); @@ -940,7 +949,6 @@ for (const testcat in tests) { else if (test['tag_marker']) bbcode.setTagMarker(test['tag_marker']); - try { /*if (test['regex']) expect(new RegExp(test['regex']).test(bbcode.parse(test['bbcode']))).toBe(true);