Skip to content

Commit

Permalink
Merge pull request #379 from macbre/domQueries-ignore-inserts-to-docu…
Browse files Browse the repository at this point in the history
…mentfragments

Ignore DOM inserts to DocumentFragments
  • Loading branch information
macbre committed Aug 1, 2014
2 parents 3349737 + 53ebecb commit 85f3e21
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 4 additions & 0 deletions core/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@
break;
}

if (node instanceof DocumentFragment) {
entry = 'DocumentFragment';
}

// div#foo
if (node.id && node.id !== '') {
entry += '#' + node.id;
Expand Down
1 change: 1 addition & 0 deletions lib/metadata/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@
},
"DOMinserts": {
"desc": "number of DOM nodes inserts",
"offenders": true,
"unit": "number",
"module": "domQueries"
},
Expand Down
17 changes: 13 additions & 4 deletions modules/domQueries/domQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,24 @@ exports.module = function(phantomas) {
// count DOM inserts
function appendSpy(child) {
/* jshint validthis: true */
var hasParent = (typeof this.parentNode !== 'undefined');

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

var destNodePath = phantomas.getDOMPath(this),
appendedNodePath = phantomas.getDOMPath(child);

// don't count elements added to fragments as a DOM inserts (issue #350)
// DocumentFragment > div[0]
if (destNodePath.indexOf('DocumentFragment') === 0) {
return;
}

phantomas.incrMetric('DOMinserts');
phantomas.log('DOM insert: node "' + phantomas.getDOMPath(child) + '" added to "' + phantomas.getDOMPath(this) + '"');
phantomas.addOffender('DOMinserts', '"%s" appended to "%s"', appendedNodePath, destNodePath);

phantomas.log('DOM insert: node "%s" appended to "%s"', appendedNodePath, destNodePath);
}

phantomas.spy(Node.prototype, 'appendChild', appendSpy);
Expand Down

0 comments on commit 85f3e21

Please sign in to comment.