diff options
author | Alex Yatskov <alex@foosoft.net> | 2016-06-12 14:32:23 -0700 |
---|---|---|
committer | Alex Yatskov <alex@foosoft.net> | 2016-06-14 20:44:38 -0700 |
commit | 061cbb0141df513113563c86e5df7c17cd46700f (patch) | |
tree | 348902250a9ab755c98bb03e1f27e8ab6a5857eb | |
parent | 14fd0d65144bd672bcfb9702fdfb96d0963f350b (diff) |
Better selection handling, fixing scan length being treated as a string.
-rw-r--r-- | ext/bg/js/options-form.js | 2 | ||||
-rw-r--r-- | ext/bg/js/options.js | 2 | ||||
-rw-r--r-- | ext/fg/js/range.js | 27 |
3 files changed, 23 insertions, 8 deletions
diff --git a/ext/bg/js/options-form.js b/ext/bg/js/options-form.js index 239b09d5..d515b789 100644 --- a/ext/bg/js/options-form.js +++ b/ext/bg/js/options-form.js @@ -40,7 +40,7 @@ function formToOptions(section, callback) { switch (section) { case 'general': - optsNew.scanLength = $('#scan-length').val(); + optsNew.scanLength = parseInt($('#scan-length').val()); optsNew.activateOnStartup = $('#activate-on-startup').prop('checked'); optsNew.loadEnamDict = $('#load-enamdict').prop('checked'); optsNew.selectMatchedText = $('#select-matched-text').prop('checked'); diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js index f131ab9b..0a5197f9 100644 --- a/ext/bg/js/options.js +++ b/ext/bg/js/options.js @@ -38,6 +38,8 @@ function sanitizeOptions(options) { } } + options.scanLength = parseInt(options.scanLength); + return options; } diff --git a/ext/fg/js/range.js b/ext/fg/js/range.js index 0cf79ddf..f6af7122 100644 --- a/ext/fg/js/range.js +++ b/ext/fg/js/range.js @@ -27,16 +27,29 @@ class Range { } setLength(length) { - const node = this.rng.startContainer; - const offset = this.rng.startOffset; + const end = this.findEnd(this.rng.startContainer, this.rng.startOffset, length); + this.rng.setEnd(end.node, end.offset); + } + + findEnd(node, offset, length) { + if (node.nodeType === 3) { + const remainder = node.data.length - offset; + if (remainder >= length) { + return {node, offset: offset + length}; + } + + length -= remainder; + } + + if (node.childNodes.length > 0) { + return this.findEnd(node.childNodes[0], 0, length); + } - length = Math.min(node.length - offset, length); - if (length === 0) { - return null; + if (node.nextSibling !== null) { + return this.findEnd(node.nextSibling, 0, length); } - this.rng.setEnd(node, offset + length); - return length; + return {node, offset: node.data.length}; } containsPoint(point) { |