-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
348 lines (324 loc) · 8.74 KB
/
util.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
function findFirstNotEmpty () {
let arg = arguments
var i = -1,
temp,
length = arg.length
while (length--) {
temp = arg[++i]
if (temp !== undefined && temp !== null) {
break
}
}
return temp
}
function getAttrFormStr (str) {
str = str
.replace(/('|")([^'|"]*)\1/gm, function (a, b, c) {
return a.replace(/[\r\n\s]/gm, '$blank$')
})
.replace(/[\r\n\s]/gm, '')
let isObj = /{.*}/.test(str)
let isArr = /\[.*\]/.test(str)
function getAttrInObj (str) {
return str.replace(/{.*}/gm, function (objstr) {
let grouplist = objstr.replace(/[{}'"]/gm, '').split(',')
grouplist.forEach(e => {
if (e) {
let value = /(.*):/.exec(e)
attrList.push(value ? value[1] : e)
}
})
return ''
})
}
function getAttrInArr (str) {
let grouplist = str.replace(/[\[\]'"]/gm, '')
if (isObj) {
grouplist = grouplist.replace(/{.*}/gm, getAttrInObj)
}
grouplist.split(',').forEach(e => {
if (e) {
if (e.includes('?')) {
let value = /\?(.*)/.exec(e)
attrList = attrList.concat(value[1].split(':'))
} else {
attrList.push(e)
}
}
})
}
let attrList = []
if (isObj || isArr) {
var complex = str.replace(/[\r\n\s]/gm, '')
if (isObj) {
complex = getAttrInObj(complex)
}
if (isArr) {
getAttrInArr(complex)
}
}
var reg = /('|")([^'|"]*)\1/gm
let attr = {}
while ((attr = reg.exec(str))) {
attrList = attrList.concat(attr[2].split('$blank$'))
}
return attrList
}
function validArr (arr) {
return arr && Array.isArray(arr) && arr.length > 0
}
function findEleIndexInParent (ele, childrens) {
if (!ele) return
let parent
if ((parent = ele.parent)) {
let i = 0,
e
while ((e = childrens[i++])) {
if (e === ele) {
break
}
}
return i - 1
}
}
function findMutualArr (children, blockScope) {
if (blockScope && /if/.test(blockScope)) {
let matchIfReg = []
getBlockIf(blockScope, (ifId, blockId, isElse) => {
matchIfReg.push(new RegExp(`(?<!>>${blockId})if${ifId}`))
})
children = children.filter(e => {
let matchRes = true
for (let i = 0; i < matchIfReg.length; i++) {
const h = matchIfReg[i]
if (h.test(e.blockScope)) {
matchRes = false
break
}
}
return matchRes
})
}
return children
}
function getChildMutual (ele, next) {
let children = ele.parent.childrens.filter(e => e.type === 1)
let blockScope = ele.blockScope
children = findMutualArr(children, blockScope, false)
let currentIndex = findEleIndexInParent(ele, children)
if (ele.ifId) {
var operator = next ? 1 : -1
var tempSibling,
delIndex = currentIndex
while ((tempSibling = children[(delIndex += operator)])) {
if (tempSibling.ifId !== ele.ifId) {
break
}
}
delIndex -= operator
next
? children.splice(currentIndex, delIndex - currentIndex)
: children.splice(delIndex, currentIndex - delIndex)
}
return children
}
function findSibling (ele, next = true) {
let children = getChildMutual(ele, next)
let eleIndex = findEleIndexInParent(ele, children)
var operator = next ? 1 : -1
if (typeof eleIndex === 'number') {
function findIfConditionsCollention (child) {
let index = findEleIndexInParent(ele, child)
let collectionArr = []
let { ifId, forId } = child[(index += operator)]
if (!ifId) {
collectionArr = [child[index]]
} else {
while (ifId) {
let collection = child.filter(
e => e.ifId === ifId && e.forId === forId
)
let existElse = collection.find(e => e.else)
collectionArr = collectionArr.concat(collection)
if (existElse) {
break
} else {
index = index + collection.length * operator
let nextEle = child[index]
ifId = nextEle && nextEle.ifId
forId = nextEle && nextEle.forId
if (!ifId) {
collectionArr.push(child[index])
}
}
}
}
return collectionArr
}
let currentIndex = eleIndex
let ifChild = parseIfInBlock(children, ele)
let maybeSibele = new Map()
ifChild.forEach(e => {
let collectionArr = findIfConditionsCollention(e)
collectionArr.forEach(i => maybeSibele.set(i, true))
})
return maybeSibele.keys()
}
return []
}
function findSiblingAll (ele, next = true) {
let childrens = getChildMutual(ele, next)
let currentIndex = findEleIndexInParent(ele, childrens)
var operator = next ? 1 : -1
if (typeof currentIndex === 'number') {
if (next) {
return childrens.slice(currentIndex + 1, childrens.length)
} else {
return childrens.slice(0, currentIndex)
}
}
return []
}
function parseIfInBlock (child, ele) {
let ifCondition = {}
let muaIf = {}
getBlockIf(ele.blockScope, (ifId, blockId, isElse) => {
muaIf[ifId] = blockId
})
child.forEach(e => {
getBlockIf(e.blockScope, (ifId, blockId, isElse) => {
if (!muaIf[ifId]) {
if (!ifCondition[ifId]) {
ifCondition[ifId] = {}
}
ifCondition[ifId][blockId] = isElse
}
})
})
let regIfArr = []
Object.keys(ifCondition).forEach(e => {
let regIf = [],
elseExist = false
Object.keys(ifCondition[e]).forEach(i => {
let value = ifCondition[e][i]
if (value) {
elseExist = true
}
// ifid和blockid完全匹配或者不包含ifId
regIf.push(`((?<=>>${i})if${e})|(^((?!if${e}).)*$)`)
})
if (!elseExist) {
// 不包含ifid>> 且以ifid结尾
// regIf.push(`(^((?!if${e}>>).)*$)((?<!if${e})$)`)
regIf.push(`^((?!if${e}).)*$`)
}
regIfArr.push(regIf)
})
let ifCombine = combine_arr(regIfArr)
let ifChild = ifCombine.map(e =>
child.filter(ele => !ele.blockScope || e.test(ele.blockScope))
)
return ifChild
}
function combine_arr (arr) {
var sarr = [[]]
for (var i = 0; i < arr.length; i++) {
var tarr = []
for (var j = 0; j < sarr.length; j++)
for (var k = 0; k < arr[i].length; k++)
tarr.push(sarr[j].concat(`(?=.*(${arr[i][k]}))`).toString())
sarr = tarr
}
return sarr.map(e => new RegExp(e))
}
function getBlockIf (scope, cb) {
if (scope) {
let matchIf = scope.match(/\d*if\d*(!!)?/g)
Array.isArray(matchIf) &&
matchIf.map(e => {
var attr = e.split('if')
var ifId = attr[1].replace(/!!/, '').toString()
cb(ifId, attr[0].toString(), attr[1].endsWith('!!'))
})
}
}
function getVueConfig(url) {
let pkgdir = require('pkg-dir')
var pkg = pkgdir.sync(url)
const globby = require('globby')
var slash = require('slash')
const vueConfig = globby.sync(['vue.config.js'], {
cwd: slash(pkg || process.cwd()),
absolute: true,
})
var vueConfigData = []
if (validArr(vueConfig)) {
vueConfig.forEach(e=>{
vueConfigData.push(require(e))
})
}
return vueConfigData
}
function getIgnoreConfig(url) {
let pkgdir = require('pkg-dir')
var pkg = pkgdir.sync(url)
const globby = require('globby')
var slash = require('slash')
const package = globby.sync(['package.json'], {
cwd: slash(pkg || process.cwd()),
absolute: true,
})
if (validArr(package)) {
let ignoreConfig = []
package.forEach(packageName=>{
let pack = require(packageName);
if (pack.ignoreCss) {
ignoreConfig = ignoreConfig.concat(pack.ignoreCss)
}
})
return ignoreConfig
}
}
function getType(ele) {
return Object.prototype.toString.call(ele)
}
function validIsIgnoreByConfing(nodes,ignores){
let node,i=0
while ((node = nodes[i++])) {
try {
for (let i = 0; i < ignores.length; i++) {
const ignore = ignores[i];
let type = getType(ignore)
if (type === '[object String]') {
if (ignore === node.value) return true
}else if(type === '[object Object]' && ignore.reg){
if (new RegExp(ignore.reg,ignore.attr) .test(node.value)) return true
}
}
} catch (error) {
}
}
}
function validIsIgnoreByComment(node) {
let result = false
node.each(e=>{
if(e.type==='comment'){
let text = e.text
if (text.includes('ignorecss')) {
result = true
}
}
})
return result
}
module.exports = {
findFirstNotEmpty,
getAttrFormStr,
validArr,
findSibling,
findSiblingAll,
getIgnoreConfig,
validIsIgnoreByConfing,
validIsIgnoreByComment,
getVueConfig
}