Skip to content

Commit de8278b

Browse files
committed
Add exception handling to HTML escaping in compatibility modes with RipURQ and URQ_DOS
1 parent 1f0f050 commit de8278b

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

js/Client.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ Client.prototype.drawText = function () {
169169
this.crtlTextField.empty();
170170

171171
$.each(GlobalPlayer.text, function(index, text) {
172+
var content = text[0];
172173
// RipURQ and URQ_DOS do not support HTML
173-
var div;
174174
if (['ripurq', 'dosurq'].includes(Game.getVar('urq_mode'))) {
175-
div = $('<div>').text(text[0]);
176-
} else {
177-
div = $('<div>').html(text[0]);
175+
// <br> tag can be generated by #/$ construct from URQL code
176+
content = getEscapedHtmlWithAllowedTags(content, ['br']);
178177
}
178+
var div = $('<div>').html(content);
179179

180180
if (div.find('*:not(a, s, b, small, span, q, i)').length == 0) {
181181
div.addClass('text');
@@ -249,7 +249,8 @@ Client.prototype.drawButtons = function () {
249249
var description = button.desc;
250250
// RipURQ and URQ_DOS do not support HTML
251251
if (['ripurq', 'dosurq'].includes(Game.getVar('urq_mode'))) {
252-
description = $('<div>').text(description).html();
252+
// <br> tag can be generated by #/$ construct from URQL code
253+
description = getEscapedHtmlWithAllowedTags(description, ['br']);
253254
}
254255
if (settings['numeric_keys']) {
255256
description = '<b>' + (index + 1) + ':</b> ' + description;

js/tools.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,46 @@ function murmurhash3_32(str) {
153153
}
154154
}
155155

156+
/**
157+
* @param {string} content with HTML
158+
* @param {array} allowed tags
159+
*/
160+
function getEscapedHtmlWithAllowedTags(html, allowedTags) {
161+
if (!allowedTags || !Array.isArray(allowedTags)) {
162+
allowedTags = [];
163+
}
164+
165+
// Regular expression for searching tags and text
166+
var tagRegex = /<([^>]+)>|[^<>]+/g;
167+
168+
// Function to process each part
169+
function processPart(match) {
170+
// If it is a tag
171+
if (match.startsWith('<')) {
172+
var content = match.slice(1, -1);
173+
var tagName = content.match(/^\/?\s*([a-zA-Z]+)/);
174+
175+
if (tagName && allowedTags.includes(tagName[1].toLowerCase())) {
176+
return match; // Return the tag as is
177+
}
178+
179+
// Escape disallowed tag
180+
return match
181+
.replace(/</g, '&lt;')
182+
.replace(/>/g, '&gt;');
183+
}
184+
185+
// If it is text, escape only the necessary characters
186+
return $('<div>')
187+
.text(match)
188+
.html()
189+
.replace(/&amp;/g, '&'); // Restore ampersands
190+
}
191+
192+
// Split the HTML into parts and process each one
193+
return html.replace(tagRegex, processPart);
194+
}
195+
156196
/**
157197
* @param {string} value
158198
*/

0 commit comments

Comments
 (0)