From b9c05b06d0aa318ac8dd49c1c7f405d37abc3135 Mon Sep 17 00:00:00 2001
From: El RIDO
Date: Sat, 11 Feb 2017 19:34:51 +0100
Subject: [PATCH] added test for sprintf function, removing dead code and
optimizing test cases
---
js/privatebin.js | 36 ++++++++--------
js/test.js | 104 ++++++++++++++++++++++++++++++++++++++++------
tpl/bootstrap.php | 2 +-
tpl/page.php | 2 +-
4 files changed, 111 insertions(+), 33 deletions(-)
diff --git a/js/privatebin.js b/js/privatebin.js
index e5067f47..bb742ea8 100644
--- a/js/privatebin.js
+++ b/js/privatebin.js
@@ -179,6 +179,9 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
/**
* minimal sprintf emulation for %s and %d formats
*
+ * Note that this function needs the parameters in the same order as the
+ * format strings appear in the string, contrary to the original.
+ *
* @see {@link https://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format#4795914}
* @name helper.sprintf
* @function
@@ -195,27 +198,22 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
}
var format = args[0],
i = 1;
- return format.replace(/%((%)|s|d)/g, function (m) {
+ return format.replace(/%(s|d)/g, function (m) {
// m is the matched format, e.g. %s, %d
- var val;
- if (m[2]) {
- val = m[2];
- } else {
- val = args[i];
- // A switch statement so that the formatter can be extended.
- switch (m)
- {
- case '%d':
- val = parseFloat(val);
- if (isNaN(val)) {
- val = 0;
- }
- break;
- default:
- // Default is %s
- }
- ++i;
+ var val = args[i];
+ // A switch statement so that the formatter can be extended.
+ switch (m)
+ {
+ case '%d':
+ val = parseFloat(val);
+ if (isNaN(val)) {
+ val = 0;
+ }
+ break;
+ default:
+ // Default is %s
}
+ ++i;
return val;
});
},
diff --git a/js/test.js b/js/test.js
index 09ba8813..cf44a19c 100644
--- a/js/test.js
+++ b/js/test.js
@@ -93,6 +93,10 @@ describe('helper', function () {
});
describe('setElementText', function () {
+ after(function () {
+ cleanup();
+ });
+
jsc.property(
'replaces the content of an element',
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
@@ -104,20 +108,23 @@ describe('helper', function () {
ids.forEach(function(item, i) {
html += '' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
';
});
- var clean = jsdom(html);
+ var elements = $('').html(html);
ids.forEach(function(item, i) {
var id = item.join(''),
- element = $(document.getElementById(id));
+ element = elements.find('#' + id).first();
$.PrivateBin.helper.setElementText(element, replacingContent);
- result *= replacingContent === $(document.getElementById(id)).text();
+ result *= replacingContent === element.text();
});
- clean();
return Boolean(result);
}
);
});
describe('setMessage', function () {
+ after(function () {
+ cleanup();
+ });
+
jsc.property(
'replaces the content of an empty element, analog to setElementText',
jsc.nearray(jsc.nearray(jsc.elements(alnumString))),
@@ -129,14 +136,13 @@ describe('helper', function () {
ids.forEach(function(item, i) {
html += '
' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
';
});
- var clean = jsdom(html);
+ var elements = $('