Comparing version 2.2.1 to 3.0.0
@@ -30,7 +30,5 @@ !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Curse=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var _ref = arguments[1] === undefined ? {} : arguments[1]; | ||
var lineBreak = _ref.lineBreak; | ||
var nodeLengths = _ref.nodeLengths; | ||
this.lineBreak = lineBreak; | ||
this.nodeLengths = nodeLengths || {}; | ||
var nodeLengthFn = _ref.nodeLengthFn; | ||
this.element = element; | ||
this.nodeLengthFn = nodeLengthFn; | ||
this.reset(); | ||
@@ -157,2 +155,3 @@ }; | ||
if (!setStart && start <= idx + nodeLength) { | ||
this.indexOfNode(node); | ||
if (isText) { | ||
@@ -278,18 +277,26 @@ range.setStart(node, start - idx); | ||
* @param {Node} node a Node, typically a Text or HTMLElement node | ||
* @param {Bool} ignoreNodeLengthFn ignore the custom nodeLengthFn | ||
* @return {Number} the length of the node, as text | ||
*/ | ||
value: function nodeLength(node) { | ||
var charNodes = ["BR", "HR", "IMG"]; | ||
var previousSibling = undefined; | ||
var _this = this; | ||
var ignoreNodeLengthFn = arguments[1] === undefined ? false : arguments[1]; | ||
if (this.nodeLengthFn && !ignoreNodeLengthFn) { | ||
var _ret = (function () { | ||
var nodeLength = _this.nodeLength.bind(_this); | ||
if (this.lineBreak) { | ||
previousSibling = node.previousElementSibling; | ||
return { | ||
v: _this.nodeLengthFn(node, function __super() { | ||
return nodeLength(node, true); | ||
}) | ||
}; | ||
})(); | ||
if (typeof _ret === "object") return _ret.v; | ||
} | ||
if (previousSibling && previousSibling.classList.contains(this.lineBreak)) { | ||
return 1; | ||
} else if (node.nodeName === "#text") { | ||
var charNodes = ["BR", "HR", "IMG"]; | ||
if (node.nodeName === "#text") { | ||
return node.data.length; | ||
} else if (this.nodeLengths[node.nodeName]) { | ||
return this.nodeLengths[node.nodeName]; | ||
} else if (charNodes.indexOf(node.nodeName) > -1) { | ||
@@ -315,8 +322,8 @@ return 1; | ||
value: function offset() { | ||
var _this = this; | ||
var _this2 = this; | ||
var startOffset = arguments[0] === undefined ? 0 : arguments[0]; | ||
var endOffset = arguments[1] === undefined ? startOffset : arguments[1]; | ||
return (function () { | ||
_this.start += startOffset; | ||
_this.end += endOffset; | ||
_this2.start += startOffset; | ||
_this2.end += endOffset; | ||
})(); | ||
@@ -323,0 +330,0 @@ }, |
{ | ||
"name": "cursejs", | ||
"version": "2.2.1", | ||
"version": "3.0.0", | ||
"description": "A user selection caching utility", | ||
@@ -5,0 +5,0 @@ "main": "dist/curse.js", |
@@ -49,2 +49,22 @@ # Curse | ||
It's possible that depending on your setup, you may need to pass a custom | ||
function to a Curse to count node length. This can be done by passing | ||
`nodeLengthFn`: | ||
```javascript | ||
let curse = new Curse(element, { nodeLengthFn: nodeLengthFn }); | ||
function nodeLengthFn(node, __super) { | ||
if (node.classList.contains('Foo')) { | ||
return 12; | ||
} else { | ||
/* | ||
* `__super` can be called to call the original `nodeLength` function on the | ||
* given node. | ||
*/ | ||
return __super(); | ||
} | ||
} | ||
``` | ||
Curse still gets a little confused when it sees certain types of HTML elements | ||
@@ -51,0 +71,0 @@ in a contenteditable. If you run across something, please [open an |
@@ -20,6 +20,5 @@ /** | ||
export default class Curse { | ||
constructor(element, { lineBreak, nodeLengths } = {}) { | ||
this.lineBreak = lineBreak; | ||
this.nodeLengths = nodeLengths || {}; | ||
constructor(element, { nodeLengthFn } = {}) { | ||
this.element = element; | ||
this.nodeLengthFn = nodeLengthFn; | ||
this.reset(); | ||
@@ -130,2 +129,3 @@ } | ||
if (!setStart && start <= idx + nodeLength) { | ||
this.indexOfNode(node); | ||
if (isText) { | ||
@@ -230,18 +230,18 @@ range.setStart(node, start - idx); | ||
* @param {Node} node a Node, typically a Text or HTMLElement node | ||
* @param {Bool} ignoreNodeLengthFn ignore the custom nodeLengthFn | ||
* @return {Number} the length of the node, as text | ||
*/ | ||
nodeLength(node) { | ||
let charNodes = ['BR', 'HR', 'IMG']; | ||
let previousSibling; | ||
nodeLength(node, ignoreNodeLengthFn = false) { | ||
if (this.nodeLengthFn && !ignoreNodeLengthFn) { | ||
let nodeLength = this.nodeLength.bind(this); | ||
if (this.lineBreak) { | ||
previousSibling = node.previousElementSibling; | ||
return this.nodeLengthFn(node, function __super() { | ||
return nodeLength(node, true); | ||
}); | ||
} | ||
if (previousSibling && previousSibling.classList.contains(this.lineBreak)) { | ||
return 1; | ||
} else if (node.nodeName === '#text') { | ||
let charNodes = ['BR', 'HR', 'IMG']; | ||
if (node.nodeName === '#text') { | ||
return node.data.length; | ||
} else if (this.nodeLengths[node.nodeName]) { | ||
return this.nodeLengths[node.nodeName]; | ||
} else if (charNodes.indexOf(node.nodeName) > -1) { | ||
@@ -248,0 +248,0 @@ return 1; |
@@ -42,2 +42,10 @@ describe('Curse', function() { | ||
}); | ||
it('can accept a custom node length function', function() { | ||
curse = new Curse($e, { nodeLengthFn: function nodeLength(/* node, __super */) { | ||
return 'CUSTOM'; | ||
} }); | ||
curse.nodeLength($e.firstChild).should.equal('CUSTOM'); | ||
}); | ||
}); | ||
@@ -44,0 +52,0 @@ |
38033
1024
97