forked from RedDragonWebDesign/adblock-filter-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdBlockSyntaxBlock.js
105 lines (93 loc) · 2.44 KB
/
AdBlockSyntaxBlock.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
// Copyright https://www.RedDragonWebDesign.com/ All rights reserved.
"use strict";
import { AdBlockSyntaxLine } from './AdBlockSyntaxLine.js';
export class AdBlockSyntaxBlock {
string = "";
richText = "";
countTrue = 0;
countFalse = 0
countNotSure = 0;
countComments = 0;
countMismatches = 0;
lineCount = 0;
parseString(s) {
this._parse(s);
}
_unescapeHTML(input) {
return input
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, "\"")
.replace(/'/g, "'");
}
parseRichText(richText) {
let s = richText;
// remove spans
s = s.replace(/<span class=\".*?\">/g, "");
s = s.replace(/<\/span>/g, "");
// convert <br> to \n
s = s.replace(/<br>/g, "\n");
// remove <div> </div> - these are placed when the user hits enter
s = s.replace(/<div>/g, "\n");
s = s.replace(/<\/div>/g, "");
// remove <font color="#000000"> </font> - these are placed when the user hits enter
s = s.replace(/<font.*?>/g, "");
s = s.replace(/<\/font>/g, "");
// remove
s = s.replace(/ /g, " ");
// replace tab with 4 spaces
s = s.replace(/\t/g, " ");
s = this._unescapeHTML(s);
this._parse(s);
}
getRichText() {
return this.richText;
}
getLineCount() {
return this.lineCount;
}
getErrorCount() {
return this.countFalse;
}
_parse(s) {
this.string = s;
let lines = s.split("\n");
this.lineCount = lines.length - 1; // I'm not sure why array has an extra line, but it does, subtract 1 line from count
let counter = 0;
for ( let lineString of lines) {
this.richText += "<span class='line'>";
if ( lineString !== '' ) {
let line = new AdBlockSyntaxLine(lineString);
if ( ( line.isValid === false || line.isValid === "mismatch" ) ) {
counter++;
}
this.richText += line.getRichText();
// increment the true/false counters
this._incrementCounters(line);
}
this.richText += "</span>";
}
this.richText = this.richText.slice(0, this.richText.length - 4);
}
_incrementCounters(line) {
if ( line.syntax['comment'] ) {
this.countComments++;
} else {
switch( line.isValid ) {
case true:
this.countTrue++;
break;
case false:
this.countFalse++;
break;
case "not sure":
this.countNotSure++;
break;
case "mismatch":
this.countMismatches++;
break;
}
}
}
}