diff --git a/public/src/utils.js b/public/src/utils.js index 8f2dcb8e87..f1154b64e9 100644 --- a/public/src/utils.js +++ b/public/src/utils.js @@ -405,9 +405,13 @@ (relative_path.length > 0 ? targetLocation.pathname.indexOf(relative_path) === 0 : true) // Subfolder installs need this additional check ); }, + + rtrim: function (str) { + return str.replace(/\s+$/g, ''); + }, }; - /* eslint "no-extend-native": "off" */ + /* eslint "no-extend-native": "off" */ if (typeof String.prototype.startsWith !== 'function') { String.prototype.startsWith = function (prefix) { if (this.length < prefix.length) { @@ -429,9 +433,11 @@ }; } + // DEPRECATED: remove in 1.6 if (typeof String.prototype.rtrim !== 'function') { String.prototype.rtrim = function () { - return this.replace(/\s+$/g, ''); + console.warn('[deprecated] `String.prototype.rtrim` is deprecated as of NodeBB v1.5; use `utils.rtrim` instead.'); + return utils.rtrim(this); }; } diff --git a/src/topics/create.js b/src/topics/create.js index 2ec75d3781..f7ee2e326e 100644 --- a/src/topics/create.js +++ b/src/topics/create.js @@ -88,10 +88,6 @@ module.exports = function (Topics) { ], callback); }; - function rtrim(str) { - return str.replace(/\s+$/g, ''); - } - Topics.post = function (data, callback) { var uid = data.uid; data.title = String(data.title).trim(); @@ -106,7 +102,7 @@ module.exports = function (Topics) { }, function (next) { if (data.content) { - data.content = rtrim(data.content); + data.content = utils.rtrim(data.content); } check(data.content, meta.config.minimumPostLength, meta.config.maximumPostLength, 'content-too-short', 'content-too-long', next); }, @@ -239,7 +235,7 @@ module.exports = function (Topics) { function (filteredData, next) { content = filteredData.content || data.content; if (content) { - content = rtrim(content); + content = utils.rtrim(content); } check(content, meta.config.minimumPostLength, meta.config.maximumPostLength, 'content-too-short', 'content-too-long', next); diff --git a/test/utils.js b/test/utils.js index 2beedcf3eb..9c8c2bfb12 100644 --- a/test/utils.js +++ b/test/utils.js @@ -171,4 +171,11 @@ describe('Utility Methods', function () { } done(); }); + + it('`utils.rtrim` should remove trailing space', function (done) { + assert.strictEqual(utils.rtrim(' thing '), ' thing'); + assert.strictEqual(utils.rtrim('\tthing\t\t'), '\tthing'); + assert.strictEqual(utils.rtrim('\t thing \t'), '\t thing'); + done(); + }); });