From 2b2544a4b139f9ecfae8620a0ef59c18e7ddf440 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Tue, 9 May 2017 14:35:11 -0600
Subject: [PATCH] Deprecate non-standard `String.prototype.rtrim`
---
public/src/utils.js | 10 ++++++++--
src/topics/create.js | 8 ++------
test/utils.js | 7 +++++++
3 files changed, 17 insertions(+), 8 deletions(-)
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();
+ });
});