LEFT | RIGHT |
1 "use strict"; | 1 "use strict"; |
2 | 2 |
3 let readline = require("readline"); | 3 let readline = require("readline"); |
4 let punycode = require("punycode"); | 4 let punycode = require("punycode"); |
5 let tldjs = require("tldjs"); | 5 let tldjs = require("tldjs"); |
6 let filterClasses = require("./adblockplus.js"); | 6 let filterClasses = require("./adblockplus.js"); |
7 | 7 |
8 let typeMap = filterClasses.RegExpFilter.typeMap; | 8 let typeMap = filterClasses.RegExpFilter.typeMap; |
9 | 9 |
10 const selectorLimit = 5000; | 10 const selectorLimit = 5000; |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 // First we figure out where all the IDs are | 259 // First we figure out where all the IDs are |
260 let sep = ""; | 260 let sep = ""; |
261 let start = null; | 261 let start = null; |
262 let positions = []; | 262 let positions = []; |
263 for (let i = 0; i < selector.length; i++) | 263 for (let i = 0; i < selector.length; i++) |
264 { | 264 { |
265 let chr = selector[i]; | 265 let chr = selector[i]; |
266 | 266 |
267 if (chr == "\\") // ignore escaped characters | 267 if (chr == "\\") // ignore escaped characters |
268 i++; | 268 i++; |
269 else if (chr == sep) // don't split within quoted text | 269 else if (chr == sep) // don't match IDs within quoted text |
270 sep = ""; // e.g. [attr=","] | 270 sep = ""; // e.g. [attr="#Hello"] |
271 else if (sep == "") | 271 else if (sep == "") |
272 { | 272 { |
273 if (chr == '"' || chr == "'") | 273 if (chr == '"' || chr == "'") |
274 sep = chr; | 274 sep = chr; |
275 else if (start == null) // look for the start of an ID | 275 else if (start == null) // look for the start of an ID |
276 { | 276 { |
277 if (chr == "#") | 277 if (chr == "#") |
278 start = i; | 278 start = i; |
279 } | 279 } |
280 else if (chr != "-" && chr != "_" && | 280 else if (chr != "-" && chr != "_" && |
281 (chr < "0" || | 281 (chr < "0" || |
282 chr > "9" && chr < "A" || | 282 chr > "9" && chr < "A" || |
283 chr > "Z" && chr < "a" || | 283 chr > "Z" && chr < "a" || |
284 chr > "z" && chr < "\x80")) // look for the end of the ID | 284 chr > "z" && chr < "\x80")) // look for the end of the ID |
285 { | 285 { |
286 positions.push({start: start, end: i}); | 286 positions.push({start: start, end: i}); |
287 start = null; | 287 start = null; |
288 } | 288 } |
289 } | 289 } |
290 } | 290 } |
291 if (start != null) | 291 if (start != null) |
292 positions.push({start: start, end: selector.length}); | 292 positions.push({start: start, end: selector.length}); |
293 | 293 |
294 // Now replace them all with the [id="someID"] form | 294 // Now replace them all with the [id="someID"] form |
295 let newSelector = ""; | 295 let newSelector = []; |
296 let position = 0; | 296 let i = 0; |
297 for (let ID of positions) | 297 for (let pos of positions) |
298 { | 298 { |
299 newSelector += selector.substring(position, ID.start); | 299 newSelector.push(selector.substring(i, pos.start)); |
300 newSelector += '[id=' + selector.substring(ID.start + 1, ID.end) + ']'; | 300 newSelector.push('[id=' + selector.substring(pos.start + 1, pos.end) + ']'); |
301 position = ID.end; | 301 i = pos.end; |
302 } | 302 } |
303 newSelector += selector.substring(position); | 303 newSelector.push(selector.substring(i)); |
304 | 304 |
305 return newSelector; | 305 return newSelector.join(""); |
306 } | 306 } |
307 | 307 |
308 function logRules() | 308 function logRules() |
309 { | 309 { |
310 let rules = []; | 310 let rules = []; |
311 | 311 |
312 function addRule(rule) | 312 function addRule(rule) |
313 { | 313 { |
314 if (!hasNonASCI(rule)) | 314 if (!hasNonASCI(rule)) |
315 rules.push(rule); | 315 rules.push(rule); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 addRule(convertFilter(filter, "block", true)); | 358 addRule(convertFilter(filter, "block", true)); |
359 for (let filter of requestExceptions) | 359 for (let filter of requestExceptions) |
360 addRule(convertFilter(filter, "ignore-previous-rules", true)); | 360 addRule(convertFilter(filter, "ignore-previous-rules", true)); |
361 | 361 |
362 console.log(JSON.stringify(rules, null, "\t")); | 362 console.log(JSON.stringify(rules, null, "\t")); |
363 } | 363 } |
364 | 364 |
365 let rl = readline.createInterface({input: process.stdin, terminal: false}); | 365 let rl = readline.createInterface({input: process.stdin, terminal: false}); |
366 rl.on("line", parseFilter); | 366 rl.on("line", parseFilter); |
367 rl.on("close", logRules); | 367 rl.on("close", logRules); |
LEFT | RIGHT |