OLD | NEW |
1 (function() | 1 "use strict"; |
| 2 |
2 { | 3 { |
3 var filterValidation = require("filterValidation"); | 4 let filterValidation = require("filterValidation"); |
4 var parseFilter = filterValidation.parseFilter; | 5 let parseFilter = filterValidation.parseFilter; |
5 var parseFilters = filterValidation.parseFilters; | 6 let parseFilters = filterValidation.parseFilters; |
6 | 7 |
7 var filterClasses = require("filterClasses"); | 8 let filterClasses = require("filterClasses"); |
8 var BlockingFilter = filterClasses.BlockingFilter; | 9 let BlockingFilter = filterClasses.BlockingFilter; |
9 var ElemHideFilter = filterClasses.ElemHideFilter; | 10 let ElemHideFilter = filterClasses.ElemHideFilter; |
10 var CommentFilter = filterClasses.CommentFilter; | 11 let CommentFilter = filterClasses.CommentFilter; |
11 | 12 |
12 module("Filter validation"); | 13 module("Filter validation"); |
13 | 14 |
14 test("Detecting invalid filters", function() | 15 test("Detecting invalid filters", () => |
15 { | 16 { |
16 equal(parseFilter("||example.com^$unknown").error.type, "invalid-filter", "u
nknown option"); | 17 equal(parseFilter("||example.com^$unknown").error.type, "invalid-filter", "u
nknown option"); |
17 equal(parseFilter("[foobar]").error.type, "unexpected-filter-list-header", "
filter list header"); | 18 equal(parseFilter("[foobar]").error.type, "unexpected-filter-list-header", "
filter list header"); |
18 equal(parseFilter("##[foo").error.type, "invalid-css-selector", "invalid sel
ector"); | 19 equal(parseFilter("##[foo").error.type, "invalid-css-selector", "invalid sel
ector"); |
19 ok(/\b4\b/.test(parseFilters("!comment\r\n||example.com^\n\n##/").errors[0])
, "error contains corresponding line number"); | 20 ok(/\b4\b/.test(parseFilters("!comment\r\n||example.com^\n\n##/").errors[0])
, "error contains corresponding line number"); |
20 }); | 21 }); |
21 | 22 |
22 test("Allowing valid filters", function() | 23 test("Allowing valid filters", () => |
23 { | 24 { |
24 var text, filter; | 25 let text, filter; |
25 | 26 |
26 text = "||example.com^"; | 27 text = "||example.com^"; |
27 filter = parseFilter(text).filter; | 28 filter = parseFilter(text).filter; |
28 ok(filter instanceof BlockingFilter, "blocking filter parsed"); | 29 ok(filter instanceof BlockingFilter, "blocking filter parsed"); |
29 equal(filter.text, text, "blocking filter text matches"); | 30 equal(filter.text, text, "blocking filter text matches"); |
30 | 31 |
31 text = '##div:first-child a[src="http://example.com"] > .foo + #bar'; | 32 text = '##div:first-child a[src="http://example.com"] > .foo + #bar'; |
32 filter = parseFilter(text).filter; | 33 filter = parseFilter(text).filter; |
33 ok(filter instanceof ElemHideFilter, "elemhide filter parsed"); | 34 ok(filter instanceof ElemHideFilter, "elemhide filter parsed"); |
34 equal(filter.text, text, "elemhide filter text matches"); | 35 equal(filter.text, text, "elemhide filter text matches"); |
35 | 36 |
36 text = "! foo bar"; | 37 text = "! foo bar"; |
37 filter = parseFilter(text).filter; | 38 filter = parseFilter(text).filter; |
38 ok(filter instanceof CommentFilter, "comment filter parsed"); | 39 ok(filter instanceof CommentFilter, "comment filter parsed"); |
39 equal(filter.text, text, "comment filter text matches"); | 40 equal(filter.text, text, "comment filter text matches"); |
40 | 41 |
41 equal(parseFilter("").filter, null, "empty filter parsed as 'null'"); | 42 equal(parseFilter("").filter, null, "empty filter parsed as 'null'"); |
42 }); | 43 }); |
43 | 44 |
44 test("Normalizing filters", function() | 45 test("Normalizing filters", () => |
45 { | 46 { |
46 var ws = " \t\r\n"; | 47 let ws = " \t\r\n"; |
47 | 48 |
48 equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws
).filter.text, "@@||example.com^", "unnecessary spaces"); | 49 equal(parseFilter(ws + "@@" + ws + "||" + ws + "example.com" + ws + "^" + ws
).filter.text, "@@||example.com^", "unnecessary spaces"); |
49 equal(parseFilter(ws).filter, null, "only spaces"); | 50 equal(parseFilter(ws).filter, null, "only spaces"); |
50 }); | 51 }); |
51 | 52 |
52 test("Parsing multiple filters", function() | 53 test("Parsing multiple filters", () => |
53 { | 54 { |
54 var result = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n"); | 55 let result = parseFilters("||example.com^\n \n###foobar\r\n! foo bar\n"); |
55 | 56 |
56 equal(result.errors.length, 0, "no error occurred"); | 57 equal(result.errors.length, 0, "no error occurred"); |
57 equal(result.filters.length, 3, "all filters parsed"); | 58 equal(result.filters.length, 3, "all filters parsed"); |
58 | 59 |
59 ok(result.filters[0] instanceof BlockingFilter, "1st filter is blocking"); | 60 ok(result.filters[0] instanceof BlockingFilter, "1st filter is blocking"); |
60 equal(result.filters[0].text, "||example.com^", "1st filter text matches"); | 61 equal(result.filters[0].text, "||example.com^", "1st filter text matches"); |
61 | 62 |
62 ok(result.filters[1] instanceof ElemHideFilter, "2nd filter is elemhide"); | 63 ok(result.filters[1] instanceof ElemHideFilter, "2nd filter is elemhide"); |
63 equal(result.filters[1].text, "###foobar", "2nd filter text matches"); | 64 equal(result.filters[1].text, "###foobar", "2nd filter text matches"); |
64 | 65 |
65 ok(result.filters[2] instanceof CommentFilter, "3rd filter is comment"); | 66 ok(result.filters[2] instanceof CommentFilter, "3rd filter is comment"); |
66 equal(result.filters[2].text, "! foo bar", "3rd filter text matches"); | 67 equal(result.filters[2].text, "! foo bar", "3rd filter text matches"); |
67 }); | 68 }); |
68 })(); | 69 } |
OLD | NEW |