added test for setMessage function, fixing bug for elements with only one child

This commit is contained in:
El RIDO 2017-02-11 10:40:34 +01:00
parent 61a59911b8
commit b992bcc732
No known key found for this signature in database
GPG Key ID: 0F5C940A6BD81F92
4 changed files with 53 additions and 5 deletions

View File

@ -136,7 +136,7 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) {
setMessage: function(element, message) setMessage: function(element, message)
{ {
var content = element.contents(); var content = element.contents();
if (content.length > 0) if (content.length > 1)
{ {
content[content.length - 1].nodeValue = ' ' + message; content[content.length - 1].nodeValue = ' ' + message;
} }

View File

@ -77,7 +77,7 @@ describe('helper', function () {
var html = '', var html = '',
result = true; result = true;
ids.forEach(function(item, i) { ids.forEach(function(item, i) {
html += '<div id="' + item.join('') + '">' + (contents[i] || contents[0]) + '</div>'; html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
}); });
var clean = jsdom(html); var clean = jsdom(html);
ids.forEach(function(item, i) { ids.forEach(function(item, i) {
@ -102,7 +102,7 @@ describe('helper', function () {
var html = '', var html = '',
result = true; result = true;
ids.forEach(function(item, i) { ids.forEach(function(item, i) {
html += '<div id="' + item.join('') + '">' + (contents[i] || contents[0]) + '</div>'; html += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
}); });
var clean = jsdom(html); var clean = jsdom(html);
ids.forEach(function(item, i) { 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 += '<div id="' + item.join('') + '">' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
});
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 += '<div id="' + item.join('') + '"><span class="' + (classes[i] || classes[0]) + '"></span> ' + $.PrivateBin.helper.htmlEntities(contents[i] || contents[0]) + '</div>';
});
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 () { describe('scriptLocation', function () {
jsc.property( jsc.property(
'returns the URL without query & fragment', 'returns the URL without query & fragment',

View File

@ -69,7 +69,7 @@ if ($MARKDOWN):
<?php <?php
endif; endif;
?> ?>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-CvVHawXqZ6ArGeNCmrYkd9brSbFtb73JfnMzj92d9NzNGRxc2O6nPm2d7CX8pgeughLQ45jfL9gidTktUNxvOQ==" crossorigin="anonymous"></script> <script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-2ux6N+vpaJF7Gal63ZMJCdcOsQsaNcMbZwRqh+/pwAH5N6ZpEPgN5LiOGHVGGcna4Cut4veXu7lfRa8ZqEzHkw==" crossorigin="anonymous"></script>
<!--[if lt IE 10]> <!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style> <style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]--> <![endif]-->

View File

@ -47,7 +47,7 @@ if ($MARKDOWN):
<?php <?php
endif; endif;
?> ?>
<script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-CvVHawXqZ6ArGeNCmrYkd9brSbFtb73JfnMzj92d9NzNGRxc2O6nPm2d7CX8pgeughLQ45jfL9gidTktUNxvOQ==" crossorigin="anonymous"></script> <script type="text/javascript" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-2ux6N+vpaJF7Gal63ZMJCdcOsQsaNcMbZwRqh+/pwAH5N6ZpEPgN5LiOGHVGGcna4Cut4veXu7lfRa8ZqEzHkw==" crossorigin="anonymous"></script>
<!--[if lt IE 10]> <!--[if lt IE 10]>
<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style> <style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
<![endif]--> <![endif]-->