diff --git a/Gruntfile.coffee b/Gruntfile.coffee index a57a1d5ea..f11582ef9 100755 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -25,6 +25,10 @@ module.exports = (grunt) -> output = if parts2.length is 0 then '""' else parts2.join ' + ' "(innerHTML: #{output})" + assert = (statement, objs...) -> + return '' unless grunt.config('pkg').tests_enabled + "throw new Error 'Assertion failed: ' + `#{JSON.stringify statement}` unless #{statement}" + # Project configuration. grunt.initConfig pkg: grunt.file.readJSON 'package.json' @@ -35,6 +39,7 @@ module.exports = (grunt) -> pkg = grunt.config 'pkg' pkg.importHTML = importHTML pkg.html = html + pkg.assert = assert pkg.tests_enabled or= false pkg enumerable: true diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index ebd12b44c..2e06952fe 100755 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -52,16 +52,20 @@ Linkify = endNode = saved {data} = saved - word += data - {length} = data if end = space.exec data # Set our snapshot and regex to start on this node at this position when the loop resumes + word += data[...end.index] test.lastIndex = length = end.index i-- break + else + {length} = data + word += data - links.push Linkify.makeRange node, endNode, index, length if Linkify.regString.exec word + if Linkify.regString.exec word + links.push Linkify.makeRange node, endNode, index, length + <%= assert('word is links[links.length-1].toString()') %> break unless test.lastIndex and node is endNode @@ -105,7 +109,7 @@ Linkify = # Clean start of range i = text.search Linkify.regString - if i + if i > 0 text = text.slice i i-- while range.startOffset + i >= range.startContainer.data.length