From b00bcd1352e1726a3f48309200b79bd9841084a4 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sat, 11 Feb 2017 16:02:24 +0100 Subject: [PATCH] added test for urls2links function, fixing bug - asterisk is allowed in URLs query string --- js/privatebin.js | 4 ++-- js/test.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++ tpl/bootstrap.php | 2 +- tpl/page.php | 2 +- 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index 8b98ba8..e5067f4 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -164,13 +164,13 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) { var markup = '$1'; element.html( element.html().replace( - /((http|https|ftp):\/\/[\w?=&.\/-;#@~%+-]+(?![\w\s?&.\/;#~%"=-]*>))/ig, + /((http|https|ftp):\/\/[\w?=&.\/-;#@~%+*-]+(?![\w\s?&.\/;#~%"=-]*>))/ig, markup ) ); element.html( element.html().replace( - /((magnet):[\w?=&.\/-;#@~%+-]+)/ig, + /((magnet):[\w?=&.\/-;#@~%+*-]+)/ig, markup ) ); diff --git a/js/test.js b/js/test.js index 7906743..09ba881 100644 --- a/js/test.js +++ b/js/test.js @@ -165,6 +165,64 @@ describe('helper', function () { ); }); + describe('urls2links', function () { + jsc.property( + 'ignores non-URL content', + 'string', + function (content) { + var element = $('
' + content + '
'), + before = element.html(); + $.PrivateBin.helper.urls2links(element); + return before === element.html(); + } + ); + jsc.property( + 'replaces URLs with anchors', + 'string', + jsc.elements(['http', 'https', 'ftp']), + jsc.nearray(jsc.elements(a2zString)), + jsc.array(jsc.elements(queryString)), + jsc.array(jsc.elements(queryString)), + 'string', + function (prefix, schema, address, query, fragment, postfix) { + var query = query.join(''), + fragment = fragment.join(''), + url = schema + '://' + address.join('') + '/?' + query + '#' + fragment, + prefix = $.PrivateBin.helper.htmlEntities(prefix), + postfix = ' ' + $.PrivateBin.helper.htmlEntities(postfix), + element = $('
' + prefix + url + postfix + '
'); + + // special cases: When the query string and fragment imply the beginning of an HTML entity, eg. � or &#x + if ( + query.slice(-1) === '&' && + (parseInt(fragment.substring(0, 1), 10) >= 0 || fragment.charAt(0) === 'x' ) + ) + { + url = schema + '://' + address.join('') + '/?' + query.substring(0, query.length - 1); + postfix = ''; + element = $('
' + prefix + url + '
'); + } + + $.PrivateBin.helper.urls2links(element); + return element.html() === $('
' + prefix + '' + url + '' + postfix + '
').html(); + } + ); + jsc.property( + 'replaces magnet links with anchors', + 'string', + jsc.array(jsc.elements(queryString)), + 'string', + function (prefix, query, postfix) { + var url = 'magnet:?' + query.join(''), + prefix = $.PrivateBin.helper.htmlEntities(prefix), + postfix = $.PrivateBin.helper.htmlEntities(postfix), + element = $('
' + prefix + url + ' ' + postfix + '
'); + $.PrivateBin.helper.urls2links(element); + return element.html() === $('
' + prefix + '' + url + ' ' + postfix + '
').html(); + } + ); + }); + describe('scriptLocation', function () { jsc.property( 'returns the URL without query & fragment', diff --git a/tpl/bootstrap.php b/tpl/bootstrap.php index 50f203b..15f83e0 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 88af3aa..ccfeacb 100644 --- a/tpl/page.php +++ b/tpl/page.php @@ -47,7 +47,7 @@ if ($MARKDOWN): - +