瀏覽代碼

Add a 'jQuery - find' wrapper on jQuery search by ID

Gaël Métais 10 年之前
父節點
當前提交
d3444e9aa0
共有 1 個文件被更改,包括 37 次插入10 次删除
  1. 37 10
      lib/tools/phantomas/custom_modules/modules/jQYLT/jQYLT.js

+ 37 - 10
lib/tools/phantomas/custom_modules/modules/jQYLT/jQYLT.js

@@ -198,8 +198,7 @@ exports.module = function(phantomas) {
                             callDetails: {
                             callDetails: {
                                 context: {
                                 context: {
                                     length: this.length,
                                     length: this.length,
-                                    firstElementPath: phantomas.getDOMPath(context),
-                                    toto: this
+                                    firstElementPath: phantomas.getDOMPath(context)
                                 },
                                 },
                                 arguments: [selector]
                                 arguments: [selector]
                             },
                             },
@@ -213,6 +212,40 @@ exports.module = function(phantomas) {
                         phantomas.leaveContext(moreData);
                         phantomas.leaveContext(moreData);
                     }) || phantomas.log('jQuery: can not measure jQuerySizzleCalls (jQuery used on the page is too old)!');
                     }) || phantomas.log('jQuery: can not measure jQuerySizzleCalls (jQuery used on the page is too old)!');
 
 
+
+                    phantomas.spy(jQuery.fn, 'init', function(selector, context) {
+                        if (typeof selector === 'string' && /^#([\w\-]*)$/.exec(selector) !== null && !context) {
+
+                            phantomas.enterContext({
+                                type: 'jQuery - find',
+                                callDetails: {
+                                    context: {
+                                        length: 1,
+                                        firstElementPath: '#document'
+                                    },
+                                    arguments: [selector]
+                                },
+                                backtrace: phantomas.getBacktrace()
+                            });
+
+                        }
+
+                    }, function(result) {
+                        var data = phantomas.getContextData();
+
+                        if (data.type === 'jQuery - find' &&
+                                data.callDetails.context.firstElementPath === '#document' &&
+                                data.callDetails.arguments.length === 1 &&
+                                /^#([\w\-]*)$/.exec(data.callDetails.arguments[0]) !== null) {
+
+                            var moreData = {
+                                resultsNumber : (result && result.length) ? result.length : 0
+                            };
+                            phantomas.leaveContext(moreData);
+                        }
+                    });
+
+
                     /*if (!jQuery.event) {
                     /*if (!jQuery.event) {
                         phantomas.spy(jQuery.event, 'trigger', function(ev, data, elem) {
                         phantomas.spy(jQuery.event, 'trigger', function(ev, data, elem) {
                             var path = phantomas.getDOMPath(elem),
                             var path = phantomas.getDOMPath(elem),
@@ -243,7 +276,6 @@ exports.module = function(phantomas) {
 
 
                     // Add spys on many jQuery functions
                     // Add spys on many jQuery functions
                     jQueryFunctions.forEach(function(functionName) {
                     jQueryFunctions.forEach(function(functionName) {
-                        var capitalizedName = functionName.substring(0,1).toUpperCase() + functionName.substring(1);
                         
                         
                         phantomas.spy(jQuery.fn, functionName, function(args) {
                         phantomas.spy(jQuery.fn, functionName, function(args) {
 
 
@@ -303,14 +335,9 @@ exports.module = function(phantomas) {
                                 args[index] = arg;
                                 args[index] = arg;
                             });
                             });
 
 
-                            // Differentiate between $('.foo') and $(...).find('.foo')
-                            var functionNameDisplayed = functionName;
-                            if (functionName === 'find' && this.prevObject === undefined) {
-                                functionNameDisplayed = '$';
-                            }
 
 
                             phantomas.enterContext({
                             phantomas.enterContext({
-                                type: 'jQuery - ' + functionNameDisplayed,
+                                type: 'jQuery - ' + functionName,
                                 callDetails: {
                                 callDetails: {
                                     context: {
                                     context: {
                                         length: this.length,
                                         length: this.length,
@@ -323,7 +350,7 @@ exports.module = function(phantomas) {
 
 
                         }, function(result) {
                         }, function(result) {
                             phantomas.leaveContext();
                             phantomas.leaveContext();
-                        }) || phantomas.log('jQuery: can not track jQuery - ' + capitalizedName + ' (this version of jQuery doesn\'t support it)');
+                        }) || phantomas.log('jQuery: can not track jQuery - ' + functionName + ' (this version of jQuery doesn\'t support it)');
                     });
                     });
                 });
                 });
             })(window.__phantomas);
             })(window.__phantomas);