File size: 22,248 Bytes
4a718ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
const onecolor = one.color
const styleCheckList = Vue.ref({
    isPosition: false,
    isColor: true,
    isFontSize: true,
    isFontStyle: false,
    isFontWeight: false,
    isBackgroundColor: true,
    isBorderRadius: true,
    isBorderWidth: true,
    isOpacity: false,
    ignoreUnvisible: true
})
const maxElsLimit = Vue.ref(1500)

const sourceEls = Vue.ref([])
const targetEls = Vue.ref([])
const targetHaveAssEls = Vue.ref([])
const targetNoAssEls = Vue.ref([])

const getElements = (toPlaywright = true) => {
    let toList = [];
    const ref = document.body;
    const parentWidth = ref.offsetWidth
    const parentHeight = document.documentElement.clientHeight
    const parentScrollHeight = ref.scrollHeight
    let elements = [];
    elements = document.querySelectorAll('body *')
    toList.splice(0, toList.length);
    for (let el of elements) {
        if (['script'].includes(el.tagName.toLowerCase())) continue
        if (isUnvisible(el)) continue
        toList.push(el)
    }
    if (toList.length > maxElsLimit.value) {
        toList = toList.slice(0, maxElsLimit.value)
    }
    toList.forEach((el) => {
        let clientRect = el.getBoundingClientRect()
        let pos = {
            left: clientRect.left + scrollX,
            right: clientRect.right + scrollX,
            top: clientRect.top + scrollY,
            bottom: clientRect.bottom + scrollY,
            width: clientRect.width,
            height: clientRect.height
        }
        const isFixedOrSticky = ['fixed', 'sticky'].includes(
            getComputedStyle(el).position
        )
        if (isFixedOrSticky) {
            pos.left = clientRect.left
            pos.right = clientRect.right
            pos.top = clientRect.top
            pos.bottom = clientRect.bottom
        }
        let biasX = []
        let biasY = []
        if (pos.left < parentWidth / 2) biasX = ['left']
        else biasX = ['right']
        if (pos.left < parentWidth / 2 && pos.right > parentWidth / 2) biasX.push('mid')

        if (pos.top < parentHeight / 2) biasY = ['top']
        else if (pos.top > parentScrollHeight - parentHeight / 2) biasY = ['bottom']
        else biasY = ['scroll']
        if (pos.top < parentHeight / 2 && pos.bottom > parentHeight / 2 && isFixedOrSticky)
            biasY.push('mid')

        el.alisaInfo = {
            elInfo: {
                pos: pos,
                biasX,
                biasY
            },
            parentInfo: {
                width: parentWidth,
                height: parentHeight,
                scrollHeight: parentScrollHeight
            },
            styles: getComputedStyle(el),
            groupLeft: [],
            groupRight: [],
            groupTop: [],
            groupBottom: [],
            groupAxisWidth: [],
            groupAxisHeight: [],
            groupAll: [],
            raceList: [],
            assElement: null,
            assLayoutSimScore: 0,
            assStyleSimScore: 0
        }
    })

    toList.forEach((el) => {
        getSameGroup(el, toList, 'left')
        getSameGroup(el, toList, 'right')
        getSameGroup(el, toList, 'top')
        getSameGroup(el, toList, 'bottom')
        getSameGroup(el, toList, 'axisWidth')
        getSameGroup(el, toList, 'axisHeight')
    })
    toList.forEach((el) => {
        getSameRace(el, toList)
    })

    if (!toPlaywright) return toList;

    let formatList = [];
    toList.forEach((el, idx) => {
        el._idx = idx;
        let formatItem = {
            idx: idx,
            isAlisaObj: true,
            tagName: el.tagName,
            innerText: el.innerText ? el.innerText : false,
            alisaInfo: {
                ...el.alisaInfo
            }
        }
        formatList.push(formatItem)
    })

    formatList.forEach((el) => {
        let decodeKeys = [
            'groupLeft',
            'groupRight',
            'groupTop',
            'groupBottom',
            'groupAxisWidth',
            'groupAxisHeight',
            'groupAll',
            'raceList'
        ]
        for (let key of decodeKeys) {
            let dl = []
            for (let keyItem of el.alisaInfo[key]) {
                dl.push(keyItem._idx)
            }
            el.alisaInfo[key] = dl;
        }
    })

    return formatList;
}

const isUnvisible = (el) => {
    const style = getComputedStyle(el);
    const rect = el.getBoundingClientRect();
    const hidden =
        style.display === 'none' ||
        style.visibility === 'hidden' ||
        style.opacity === '0' || rect.width === 0 ||
        rect.height === 0;
    return hidden;
}

const getSameGroup = (el, elList, direction, offset = 5) => {
    let idx = elList.indexOf(el)
    if (idx < 0) return
    const Direction = direction.slice(0, 1).toUpperCase() + direction.slice(1)
    const isSamePos = (direction, pos1, pos2, offset) => {
        if (['left', 'top', 'right', 'bottom'].includes(direction))
            if (Math.abs(pos1[direction] - pos2[direction]) <= offset) return true
        if (direction == 'axisWidth')
            if (Math.abs(pos1.width / 2 + pos1.left - pos2.width / 2 - pos2.left) <= offset)
                return true
        if (direction == 'axisHeight')
            if (Math.abs(pos1.height / 2 + pos1.top - pos2.height / 2 - pos2.top) <= offset)
                return true
        return false
    }
    elList.forEach((element, i) => {
        if (idx == i) return
        if (
            isSamePos(
                direction,
                el.alisaInfo.elInfo.pos,
                element.alisaInfo.elInfo.pos,
                offset
            )
        ) {
            el.alisaInfo[`group${Direction}`].push(element)
            let itemIndex = el.alisaInfo.groupAll.indexOf(element)
            if (itemIndex < 0) el.alisaInfo.groupAll.push(element)
        }
    })
}

const getSameRace = (el, elList) => {
    let idx = elList.indexOf(el)
    if (idx < 0) return
    const isSameRace = (el1, el2) => {
        const groupAll = el1.alisaInfo.groupAll
        if (groupAll.indexOf(el2) < 0) return false
        if (el1.tagName != el2.tagName) return false
        return el1.className == el2.className
    }
    elList.forEach((element, i) => {
        if (idx == i) return
        if (isSameRace(el, element)) {
            el.alisaInfo.raceList.push(element)
        }
    })
}

const getAllAssociated = (sources, offset = 5, callback = null) => {
    let encodeKeys = [
        'groupLeft',
        'groupRight',
        'groupTop',
        'groupBottom',
        'groupAxisWidth',
        'groupAxisHeight',
        'groupAll',
        'raceList'
    ]
    sources.forEach(el => {
        for (let key of encodeKeys) {
            let dl = []
            for (let idx of el.alisaInfo[key]) {
                dl.push(sources[idx])
            }
            el.alisaInfo[key] = dl;
        }
    });
    sourceEls.value = sources;
    targetHaveAssEls.value = [];
    targetNoAssEls.value = [];
    targetEls.value.forEach((el) => {
        getElAssociated(el, sourceEls.value, offset, callback)
    })
}

const getLayoutSim = (sources, offset = 5, callback = null) => {
    getAllAssociated(sources, offset, callback);
    let allTargetEls = targetEls.value
    let viewedEls = []
    let count = 0
    allTargetEls.forEach((ei) => {
        if (viewedEls.indexOf(ei) < 0) {
            count += 1
            viewedEls.push(ei)
            ei.alisaInfo.raceList.forEach((ri) => {
                viewedEls.push(ri)
            })
        }
    })
    let arraysEqual = (a, b) => {
        if (!(Array.isArray(a) && Array.isArray(b))) return 'not equal'
        let status = 'equal'
        a.forEach((ai) => {
            if (ai !== 'mid' && b.indexOf(ai) < 0) {
                status = 'not equal'
                return
            }
            if (ai === 'mid' && b.indexOf(ai) < 0) {
                status = 'partial equal'
                return
            }
        })
        return status
    }
    let posDiff = (val1, val2, ref) => {
        if (Math.abs(val1 - val2) / ref > 1) return 0
        return 1 - Math.abs(val1 - val2) / ref
    }
    targetHaveAssEls.value.forEach((el) => {
        let layoutWeight = 1 / (el.alisaInfo.raceList.length + 1) / count
        let score = layoutWeight * 100
        let assEl = el.alisaInfo.assElement
        let halfWidth = el.alisaInfo.parentInfo.width / 2
        let halfHeight = el.alisaInfo.parentInfo.height / 2
        let biasXEqual = arraysEqual(
            el.alisaInfo.elInfo.biasX,
            assEl.alisaInfo.elInfo.biasX
        )
        let biasYEqual = arraysEqual(
            el.alisaInfo.elInfo.biasY,
            assEl.alisaInfo.elInfo.biasY
        )
        if (biasXEqual == 'not equal' || biasYEqual == 'not equal') score *= 0
        if (biasXEqual == 'partial equal' || biasYEqual == 'partial equal') score *= 0.5
        score =
            score *
            posDiff(
                el.alisaInfo.elInfo.pos.left,
                assEl.alisaInfo.elInfo.pos.left,
                halfWidth
            ) *
            posDiff(el.alisaInfo.elInfo.pos.top, assEl.alisaInfo.elInfo.pos.top, halfHeight)
        el.alisaInfo.assLayoutSimScore = score
    })
    let relativeLayoutScore = 0
    targetHaveAssEls.value.forEach((el) => {
        relativeLayoutScore += el.alisaInfo.assLayoutSimScore
    })

    let misMatchedLength = 0
    let matchedLength = 0
    targetEls.value.forEach((targetEl) => {
        let assEl = targetEl.alisaInfo.assElement
        if (!assEl) return
        let targetGroupAll = targetEl.alisaInfo.groupAll
        let sourceGroupAll = assEl.alisaInfo.groupAll
        targetGroupAll.forEach((gi) => {
            if (!gi.alisaInfo.assElement) misMatchedLength += 1
            else if (sourceGroupAll.indexOf(gi.alisaInfo.assElement) < 0)
                misMatchedLength += 1
            else matchedLength += 1
        })
    })
    let groupLayoutScore = (matchedLength / (matchedLength + misMatchedLength)) * 100

    if (misMatchedLength === 0 && matchedLength === 0) {
        groupLayoutScore = 0
    }

    console.log(
        relativeLayoutScore,
        groupLayoutScore,
        (relativeLayoutScore + groupLayoutScore) / 2
    )
    console.log(targetHaveAssEls.value)
    console.log(targetNoAssEls.value)

    return {
        relativeLayoutScore,
        groupLayoutScore,
        targetHaveAssEls: targetHaveAssEls.value.length,
        targetNoAssEls: targetNoAssEls.value.length,
        overallScore: (relativeLayoutScore + groupLayoutScore) / 2
    }
}

const getStyleSim = (sources, offset = 5, callback = null) => {
    getAllAssociated(sources, offset, callback);
    let allTargetEls = targetEls.value
    let viewedEls = []
    let count = 0
    allTargetEls.forEach((ei) => {
        if (viewedEls.indexOf(ei) < 0) {
            count += 1
            viewedEls.push(ei)
            ei.alisaInfo.raceList.forEach((ri) => {
                viewedEls.push(ri)
            })
        }
    })

    const DEFAULT_COLOR = 'rgba(0, 0, 0, 0)'

    const getGradientColors = (gradientCSS) => {
        const colorRegex = /rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*\d?\.?\d+)?\s*\)/gi;
        let colors = gradientCSS.match(colorRegex) || [];
        if (colors.length === 0) return null;
        // return avg. color if multiple colors found
        const colorObjs = colors.map(color => onecolor(color));
        let avgColor = { _red: 0, _green: 0, _blue: 0 };
        for (let color of colorObjs) {
            avgColor._red += color._red;
            avgColor._green += color._green;
            avgColor._blue += color._blue;
        }
        avgColor._red = Math.round(avgColor._red / colorObjs.length);
        avgColor._green = Math.round(avgColor._green / colorObjs.length);
        avgColor._blue = Math.round(avgColor._blue / colorObjs.length);
        return avgColor;
    }

    const perceptualColorDistance = (c1, c2) => {
        const rMean = (c1._red + c2._red) / 2;
        const rDiff = c1._red - c2._red;
        const gDiff = c1._green - c2._green;
        const bDiff = c1._blue - c2._blue;
        return Math.sqrt(
            (2 + rMean / 256) * rDiff * rDiff +
            4 * gDiff * gDiff +
            (2 + (255 - rMean) / 256) * bDiff * bDiff
        );
    }

    let computedStyles = (el) => {
        return el.alisaInfo.styles;
    }

    let colorDiff = (tgt, src) => {
        const color1 = onecolor(computedStyles(src).color || DEFAULT_COLOR)
        const color2 = onecolor(computedStyles(tgt).color || DEFAULT_COLOR)
        const colorDiff = perceptualColorDistance(color1, color2);
        return 1 - colorDiff < 0 ? 0 : 1 - colorDiff;
    }

    let backgroundColorDiff = (tgt, src) => {
        let imageColor1 = getGradientColors(computedStyles(src).backgroundImage)
        let imageColor2 = getGradientColors(computedStyles(tgt).backgroundImage)
        let colorDiff = 0;
        if (imageColor1 || imageColor2) {
            if (!imageColor1) imageColor1 = onecolor(DEFAULT_COLOR);
            if (!imageColor2) imageColor2 = onecolor(DEFAULT_COLOR);
            colorDiff = perceptualColorDistance(imageColor1, imageColor2);
        }
        else {
            const color1 = onecolor(computedStyles(src).backgroundColor || DEFAULT_COLOR)
            const color2 = onecolor(computedStyles(tgt).backgroundColor || DEFAULT_COLOR)
            colorDiff = perceptualColorDistance(color1, color2);
        }
        return 1 - colorDiff < 0 ? 0 : 1 - colorDiff;
    }

    let fontSizeDiff = (tgt, src) => {
        let size1 = computedStyles(src).fontSize;
        let size2 = computedStyles(tgt).fontSize;
        size1 = parseFloat(size1);
        size2 = parseFloat(size2);
        let sizeDiff = Math.abs(size1 - size2);
        return 1 - sizeDiff / size2;
    }

    let fontWeightDiff = (tgt, src) => {
        let fontWeight1 = computedStyles(src).fontWeight;
        let fontWeight2 = computedStyles(tgt).fontWeight;
        fontWeight1 = parseFloat(fontWeight1);
        fontWeight2 = parseFloat(fontWeight2);
        if (fontWeight1.toString() === 'NaN') fontWeight1 = 400;
        if (fontWeight2.toString() === 'NaN') fontWeight2 = 400;
        let sizeDiff = Math.abs(fontWeight1 - fontWeight2);
        return 1 - sizeDiff / fontWeight2;
    }

    let positionDiff = (tgt, src) => {
        let position1 = computedStyles(src).position;
        let position2 = computedStyles(tgt).position;
        return position1 == position2 ? 1 : 0;
    }

    let fontStyleDiff = (tgt, src) => {
        let fontStyle1 = computedStyles(src).fontStyle;
        let fontStyle2 = computedStyles(tgt).fontStyle;
        return fontStyle1 == fontStyle2 ? 1 : 0;
    }

    let borderRadiusDiff = (tgt, src) => {
        let topLeft1 = computedStyles(src).borderTopLeftRadius;
        let topLeft2 = computedStyles(tgt).borderTopLeftRadius;
        let topRight1 = computedStyles(src).borderTopRightRadius;
        let topRight2 = computedStyles(tgt).borderTopRightRadius;
        let bottomLeft1 = computedStyles(src).borderBottomLeftRadius;
        let bottomLeft2 = computedStyles(tgt).borderBottomLeftRadius;
        let bottomRight1 = computedStyles(src).borderBottomRightRadius;
        let bottomRight2 = computedStyles(tgt).borderBottomRightRadius;
        topLeft1 = parseFloat(topLeft1)
        topLeft2 = parseFloat(topLeft2)
        topRight1 = parseFloat(topRight1)
        topRight2 = parseFloat(topRight2)
        bottomLeft1 = parseFloat(bottomLeft1)
        bottomLeft2 = parseFloat(bottomLeft2)
        bottomRight1 = parseFloat(bottomRight1)
        bottomRight2 = parseFloat(bottomRight2)
        let topLeftDiff = Math.abs(topLeft1 - topLeft2);
        let topRightDiff = Math.abs(topRight1 - topRight2);
        let bottomLeftDiff = Math.abs(bottomLeft1 - bottomLeft2);
        let bottomRightDiff = Math.abs(bottomRight1 - bottomRight2);
        let targetRelative = tgt.alisaInfo.elInfo.pos.height;
        targetRelative = targetRelative > tgt.alisaInfo.elInfo.pos.width ? tgt.alisaInfo.elInfo.pos.width / 2 : targetRelative / 2;
        let avg = (topLeftDiff + topRightDiff + bottomLeftDiff + bottomRightDiff) / targetRelative;
        avg = avg > 1 ? 1 : avg;
        return 1 - avg;
    }

    let borderWidthDiff = (tgt, src) => {
        let top1 = computedStyles(src).borderTopWidth;
        let top2 = computedStyles(tgt).borderTopWidth;
        let right1 = computedStyles(src).borderRightWidth;
        let right2 = computedStyles(tgt).borderRightWidth;
        let bottom1 = computedStyles(src).borderBottomWidth;
        let bottom2 = computedStyles(tgt).borderBottomWidth;
        let left1 = computedStyles(src).borderLeftWidth;
        let left2 = computedStyles(tgt).borderLeftWidth;
        top1 = parseFloat(top1)
        top2 = parseFloat(top2)
        right1 = parseFloat(right1)
        right2 = parseFloat(right2)
        bottom1 = parseFloat(bottom1)
        bottom2 = parseFloat(bottom2)
        left1 = parseFloat(left1)
        left2 = parseFloat(left2)
        let topDiff = Math.abs(top1 - top2);
        let rightDiff = Math.abs(right1 - right2);
        let bottomDiff = Math.abs(bottom1 - bottom2);
        let leftDiff = Math.abs(left1 - left2);
        let targetRelative = tgt.alisaInfo.elInfo.pos.height;
        targetRelative = targetRelative > tgt.alisaInfo.elInfo.pos.width ? tgt.alisaInfo.elInfo.pos.width / 2 : targetRelative / 2;
        let avg = (topDiff + rightDiff + bottomDiff + leftDiff) / targetRelative;
        avg = avg > 1 ? 1 : avg;
        return 1 - avg;
    }

    let opacityDiff = (tgt, src) => {
        let opacity1 = computedStyles(src).opacity;
        let opacity2 = computedStyles(tgt).opacity;
        opacity1 = parseFloat(opacity1);
        opacity2 = parseFloat(opacity2);
        if (opacity1.toString() === 'NaN') opacity1 = 1;
        if (opacity2.toString() === 'NaN') opacity2 = 1;
        let sizeDiff = Math.abs(opacity1 - opacity2);
        return 1 - sizeDiff / opacity2;
    }

    targetHaveAssEls.value.forEach((el) => {
        let layoutWeight = 1 / (el.alisaInfo.raceList.length + 1) / count
        let score = layoutWeight * 100
        let unitScoreList = [];
        let assEl = el.alisaInfo.assElement
        if (styleCheckList.value.isPosition)
            unitScoreList.push(positionDiff(el, assEl));
        if (styleCheckList.value.isColor)
            unitScoreList.push(colorDiff(el, assEl));
        if (styleCheckList.value.isBackgroundColor)
            unitScoreList.push(backgroundColorDiff(el, assEl));
        if (styleCheckList.value.isFontSize)
            unitScoreList.push(fontSizeDiff(el, assEl));
        if (styleCheckList.value.isFontStyle)
            unitScoreList.push(fontStyleDiff(el, assEl));
        if (styleCheckList.value.isFontWeight)
            unitScoreList.push(fontWeightDiff(el, assEl));
        if (styleCheckList.value.isBorderRadius)
            unitScoreList.push(borderRadiusDiff(el, assEl))
        if (styleCheckList.value.isBorderWidth)
            unitScoreList.push(borderWidthDiff(el, assEl))
        if (styleCheckList.value.isOpacity)
            unitScoreList.push(opacityDiff(el, assEl))
        const sum = unitScoreList.reduce((acc, val) => acc + val, 0);
        const avg = sum / unitScoreList.length;
        el.alisaInfo.assStyleSimScore = score * avg;
    })

    let relativeStyleScore = 0
    targetHaveAssEls.value.forEach((el) => {
        relativeStyleScore += el.alisaInfo.assStyleSimScore
    })
    console.log(relativeStyleScore)

    return {
        relativeStyleScore
    }
}

const getElAssociated = (targetEl, sourceElList, offset = 5, callBack = null) => {
    let candidateList = [...sourceElList]
    let sizeDiff = (source, target) => {
        return (
            Math.abs(
                source.alisaInfo.elInfo.pos.width - target.alisaInfo.elInfo.pos.width
            ) +
            Math.abs(
                source.alisaInfo.elInfo.pos.height - target.alisaInfo.elInfo.pos.height
            )
        )
    }
    let posDiff = (source, target) => {
        return Math.sqrt(
            Math.pow(
                target.alisaInfo.elInfo.pos.left - source.alisaInfo.elInfo.pos.left,
                2
            ) +
            Math.pow(
                target.alisaInfo.elInfo.pos.top - source.alisaInfo.elInfo.pos.top,
                2
            )
        )
    }
    candidateList.sort((a, b) => {
        const aSizeDiff = sizeDiff(a, targetEl)
        const bSizeDiff = sizeDiff(b, targetEl)
        if (aSizeDiff == bSizeDiff) {
            const aPosDiff = posDiff(a, targetEl)
            const bPosDiff = posDiff(b, targetEl)
            return aPosDiff - bPosDiff
        }
        return aSizeDiff - bSizeDiff
    })
    if (targetEl.innerText == '' || !targetEl.innerText) {
        for (let item of candidateList) {
            if (sizeDiff(item, targetEl) <= offset * 2) {
                targetEl.alisaInfo.assElement = item
                item.alisaInfo.assElement = targetEl
                targetHaveAssEls.value.push(targetEl)
                if (callBack)
                    callBack(targetEl, item)
                return
            }
        }
    } else {
        for (let item of candidateList) {
            if (LCSSim(item.innerText, targetEl.innerText) >= 0.8) {
                targetEl.alisaInfo.assElement = item
                item.alisaInfo.assElement = targetEl
                targetHaveAssEls.value.push(targetEl)
                if (callBack)
                    callBack(targetEl, item)
                return
            }
        }
    }
    targetNoAssEls.value.push(targetEl)
}

const LCS = (a, b) => {
    const dp = Array(a.length + 1).fill()
        .map(() => Array(b.length + 1).fill(0))
    for (let i = 1; i <= a.length; i++) {
        for (let j = 1; j <= b.length; j++) {
            dp[i][j] =
                a[i - 1] === b[j - 1]
                    ? dp[i - 1][j - 1] + 1
                    : Math.max(dp[i - 1][j], dp[i][j - 1])
        }
    }
    return dp[a.length][b.length]
}

const LCSSim = (a, b) => {
    if (!a || !b) return 0;
    const lcsLength = LCS(a, b)
    return lcsLength / Math.max(a.length, b.length)
}