UNPKG

7.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3/**
4 * Cache to store generated match functions
5 * @type {Object}
6 */
7var pMatchFunctionCache = {};
8/**
9 * Function cache
10 */
11var functionCache = {
12 f145: function (el, tagName, classes) {
13 'use strict';
14 tagName = tagName || '';
15 classes = classes || [];
16 if (el.id !== tagName.substr(1)) {
17 return false;
18 }
19 for (var cls = classes, i = 0; i < cls.length; i++) {
20 if (el.classNames.indexOf(cls[i]) === -1) {
21 return false;
22 }
23 }
24 return true;
25 },
26 f45: function (el, tagName, classes) {
27 'use strict';
28 tagName = tagName || '';
29 classes = classes || [];
30 for (var cls = classes, i = 0; i < cls.length; i++) {
31 if (el.classNames.indexOf(cls[i]) === -1) {
32 return false;
33 }
34 }
35 return true;
36 },
37 f15: function (el, tagName) {
38 'use strict';
39 tagName = tagName || '';
40 if (el.id !== tagName.substr(1)) {
41 return false;
42 }
43 return true;
44 },
45 f1: function (el, tagName) {
46 'use strict';
47 tagName = tagName || '';
48 if (el.id !== tagName.substr(1)) {
49 return false;
50 }
51 },
52 f5: function () {
53 'use strict';
54 return true;
55 },
56 f245: function (el, tagName, classes, attr_key, value) {
57 'use strict';
58 tagName = tagName || '';
59 classes = classes || [];
60 attr_key = attr_key || '';
61 value = value || '';
62 var attrs = el.attributes;
63 return Object.keys(attrs).some(function (key) {
64 var val = attrs[key];
65 return key === attr_key && val === value;
66 });
67 // for (let cls = classes, i = 0; i < cls.length; i++) {if (el.classNames.indexOf(cls[i]) === -1){ return false;}}
68 // return true;
69 },
70 f25: function (el, tagName, classes, attr_key, value) {
71 'use strict';
72 tagName = tagName || '';
73 classes = classes || [];
74 attr_key = attr_key || '';
75 value = value || '';
76 var attrs = el.attributes;
77 return Object.keys(attrs).some(function (key) {
78 var val = attrs[key];
79 return key === attr_key && val === value;
80 });
81 // return true;
82 },
83 f2: function (el, tagName, classes, attr_key, value) {
84 'use strict';
85 tagName = tagName || '';
86 classes = classes || [];
87 attr_key = attr_key || '';
88 value = value || '';
89 var attrs = el.attributes;
90 return Object.keys(attrs).some(function (key) {
91 var val = attrs[key];
92 return key === attr_key && val === value;
93 });
94 },
95 f345: function (el, tagName, classes) {
96 'use strict';
97 tagName = tagName || '';
98 classes = classes || [];
99 if (el.tagName !== tagName) {
100 return false;
101 }
102 for (var cls = classes, i = 0; i < cls.length; i++) {
103 if (el.classNames.indexOf(cls[i]) === -1) {
104 return false;
105 }
106 }
107 return true;
108 },
109 f35: function (el, tagName) {
110 'use strict';
111 tagName = tagName || '';
112 return el.tagName === tagName;
113 },
114 f3: function (el, tagName) {
115 'use strict';
116 tagName = tagName || '';
117 if (el.tagName !== tagName) {
118 return false;
119 }
120 }
121};
122/**
123 * Matcher class to make CSS match
124 *
125 * @class Matcher
126 */
127var Matcher = /** @class */ (function () {
128 /**
129 * Creates an instance of Matcher.
130 * @param {string} selector
131 *
132 * @memberof Matcher
133 */
134 function Matcher(selector) {
135 this.nextMatch = 0;
136 functionCache.f5 = functionCache.f5;
137 this.matchers = selector.split(' ').map(function (matcher) {
138 if (pMatchFunctionCache[matcher])
139 return pMatchFunctionCache[matcher];
140 var parts = matcher.split('.');
141 var tagName = parts[0];
142 var classes = parts.slice(1).sort();
143 // let source = '"use strict";';
144 var function_name = 'f';
145 var attr_key = '';
146 var value = '';
147 if (tagName && tagName !== '*') {
148 var reg = void 0;
149 if (tagName.startsWith('#')) {
150 // source += 'if (el.id != ' + JSON.stringify(tagName.substr(1)) + ') return false;';// 1
151 function_name += '1';
152 }
153 else {
154 reg = /^\[\s*(\S+)\s*(=|!=)\s*((((["'])([^\6]*)\6))|(\S*?))\]\s*/.exec(tagName);
155 if (reg) {
156 attr_key = reg[1];
157 var method = reg[2];
158 if (method !== '=' && method !== '!=') {
159 throw new Error('Selector not supported, Expect [key${op}value].op must be =,!=');
160 }
161 if (method === '=') {
162 method = '==';
163 }
164 value = reg[7] || reg[8];
165 // source += `let attrs = el.attributes;for (let key in attrs){const val = attrs[key]; if (key == "${attr_key}" && val == "${value}"){return true;}} return false;`;// 2
166 function_name += '2';
167 }
168 else {
169 // source += 'if (el.tagName != ' + JSON.stringify(tagName) + ') return false;';// 3
170 function_name += '3';
171 }
172 }
173 }
174 if (classes.length > 0) {
175 // source += 'for (let cls = ' + JSON.stringify(classes) + ', i = 0; i < cls.length; i++) if (el.classNames.indexOf(cls[i]) === -1) return false;';// 4
176 function_name += '4';
177 }
178 // source += 'return true;';// 5
179 function_name += '5';
180 var obj = {
181 func: functionCache[function_name],
182 tagName: tagName || '',
183 classes: classes || '',
184 attr_key: attr_key || '',
185 value: value || ''
186 };
187 // source = source || '';
188 return pMatchFunctionCache[matcher] = obj;
189 });
190 }
191 /**
192 * Trying to advance match pointer
193 * @param {HTMLElement} el element to make the match
194 * @return {bool} true when pointer advanced.
195 */
196 Matcher.prototype.advance = function (el) {
197 if (this.nextMatch < this.matchers.length &&
198 this.matchers[this.nextMatch].func(el, this.matchers[this.nextMatch].tagName, this.matchers[this.nextMatch].classes, this.matchers[this.nextMatch].attr_key, this.matchers[this.nextMatch].value)) {
199 this.nextMatch++;
200 return true;
201 }
202 return false;
203 };
204 /**
205 * Rewind the match pointer
206 */
207 Matcher.prototype.rewind = function () {
208 this.nextMatch--;
209 };
210 Object.defineProperty(Matcher.prototype, "matched", {
211 /**
212 * Trying to determine if match made.
213 * @return {bool} true when the match is made
214 */
215 get: function () {
216 return this.nextMatch === this.matchers.length;
217 },
218 enumerable: true,
219 configurable: true
220 });
221 /**
222 * Rest match pointer.
223 * @return {[type]} [description]
224 */
225 Matcher.prototype.reset = function () {
226 this.nextMatch = 0;
227 };
228 /**
229 * flush cache to free memory
230 */
231 Matcher.prototype.flushCache = function () {
232 pMatchFunctionCache = {};
233 };
234 return Matcher;
235}());
236exports.default = Matcher;