From b992bcc7322617fd215ce29a3f67283b89a897e5 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 11 Feb 2017 10:40:34 +0100 Subject: [PATCH] added test for setMessage function, fixing bug for elements with only one child --- js/privatebin.js | 2 +- js/test.js | 52 +++++++++++++++++++++++++++++++++++++++++++++-- tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index 4fd0e99b..8b98ba8b 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -136,7 +136,7 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) { setMessage: function(element, message) { var content = element.contents(); - if (content.length > 0) + if (content.length > 1) { content[content.length - 1].nodeValue = ' ' + message; } diff --git a/js/test.js b/js/test.js index 93e7a8c1..7906743c 100644 --- a/js/test.js +++ b/js/test.js @@ -77,7 +77,7 @@ describe('helper', function () { var html = '', result = true; ids.forEach(function(item, i) { - html += '
' + (contents[i] || contents[0]) + '
'; + html += '
' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
'; }); var clean = jsdom(html); ids.forEach(function(item, i) { @@ -102,7 +102,7 @@ describe('helper', function () { var html = '', result = true; ids.forEach(function(item, i) { - html += '
' + (contents[i] || contents[0]) + '
'; + html += '
' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
'; }); var clean = jsdom(html); ids.forEach(function(item, i) { @@ -117,6 +117,54 @@ describe('helper', function () { ); }); + describe('setMessage', function () { + jsc.property( + 'replaces the content of an empty element, analog to setElementText', + jsc.nearray(jsc.nearray(jsc.elements(alnumString))), + 'nearray string', + 'string', + function (ids, contents, replacingContent) { + var html = '', + result = true; + ids.forEach(function(item, i) { + html += '
' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
'; + }); + var clean = jsdom(html); + ids.forEach(function(item, i) { + var id = item.join(''), + element = $(document.getElementById(id)); + $.PrivateBin.helper.setMessage(element, replacingContent); + result *= replacingContent === $(document.getElementById(id)).text(); + }); + clean(); + return Boolean(result); + } + ); + jsc.property( + 'replaces the last child of a non-empty element', + jsc.nearray(jsc.nearray(jsc.elements(alnumString))), + jsc.nearray(jsc.nearray(jsc.elements(alnumString))), + 'nearray string', + 'string', + function (ids, classes, contents, replacingContent) { + var html = '', + result = true; + ids.forEach(function(item, i) { + html += '
' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '
'; + }); + var clean = jsdom(html); + ids.forEach(function(item, i) { + var id = item.join(''), + element = $(document.getElementById(id)); + $.PrivateBin.helper.setMessage(element, replacingContent); + result *= ' ' + replacingContent === $(document.getElementById(id)).contents()[1].nodeValue; + }); + clean(); + return Boolean(result); + } + ); + }); + describe('scriptLocation', function () { jsc.property( 'returns the URL without query & fragment', diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 60c6727d..50f203bd 100644 --- a/tpl/bootstrap.php +++ b/tpl/bootstrap.php @@ -69,7 +69,7 @@ if ($MARKDOWN): - + diff --git a/tpl/page.php b/tpl/page.php index c92136f3..88af3aa9 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -47,7 +47,7 @@ if ($MARKDOWN): - +