Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tyler/386795 to your computer and use it in GitHub Desktop.
Save tyler/386795 to your computer and use it in GitHub Desktop.
Search.prototype._findNthWord = function(node, n, current_count) {
if(current_count === undefined)
current_count = { i: 0 };
var children = node.childNodes;
for(var i = 0; i < children.length; i++) {
var child = children[i];
switch(child.nodeType) {
case 1:
var result = this._findNthTextNode(child, n, current_count);
if(result)
return result;
break;
case 3:
var text = child.wholeText.strip().replace(/\s+/g, ' ');
if(text.length > 0) {
var words = text.split(' ');
if(current_count.i + words.length > n) {
return { node: node, startsAt: current_count };
} else {
current_count.i += words.length;
}
}
}
}
return null;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment