bti-dom-utils
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -32,3 +32,4 @@ /** @module dom-utils */ | ||
set: function(n) { | ||
if (typeof n === "boolean") n = n | 0; | ||
if (n == null) n = 0; | ||
if (typeof n === "boolean") n = n ? 1 : 0; | ||
if (typeof n !== "number" || isNaN(n) || n < 0) { | ||
@@ -112,3 +113,3 @@ throw new Error("Expecting a non-negative integer for the index."); | ||
var count, reverse, node, index, curnode, len; | ||
count = 0; | ||
@@ -123,3 +124,3 @@ reverse = offset < 0; | ||
// we move the count to maintain index | ||
count = reverse ? index : Math.max(getTextValue(node).length - index, 0); | ||
count = reverse ? index : Math.max(getTextContent(node).length - index, 0); | ||
@@ -148,3 +149,3 @@ // if the count exceeds our max, we've hit the exact position | ||
if (~[ Node.ELEMENT_NODE, Node.TEXT_NODE ].indexOf(curnode.nodeType)) { | ||
len = getTextValue(curnode).length; | ||
len = getTextContent(curnode).length; | ||
@@ -328,13 +329,26 @@ // check if the length exceeds | ||
/** | ||
* Gets the text value of a node. Basically a concatenation of all text node values. | ||
* Gets the text content of a node and its descendants. This is the text content that is visible to a user viewing the HTML from browser. Hidden nodes, such as comments, are not included in the output. | ||
* @function | ||
* @param {Node} node - The node to get the text value of. | ||
* @param {Node} node - The node to get the text content of. | ||
* @returns {string} | ||
*/ | ||
var getTextValue = | ||
exports.getTextValue = function(node) { | ||
var getTextContent = | ||
exports.getTextContent = function(node) { | ||
if (Array.isArray(node)) return node.map(getTextContent).join(""); | ||
switch(node.nodeType) { | ||
case Node.ELEMENT_NODE: return node.innerHTML || node.textContent || ""; | ||
case Node.TEXT_NODE: return node.nodeValue || ""; | ||
default: return ""; | ||
case Node.DOCUMENT_NODE: | ||
case Node.DOCUMENT_FRAGMENT_NODE: | ||
return getTextContent(Array.prototype.slice.call(node.childNodes, 0)); | ||
case Node.ELEMENT_NODE: | ||
if (typeof node.innerText === "string") return node.innerText; // webkit | ||
if (typeof node.textContent === "string") return node.textContent; // firefox | ||
return getTextContent(Array.prototype.slice.call(node.childNodes, 0));// other | ||
case Node.TEXT_NODE: | ||
return node.nodeValue || ""; | ||
default: | ||
return ""; | ||
} | ||
@@ -419,9 +433,11 @@ } | ||
if (node.nodeType === Node.TEXT_NODE) { | ||
var text = node.nodeValue; | ||
var text = node.nodeValue, | ||
isStart = matches(node, start.node), | ||
isEnd = matches(node, end.node); | ||
if (matches(node, start.node)) { | ||
if (isStart && isEnd) { | ||
text = text.substring(start.index, end.index); | ||
} else if (isStart) { | ||
text = text.substr(start.index); | ||
} | ||
if (matches(node, end.node)) { | ||
} else if (isEnd) { | ||
text = text.substr(0, end.index); | ||
@@ -428,0 +444,0 @@ } |
{ | ||
"name": "bti-dom-utils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A semi-random collection of useful DOM tools.", | ||
@@ -5,0 +5,0 @@ "author": "Beneath the Ink <info@beneaththeink.com>", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
109272
960