Просмотр исходного кода

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

El RIDO 9 лет назад
Родитель
Сommit
b992bcc732
4 измененных файлов с 53 добавлено и 5 удалено
  1. 1 1
      js/privatebin.js
  2. 50 2
      js/test.js
  3. 1 1
      tpl/bootstrap.php
  4. 1 1
      tpl/page.php

+ 1 - 1
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;
             }

+ 50 - 2
js/test.js

@@ -77,7 +77,7 @@ describe('helper', function () {
                 var html = '',
                     result = true;
                 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);
                 ids.forEach(function(item, i) {
@@ -102,7 +102,7 @@ describe('helper', function () {
                 var html = '',
                     result = true;
                 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);
                 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 () {
         jsc.property(
             'returns the URL without query & fragment',

+ 1 - 1
tpl/bootstrap.php

@@ -69,7 +69,7 @@ if ($MARKDOWN):
 <?php
 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]>
 		<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
 		<![endif]-->

+ 1 - 1
tpl/page.php

@@ -47,7 +47,7 @@ if ($MARKDOWN):
 <?php
 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]>
 		<style type="text/css">body {padding-left:60px;padding-right:60px;} #ienotice {display:block;} #oldienotice {display:block;}</style>
 		<![endif]-->