cq-prolyfill.js
function buildElementsTree(contexts) {

	contexts = contexts || [document];

	var queriesArray = Object.keys(queries).map(function(key) {
		return queries[key];
	});

	var selector = queriesArray.map(function(query) {
		return query._selector;
	}).join(',');

	var elements = [];
	contexts.forEach(function(context) {
		for (var node = context.parentNode; node; node = node.parentNode) {
			// Skip nested contexts
			if (contexts.indexOf(node) !== -1) {
				return;
			}
		}
		if (context !== document && elementMatchesSelector(context, selector)) {
			elements.push(context);
		}
		elements.push.apply(elements, arrayFrom(context.querySelectorAll(selector)));
	});

	var tree = [];
	var treeCache = createCacheMap();

	elements.forEach(function(element) {

		if (element === documentElement) {
			return;
		}

		var treeNode = {
			_element: element,
			_children: [],
			_queries: [],
			_changes: [],
			_done: false,
		};

		var children = tree;
		for (var node = element.parentNode; node; node = node.parentNode) {
			if (treeCache.get(node)) {
				children = treeCache.get(node)._children;
				break;
			}
		}

		treeCache.set(element, treeNode);

		children.push(treeNode);

		queriesArray.forEach(function(query) {
			if (elementMatchesSelector(element, query._selector)) {
				treeNode._queries.push(query);
			}
		});

	});

	return tree;

}
function buildElementsTree(contexts) {

	contexts = contexts || [document];

	var queriesArray = Object.keys(queries).map(function(key) {
		return queries[key];
	});

	var selector = queriesArray.map(function(query) {
		return query._selector;
	}).join(',');

	var elements = [];
	contexts.forEach(function(context) {
		for (var node = context.parentNode; node; node = node.parentNode) {
			// Skip nested contexts
			if (contexts.indexOf(node) !== -1) {
				return;
			}
		}
		if (context !== document && elementMatchesSelector(context, selector)) {
			elements.push(context);
		}
		elements.push.apply(elements, arrayFrom(context.querySelectorAll(selector)));
	});

	var tree = [];
	var treeCache = createCacheMap();

	elements.forEach(function(element) {

		if (element === documentElement) {
			return;
		}

		var treeNode = {
			_element: element,
			_children: [],
			_queries: [],
			_changes: [],
			_done: false,
		};

		var children = tree;
		for (var node = element.parentNode; node; node = node.parentNode) {
			if (treeCache.get(node)) {
				children = treeCache.get(node)._children;
				break;
			}
		}

		treeCache.set(element, treeNode);

		children.push(treeNode);

		queriesArray.forEach(function(query) {
			if (elementMatchesSelector(element, query._selector)) {
				treeNode._queries.push(query);
			}
		});

	});

	return tree;

}
function buildElementsTree ( contexts ) { contexts = contexts || [ document ] ; var queriesArray = Object . keys ( queries ) . map ( function ( key ) { return queries [ key ] ; } ) ; var selector = queriesArray . map ( function ( query ) { return query . _selector ; } ) . join ( ',' ) ; var elements = [ ] ; contexts . forEach ( function ( context ) { for ( var node = context . parentNode ; node ; node = node . parentNode ) { // Skip nested contexts if ( contexts . indexOf ( node ) !== - 1 ) { return ; } } if ( context !== document && elementMatchesSelector ( context , selector ) ) { elements . push ( context ) ; } elements . push . apply ( elements , arrayFrom ( context . querySelectorAll ( selector ) ) ) ; } ) ; var tree = [ ] ; var treeCache = createCacheMap ( ) ; elements . forEach ( function ( element ) { if ( element === documentElement ) { return ; } var treeNode = { _element : element , _children : [ ] , _queries : [ ] , _changes : [ ] , _done : false , } ; var children = tree ; for ( var node = element . parentNode ; node ; node = node . parentNode ) { if ( treeCache . get ( node ) ) { children = treeCache . get ( node ) . _children ; break ; } } treeCache . set ( element , treeNode ) ; children . push ( treeNode ) ; queriesArray . forEach ( function ( query ) { if ( elementMatchesSelector ( element , query . _selector ) ) { treeNode . _queries . push ( query ) ; } } ) ; } ) ; return tree ; }
Build tree of all query elements

@param  {Array.<Element>} contexts
@return {Array.<{_element: Element, _children: array, _queries: array, _changes: array, _done: boolean}>}
Build tree of all query elements