Skip to content

Commit

Permalink
Allow disabling Escape Characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaynator495 committed Aug 18, 2023
1 parent 99727d0 commit 6b5f0c1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
20 changes: 19 additions & 1 deletion src/bbcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -234,6 +238,7 @@ export default class BBCode {
this.maxEmoji = -1;
this.escapeContent = true;
this.stack = [];
this.allowEscape = true;
}
//-----------------------------------------------------------------------------
// State control.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
34 changes: 22 additions & 12 deletions src/bbcodelexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,18 @@ 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.
*
* @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
Expand Down Expand Up @@ -126,7 +130,11 @@ export default class BBCodeLexer {
const start = regexBeginMarkers[tagMarker];
this.tagMarker = tagMarker;
this.endTagMarker = endMarkers[tagMarker];
this.escapeRegex = "(?<!\\\\)";
this.allowEscape = allowEscape;
if (allowEscape)
this.escapeRegex = "(?<!\\\\)";
else
this.escapeRegex = "";
// this.input will be an array of tokens, with the special property that
// the elements strictly alternate between plain text and tags/whitespace/newlines,
// and that tags always have *two* entries per tag. The first element will
Expand Down Expand Up @@ -163,16 +171,18 @@ export default class BBCodeLexer {
+")";
this.input = preg_split(this.patMain, string, -1, PREG_SPLIT_DELIM_CAPTURE);

this.genEscapeRegex = new RegExp(this.patMain.replaceAll(this.escapeRegex, "(\\\\)"), "g");
for (const input in this.input) {
const value = this.input[input];
this.input[input] = value.replace(this.genEscapeRegex, function(match) {
// If there's a backslash before the match, remove it
if (match[0] === '\\') {
return match.slice(1); // Remove the backslash
}
return match;
});
if (this.allowEscape) {
this.genEscapeRegex = new RegExp(this.patMain.replaceAll(this.escapeRegex, "(\\\\)"), "g");
for (const input in this.input) {
const value = this.input[input];
this.input[input] = value.replace(this.genEscapeRegex, function(match) {
// If there's a backslash before the match, remove it
if (match[0] === '\\') {
return match.slice(1); // Remove the backslash
}
return match;
});
}
}

// Patterns for matching specific types of tokens during lexing. (originally contained Dx flags)
Expand Down
14 changes: 11 additions & 3 deletions tests/nbbc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,12 @@ const tests = {
descr: "Wiki bbcode can't yet be escaped",
bbcode: "\\[[wiki]] \\[[wiki|test]]",
html: "\\[[wiki]] \\[[wiki|test]]"
},
{
descr: "BBCode Escape can be disabled",
bbcode: "\\[b]test[/b]",
html: "\\<b>test</b>",
allowEscape: false
}
],
"Misc Tests": [
Expand Down Expand Up @@ -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() {};
Expand All @@ -930,17 +936,19 @@ 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);
}
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);
Expand Down

0 comments on commit 6b5f0c1

Please sign in to comment.