Created
May 2, 2010 00:43
-
-
Save tyler/386795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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