123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- var OffendersHelpers = function() {
-
- this.domPathToArray = function(str) {
- return str.split(/\s?>\s?/);
- };
- this.listOfDomArraysToTree = function(listOfDomArrays) {
- var result = {};
- function recursiveTreeBuilder(tree, domArray) {
- if (domArray.length > 0) {
- var currentDomElement = domArray.shift();
- if (tree === null) {
- tree = {};
- }
- tree[currentDomElement] = recursiveTreeBuilder(tree[currentDomElement] || null, domArray);
- return tree;
- } else if (tree === null) {
- return 1;
- } else {
- return tree + 1;
- }
- }
- listOfDomArrays.forEach(function(domArray) {
- result = recursiveTreeBuilder(result, domArray);
- });
- return result;
- };
- this.domPathToDomElementObj = function(domPath) {
- if (typeof domPath === 'boolean') {
- return {
- // Not a normal element path
- type: 'notAnElement',
- element: domPath
- };
- }
- var domArray = this.domPathToArray(domPath);
- var domTree = this.listOfDomArraysToTree([this.domPathToArray(domPath)]);
- if (domArray[0] === 'html') {
- return {
- type: 'html'
- };
- }
- if (domArray[0] === 'body') {
- if (domArray.length === 1) {
- return {
- type: 'body'
- };
- } else {
- return {
- type: 'domElement',
- element: domArray[domArray.length - 1],
- tree: domTree
- };
- }
- }
- if (domArray[0] === 'head') {
- return {
- type: 'head'
- };
- }
- if (domArray[0] === '#document') {
- return {
- type: 'document'
- };
- }
- if (domArray[0] === 'window') {
- return {
- type: 'window'
- };
- }
- if (domArray[0] === 'DocumentFragment') {
- if (domArray.length === 1) {
- return {
- type: 'fragment'
- };
- } else {
- return {
- type: 'fragmentElement',
- element: domArray[domArray.length - 1],
- tree: domTree
- };
- }
- }
-
- // Not attached element, such as just created with document.createElement()
- if (domArray.length === 1) {
- return {
- type: 'createdElement',
- element: domPath
- };
- } else {
- return {
- type: 'createdElement',
- element: domArray[domArray.length - 1],
- tree: domTree
- };
- }
- };
- this.backtraceToArray = function(str) {
- var traceArray = str.split(/ \/ /);
- if (traceArray) {
- var results = [];
- var parts = null;
- var obj;
- for (var i=0 ; i<traceArray.length ; i++) {
- // Handle the new PhantomJS 2.x syntax
- parts = /^\s*at( (\w+))? \(?([^ ]+):(\d+):(\d+)\)?$$/.exec(traceArray[i]);
- if (parts) {
- obj = {
- file: parts[3],
- line: parseInt(parts[4], 10),
- column: parseInt(parts[5], 10)
- };
- if (parts[2]) {
- obj.functionName = parts[2];
- }
- results.push(obj);
- }
- }
- return results;
- } else {
- return null;
- }
- };
- this.sortVarsLikeChromeDevTools = function(vars) {
- return vars.sort(function(a, b) {
- return (a < b) ? -1 : 1;
- });
- };
- this.urlToLink = function(url) {
- var shortUrl = (url.length > 110) ? url.substr(0, 47) + ' ... ' + url.substr(-48) : url;
- return '<a href="' + url + '" target="_blank" title="' + url + '">' + shortUrl + '</a>';
- };
- this.cssOffenderPattern = function(offender) {
- // Used to work with strings
- // As of Phantomas v2, offender is now in JSON format.
- // Let's just adapt this for now and we'll see later if we remove completely this function
- return {
- css: offender.value.message,
- file: offender.url,
- line: offender.value.position.start.line,
- column: offender.value.position.start.column
- };
- };
- this.fileWithSizePattern = function(fileWithSize) {
- var parts = /^([^ ]*) \((\d+\.\d{2}|NaN) kB\)$/.exec(fileWithSize);
- if (!parts) {
- return {
- file: fileWithSize
- };
- } else {
- return {
- file: parts[1],
- size: parseFloat(parts[2])
- };
- }
- };
- this.orderByFile = function(offenders) {
- var byFileObj = {};
- offenders.forEach(function(offender) {
- var file = offender.file || 'Inline CSS';
- delete offender.file;
- if (!byFileObj[file]) {
- byFileObj[file] = {
- url: file,
- count: 0,
- offenders: []
- };
- }
- byFileObj[file].count ++;
- byFileObj[file].offenders.push(offender);
- });
- // Transform object into array
- var byFileArray = [];
- for (var file in byFileObj) {
- byFileArray.push(byFileObj[file]);
- }
- return {byFile: byFileArray};
- };
- };
- module.exports = new OffendersHelpers();
|