Last active
December 20, 2015 04:18
-
-
Save termi/6069645 to your computer and use it in GitHub Desktop.
Find Microdata properties
Note: Without itemref support
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
function findMicrodataProperties(itemScopeNode, itemPropName) { | |
if( !itemScopeNode.hasAttribute("itemscope") ) { | |
return[]; | |
} | |
var pending = [] | |
, tmp | |
, currentNode | |
, k = -1 | |
, el | |
, result = [] | |
; | |
itemPropName = " " + itemPropName + " "; | |
while( currentNode = itemScopeNode.childNodes[++k] ) { | |
if( currentNode.nodeType === 1 ) { | |
pending.push(currentNode); | |
} | |
} | |
while( currentNode = pending.pop() ) { | |
if( tmp = currentNode.getAttribute("itemprop") ) { | |
if( (" " + tmp + " ").indexOf(itemPropName) !== -1 ) { | |
result.push(currentNode); | |
} | |
} | |
if( !currentNode.hasAttribute("itemscope") ) { | |
// Push all the child elements of current onto pending, in tree order | |
// (so the first child of current will be the next element to be | |
// popped from pending). | |
k = currentNode.childNodes.length; | |
while( el = currentNode.childNodes[--k] ) { | |
if( el.nodeType === 1 ) { | |
pending.push(el); | |
} | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment