/**
 * Analyzes DOM queries done via native DOM methods & jQuery
 */
exports.version = '0.4';

exports.module = function(phantomas) {
        phantomas.setMetric('DOMqueries');
        phantomas.setMetric('DOMqueriesById');
        phantomas.setMetric('DOMqueriesByClassName');
        phantomas.setMetric('DOMinserts');

	// fake native DOM functions
	phantomas.once('init', function() {
		phantomas.evaluate(function() {
			(function(phantomas) {
				// count DOM queries by either ID or class name
				var querySpy = function(query) {
					phantomas.log('DOM query: "' + query + '"');
					phantomas.incrMetric('DOMqueries');
				};

				phantomas.spy(window.document, 'getElementById', function(id) {
					phantomas.incrMetric('DOMqueriesById');
					querySpy('#' + id);
				});

				phantomas.spy(window.document, 'getElementsByClassName', function(className) {
					phantomas.incrMetric('DOMqueriesByClassName');
					querySpy('.' + className);
				});

				// count DOM inserts
				var appendSpy = function(child) {
					var hasParent = typeof this.parentNode !== 'undefined';

					// ignore appending to the node that's not yet added to DOM tree
					if (!hasParent) {
						return;
					}

					phantomas.incrMetric('DOMinserts');
					phantomas.log('DOM insert: node "' + phantomas.getDOMPath(child) + '" added to "' + phantomas.getDOMPath(this) + '"');
				};

				phantomas.spy(Node.prototype, 'appendChild', appendSpy);
				phantomas.spy(Node.prototype, 'insertBefore', appendSpy);
			})(window.__phantomas);
		});
	});
};

@@ -52,17 +52,8 @@
 hods
- & jQuery
 %0A */
@@ -258,24 +258,137 @@
 lassName');%0A
+        phantomas.setMetric('DOMqueriesByTagName');%0A        phantomas.setMetric('DOMqueriesByQuerySelectorAll');%0A
         phan
@@ -587,51 +587,169 @@
 r ID
- or class name%0A%09%09%09%09var querySpy = function(
+, tag name, class name and selector query%0A%09%09%09%09// @see https://dvcs.w3.org/hg/domcore/raw-file/tip/Overview.html#dom-document-doctype%0A%09%09%09%09function querySpy(type, 
 quer
@@ -783,16 +783,32 @@
 M query:
+ by ' + type + '
  %22' + qu
@@ -857,33 +857,32 @@
 queries');%0A%09%09%09%09%7D
-;
 %0A%0A%09%09%09%09phantomas.
@@ -881,39 +881,42 @@
 antomas.spy(
-window.document
+Document.prototype
 , 'getElemen
@@ -997,16 +997,22 @@
 uerySpy(
+'id', 
 '#' + id
@@ -1045,23 +1045,26 @@
 spy(
-window.document
+Document.prototype
 , 'g
@@ -1175,16 +1175,25 @@
 uerySpy(
+'class', 
 '.' + cl
@@ -1219,57 +1219,552 @@
 %09%09%09%09
-// count DOM inserts%0A%09%09%09%09var appendSpy = function
+phantomas.spy(Document.prototype, 'getElementsByTagName', function(tagName) %7B%0A%09%09%09%09%09phantomas.incrMetric('getElementsByTagName');%0A%09%09%09%09%09querySpy('tag name', tagName);%0A%09%09%09%09%7D);%0A%0A%09%09%09%09// selector queries%0A%09%09%09%09function selectorQuerySpy(selector) %7B%0A%09%09%09%09%09phantomas.incrMetric('DOMqueriesByQuerySelectorAll');%0A%09%09%09%09%09querySpy('selector', selector);%0A%09%09%09%09%7D%0A%0A%09%09%09%09phantomas.spy(Document.prototype, 'querySelectorAll', selectorQuerySpy);%0A%09%09%09%09phantomas.spy(Element.prototype, 'querySelectorAll', selectorQuerySpy);%0A%0A%09%09%09%09// count DOM inserts%0A%09%09%09%09function appendSpy
 (chi
@@ -2116,17 +2116,16 @@
 );%0A%09%09%09%09%7D
-;
 %0A%0A%09%09%09%09ph

add metrics