Skip to content

Instantly share code, notes, and snippets.

@omarstreak
Created February 1, 2016 05:40
Show Gist options
  • Save omarstreak/8678510554b0179a7aba to your computer and use it in GitHub Desktop.
Save omarstreak/8678510554b0179a7aba to your computer and use it in GitHub Desktop.
Get unquoted text
function extract(mv){
//NodeIterators are really cool: https://developer.mozilla.org/en-US/docs/Web/API/NodeIterator
var nodeIterator = document.createNodeIterator(
mv.getBodyElement(),
NodeFilter.SHOW_ELEMENT,
{
acceptNode: function(node){
//this is the main function where the interesting code occurs
//because node iterator is a recursive tree walk you'll see every node below the body element
//this includes nodes that contain both the html we want, and html we don't want
//the strategy I took here is to only accept nodes that don't contain the quoted area
//and I do that by checking if any of the descendants are in the quoted area
return containsNoElementsInQuotedArea(node, mv)
}
}
);
var includedNodes = [];
var result = '';
var node;
while(node = nodeIterator.nextNode()){
//because nodeIterator is recursive we're going to be seeing nodes that have already
//been added, because an ancestor has been added to the result
//so to make sure we don't readd the same html multiple times, we need to check if the node
//has already been added through the ancestor
if(doesNotAlreadyContainNode(includedNodes, node)){
result += node.outerHTML;
includedNodes.push(node);
}
}
return result;
}
//accepts a node, and the messageView, does a node walk to see if any of this node's
//descendants are in the quoted area, and if they are, then we return false
//otherwise if this node does not contain the quoted area return true
function containsNoElementsInQuotedArea(node, mv){
var nodeIterator =
document.createNodeIterator(
node,
NodeFilter.SHOW_ELEMENT,
{
acceptNode: function(childNode){
return mv.isElementInQuotedArea(childNode);
}
}
);
//if we ever see a node, that means it's in the quoted area and return false
while(nodeIterator.nextNode()){
return false;
}
//we didn't see any nodes in the quoted area, so we're good!
return true;
}
//iterates through includedNodes, and returns true if candidate
//node is not a decendent of any
function doesNotAlreadyContainNode(includedNodes, node){
for(var ii=0; ii<includedNodes.length; ii++){
if(includedNodes[ii].contains(node)){
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment