-
Notifications
You must be signed in to change notification settings - Fork 11
/
test.js
134 lines (101 loc) · 3.15 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var libraries = {}
function addLibrary(name, callback) {
libraries[name] = callback;
}
function runTests() {
results = {};
for (key in libraries) {
var testFunction = libraries[key];
results[key] = getResults(testFunction);
}
drawResults(results);
}
function drawResults(results) {
var wrapper = document.querySelector('#results');
wrapper.innerHTML = '';
var output = document.createDocumentFragment()
var resultsList = Object.keys(results).map(function (key) {
var result = results[key]
result.key = key
return result
})
// priority: Valid (v), Longest (^), Duration (^)
.sort(function(curr, next) {
return next.validSelectors.length - curr.validSelectors.length ||
curr.longestSelector.length - next.longestSelector.length ||
curr.duration - next.duration
})
resultsList.forEach(function(data) {
var row = output.appendChild(document.createElement('tr'));
addCell(row, data.key);
addCell(row, data.validSelectors.length);
addCell(row, data.invalidSelectors.length);
addCell(row, data.notFoundSelectors);
addCell(row, data.nonUniqueSelectors.length);
addCell(row, data.nonMatchingSelectors.length);
addCell(row, "(" + data.longestSelector.length + ") " + data.longestSelector);
addCell(row, data.duration + "ms");
})
wrapper.appendChild(output);
console.log(resultsList);
}
function hasInvalidSelectors (data) {
return data.invalidSelectors.length || data.notFoundSelectors.length ||
data.nonUniqueSelectors.length || data.nonMatchingSelectors.length
}
function addCell(row, content) {
var cell = row.appendChild(document.createElement('td'));
cell.appendChild(document.createTextNode(content));
return cell;
}
function getResults(testFunction) {
var elements = document.querySelector('#wrap').querySelectorAll('*');
var result = {
duration: -1,
validSelectors: [],
invalidSelectors: [],
nonUniqueSelectors: [],
nonMatchingSelectors: [],
notFoundSelectors: 0,
longestSelector: ''
};
var outputs = [];
var timeStart = (new Date).getTime();
for (var i = 0, j = elements.length; i < j; i++) {
var element = elements[i];
var selector = testFunction(element);
outputs.push({
element: element,
selector: selector
});
}
var timeEnd = (new Date).getTime();
result.duration = timeEnd - timeStart
for (i = 0, j = outputs.length; i < j; i++) {
var selector = outputs[i].selector;
var element = outputs[i].element;
if (selector) {
var foundElements = []
try {
foundElements = document.querySelectorAll(selector);
} catch (e) {
result.invalidSelectors.push(selector);
}
if (foundElements.length > 1) {
result.nonUniqueSelectors.push(selector);
} else {
if (foundElements[0] === element) {
result.validSelectors.push(selector);
} else {
result.nonMatchingSelectors.push(selector);
}
}
if (selector.length > result.longestSelector.length) {
result.longestSelector = selector;
}
} else {
result.notFoundSelectors++;
}
}
return result;
}