-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathoptimize.js
182 lines (162 loc) · 6.14 KB
/
optimize.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* # Optimize
*
* 1.) Improve efficiency through shorter selectors by removing redundancy
* 2.) Improve robustness through selector transformation
*/
import adapt from './adapt'
import { convertNodeList } from './utilities'
/**
* Apply different optimization techniques
*
* @param {string} selector - [description]
* @param {HTMLElement|Array.<HTMLElement>} element - [description]
* @param {Object} options - [description]
* @return {string} - [description]
*/
export default function optimize (selector, elements, options = {}) {
// convert single entry and NodeList
if (!Array.isArray(elements)) {
elements = !elements.length ? [elements] : convertNodeList(elements)
}
if (!elements.length || elements.some((element) => element.nodeType !== 1)) {
throw new Error(`Invalid input - to compare HTMLElements its necessary to provide a reference of the selected node(s)! (missing "elements")`)
}
const globalModified = adapt(elements[0], options)
// chunk parts outside of quotes (http://stackoverflow.com/a/25663729)
var path = selector.replace(/> /g, '>').split(/\s+(?=(?:(?:[^"]*"){2})*[^"]*$)/)
if (path.length < 2) {
return optimizePart('', selector, '', elements)
}
const shortened = [path.pop()]
while (path.length > 1) {
const current = path.pop()
const prePart = path.join(' ')
const postPart = shortened.join(' ')
const pattern = `${prePart} ${postPart}`
const matches = document.querySelectorAll(pattern)
if (matches.length !== elements.length) {
shortened.unshift(optimizePart(prePart, current, postPart, elements))
}
}
shortened.unshift(path[0])
path = shortened
// optimize start + end
path[0] = optimizePart('', path[0], path.slice(1).join(' '), elements)
path[path.length-1] = optimizePart(path.slice(0, -1).join(' '), path[path.length-1], '', elements)
if (globalModified) {
delete global.document
}
return path.join(' ').replace(/>/g, '> ').trim()
}
/**
* Improve a chunk of the selector
*
* @param {string} prePart - [description]
* @param {string} current - [description]
* @param {string} postPart - [description]
* @param {Array.<HTMLElement>} elements - [description]
* @return {string} - [description]
*/
function optimizePart (prePart, current, postPart, elements) {
if (prePart.length) prePart = `${prePart} `
if (postPart.length) postPart = ` ${postPart}`
// robustness: attribute without value (generalization)
if (/\[*\]/.test(current)) {
const key = current.replace(/=.*$/, ']')
var pattern = `${prePart}${key}${postPart}`
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = key
} else {
// robustness: replace specific key-value with base tag (heuristic)
const references = document.querySelectorAll(`${prePart}${key}`)
for (var i = 0, l = references.length; i < l; i++) {
const reference = references[i]
if (elements.some((element) => reference.contains(element))) {
const description = reference.tagName.toLowerCase()
var pattern = `${prePart}${description}${postPart}`
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = description
}
break
}
}
}
}
// robustness: descendant instead child (heuristic)
if (/>/.test(current)) {
const descendant = current.replace(/>/, '')
var pattern = `${prePart}${descendant}${postPart}`
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = descendant
}
}
// robustness: 'nth-of-type' instead 'nth-child' (heuristic)
if (/:nth-child/.test(current)) {
// TODO: consider complete coverage of 'nth-of-type' replacement
const type = current.replace(/nth-child/g, 'nth-of-type')
var pattern = `${prePart}${type}${postPart}`
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = type
}
}
// efficiency: combinations of classname (partial permutations)
if (/\.\S+\.\S+/.test(current)) {
var names = current.trim().split('.').slice(1)
.map((name) => `.${name}`)
.sort((curr, next) => curr.length - next.length)
while (names.length) {
const partial = current.replace(names.shift(), '').trim()
var pattern = `${prePart}${partial}${postPart}`.trim()
if (!pattern.length || pattern.charAt(0) === '>' || pattern.charAt(pattern.length-1) === '>') {
break
}
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = partial
}
}
// robustness: degrade complex classname (heuristic)
names = current && current.match(/\./g)
if (names && names.length > 2) {
const references = document.querySelectorAll(`${prePart}${current}`)
for (var i = 0, l = references.length; i < l; i++) {
const reference = references[i]
if (elements.some((element) => reference.contains(element) )) {
// TODO:
// - check using attributes + regard excludes
const description = reference.tagName.toLowerCase()
var pattern = `${prePart}${description}${postPart}`
var matches = document.querySelectorAll(pattern)
if (compareResults(matches, elements)) {
current = description
}
break
}
}
}
}
return current
}
/**
* Evaluate matches with expected elements
*
* @param {Array.<HTMLElement>} matches - [description]
* @param {Array.<HTMLElement>} elements - [description]
* @return {Boolean} - [description]
*/
function compareResults (matches, elements) {
const { length } = matches
return length === elements.length && elements.every((element) => {
for (var i = 0; i < length; i++) {
if (matches[i] === element) {
return true
}
}
return false
})
}