Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterClasses.js

Issue 29550598: Issue 5735 - Use JS Map instead of Object for Filter.knownFilters (Closed) Base URL: https://github.com/adblockplus/adblockpluscore.git
Patch Set: address comments Created Sept. 20, 2017, 11:31 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | lib/filterStorage.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 }, 72 },
73 73
74 toString() 74 toString()
75 { 75 {
76 return this.text; 76 return this.text;
77 } 77 }
78 }; 78 };
79 79
80 /** 80 /**
81 * Cache for known filters, maps string representation to filter objects. 81 * Cache for known filters, maps string representation to filter objects.
82 * @type {Object} 82 * @type {Map.<string,Filter>}
83 */ 83 */
84 Filter.knownFilters = Object.create(null); 84 Filter.knownFilters = new Map();
85 85
86 /** 86 /**
87 * Regular expression that element hiding filters should match 87 * Regular expression that element hiding filters should match
88 * @type {RegExp} 88 * @type {RegExp}
89 */ 89 */
90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/; 90 Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/;
91 /** 91 /**
92 * Regular expression that RegExp filters specified as RegExps should match 92 * Regular expression that RegExp filters specified as RegExps should match
93 * @type {RegExp} 93 * @type {RegExp}
94 */ 94 */
95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/; 95 Filter.regexpRegExp = /^(@@)?\/.*\/(?:\$~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^, \s]+)?)*)?$/;
96 /** 96 /**
97 * Regular expression that options on a RegExp filter should match 97 * Regular expression that options on a RegExp filter should match
98 * @type {RegExp} 98 * @type {RegExp}
99 */ 99 */
100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)$/; 100 Filter.optionsRegExp = /\$(~?[\w-]+(?:=[^,\s]+)?(?:,~?[\w-]+(?:=[^,\s]+)?)*)$/;
101 101
102 /** 102 /**
103 * Creates a filter of correct type from its text representation - does the 103 * Creates a filter of correct type from its text representation - does the
104 * basic parsing and calls the right constructor then. 104 * basic parsing and calls the right constructor then.
105 * 105 *
106 * @param {string} text as in Filter() 106 * @param {string} text as in Filter()
107 * @return {Filter} 107 * @return {Filter}
108 */ 108 */
109 Filter.fromText = function(text) 109 Filter.fromText = function(text)
110 { 110 {
111 if (text in Filter.knownFilters) 111 let ret = Filter.knownFilters.get(text);
112 return Filter.knownFilters[text]; 112 if (ret)
kzar 2017/09/20 12:30:24 Just an observation since I don't think it matters
113 return ret;
113 114
114 let ret;
115 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null); 115 let match = (text.includes("#") ? Filter.elemhideRegExp.exec(text) : null);
116 if (match) 116 if (match)
117 { 117 {
118 let propsMatch; 118 let propsMatch;
119 if (!match[2] && 119 if (!match[2] &&
120 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3]))) 120 (propsMatch = /\[-abp-properties=(["'])([^"']+)\1\]/.exec(match[3])))
121 { 121 {
122 // This is legacy CSS properties syntax, convert to current syntax 122 // This is legacy CSS properties syntax, convert to current syntax
123 let prefix = match[3].substr(0, propsMatch.index); 123 let prefix = match[3].substr(0, propsMatch.index);
124 let expression = propsMatch[2]; 124 let expression = propsMatch[2];
125 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length); 125 let suffix = match[3].substr(propsMatch.index + propsMatch[0].length);
126 return Filter.fromText(`${match[1]}#?#` + 126 return Filter.fromText(`${match[1]}#?#` +
127 `${prefix}:-abp-properties(${expression})${suffix}`); 127 `${prefix}:-abp-properties(${expression})${suffix}`);
128 } 128 }
129 129
130 ret = ElemHideBase.fromText( 130 ret = ElemHideBase.fromText(
131 text, match[1], match[2], match[3] 131 text, match[1], match[2], match[3]
132 ); 132 );
133 } 133 }
134 else if (text[0] == "!") 134 else if (text[0] == "!")
135 ret = new CommentFilter(text); 135 ret = new CommentFilter(text);
136 else 136 else
137 ret = RegExpFilter.fromText(text); 137 ret = RegExpFilter.fromText(text);
138 138
139 Filter.knownFilters[ret.text] = ret; 139 Filter.knownFilters.set(ret.text, ret);
140 return ret; 140 return ret;
141 }; 141 };
142 142
143 /** 143 /**
144 * Deserializes a filter 144 * Deserializes a filter
145 * 145 *
146 * @param {Object} obj map of serialized properties and their values 146 * @param {Object} obj map of serialized properties and their values
147 * @return {Filter} filter or null if the filter couldn't be created 147 * @return {Filter} filter or null if the filter couldn't be created
148 */ 148 */
149 Filter.fromObject = function(obj) 149 Filter.fromObject = function(obj)
(...skipping 870 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 */ 1020 */
1021 function ElemHideEmulationFilter(text, domains, selector) 1021 function ElemHideEmulationFilter(text, domains, selector)
1022 { 1022 {
1023 ElemHideBase.call(this, text, domains, selector); 1023 ElemHideBase.call(this, text, domains, selector);
1024 } 1024 }
1025 exports.ElemHideEmulationFilter = ElemHideEmulationFilter; 1025 exports.ElemHideEmulationFilter = ElemHideEmulationFilter;
1026 1026
1027 ElemHideEmulationFilter.prototype = extend(ElemHideBase, { 1027 ElemHideEmulationFilter.prototype = extend(ElemHideBase, {
1028 type: "elemhideemulation" 1028 type: "elemhideemulation"
1029 }); 1029 });
OLDNEW
« no previous file with comments | « no previous file | lib/filterStorage.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld