From b16e2205edde45d902cae141e081fb43ad7e05c4 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sun, 7 Dec 2014 03:24:11 -0800 Subject: [PATCH] Fix text content of Linkify ranges, add test. --- Gruntfile.coffee | 5 +++++ src/Linkification/Linkify.coffee | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) 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