fix #2793: user-friendlier normalization

This commit is contained in:
gorhill 2017-10-04 13:20:43 -04:00
parent ec14e56b02
commit bd18fe3901
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 26 additions and 19 deletions

View File

@ -846,11 +846,11 @@ FilterContainer.prototype.compileProceduralSelector = (function() {
reFirstParentheses = /^\(*/, reFirstParentheses = /^\(*/,
reLastParentheses = /\)*$/, reLastParentheses = /\)*$/,
reEscapeRegex = /[.*+?^${}()|[\]\\]/g, reEscapeRegex = /[.*+?^${}()|[\]\\]/g,
reNeedScope = /^\s*[+>~]/, reNeedScope = /^\s*[+>~]/;
reAllForwardSlashes = /\//g;
var lastProceduralSelector = '', var lastProceduralSelector = '',
lastProceduralSelectorCompiled; lastProceduralSelectorCompiled,
regexToRawValue = new Map();
var compileCSSSelector = function(s) { var compileCSSSelector = function(s) {
// https://github.com/AdguardTeam/ExtendedCss/issues/31#issuecomment-302391277 // https://github.com/AdguardTeam/ExtendedCss/issues/31#issuecomment-302391277
@ -864,28 +864,31 @@ FilterContainer.prototype.compileProceduralSelector = (function() {
}; };
var compileText = function(s) { var compileText = function(s) {
var reText;
if ( reIsRegexLiteral.test(s) ) { if ( reIsRegexLiteral.test(s) ) {
s = s.slice(1, -1); reText = s.slice(1, -1);
if ( isBadRegex(s) ) { return; } if ( isBadRegex(reText) ) { return; }
} else { } else {
s = s.replace(reEscapeRegex, '\\$&'); reText = s.replace(reEscapeRegex, '\\$&');
regexToRawValue.set(reText, s);
} }
return s; return reText;
}; };
var compileCSSDeclaration = function(s) { var compileCSSDeclaration = function(s) {
var name, value, var name, value, reText,
pos = s.indexOf(':'); pos = s.indexOf(':');
if ( pos === -1 ) { return; } if ( pos === -1 ) { return; }
name = s.slice(0, pos).trim(); name = s.slice(0, pos).trim();
value = s.slice(pos + 1).trim(); value = s.slice(pos + 1).trim();
if ( reIsRegexLiteral.test(value) ) { if ( reIsRegexLiteral.test(value) ) {
value = value.slice(1, -1); reText = value.slice(1, -1);
if ( isBadRegex(value) ) { return; } if ( isBadRegex(reText) ) { return; }
} else { } else {
value = value.replace(reEscapeRegex, '\\$&'); reText = '^' + value.replace(reEscapeRegex, '\\$&') + '$';
regexToRawValue.set(reText, value);
} }
return { name: name, value: value }; return { name: name, value: reText };
}; };
var compileConditionalSelector = function(s) { var compileConditionalSelector = function(s) {
@ -933,7 +936,8 @@ FilterContainer.prototype.compileProceduralSelector = (function() {
// design. // design.
var decompile = function(compiled) { var decompile = function(compiled) {
var raw = [ compiled.selector ], var raw = [ compiled.selector ],
tasks = compiled.tasks; tasks = compiled.tasks,
value;
if ( Array.isArray(tasks) ) { if ( Array.isArray(tasks) ) {
for ( var i = 0, n = tasks.length, task; i < n; i++ ) { for ( var i = 0, n = tasks.length, task; i < n; i++ ) {
task = tasks[i]; task = tasks[i];
@ -943,23 +947,25 @@ FilterContainer.prototype.compileProceduralSelector = (function() {
raw.push(task[0], '(', task[1], ')'); raw.push(task[0], '(', task[1], ')');
break; break;
case ':has-text': case ':has-text':
value = regexToRawValue.get(task[1]);
raw.push( raw.push(
task[0], task[0],
'(/', '(',
task[1].replace(reAllForwardSlashes, '\\/'), value !== undefined ? value : '/' + task[1] + '/',
'/)' ')'
); );
break; break;
case ':matches-css': case ':matches-css':
case ':matches-css-after': case ':matches-css-after':
case ':matches-css-before': case ':matches-css-before':
value = regexToRawValue.get(task[1].value);
raw.push( raw.push(
task[0], task[0],
'(', '(',
task[1].name, task[1].name,
': /', ': ',
task[1].value.replace(reAllForwardSlashes, '\\/'), value !== undefined ? value : '/' + task[1].value + '/',
'/)' ')'
); );
break; break;
case ':if': case ':if':
@ -1034,6 +1040,7 @@ FilterContainer.prototype.compileProceduralSelector = (function() {
}; };
entryPoint.reset = function() { entryPoint.reset = function() {
regexToRawValue = new Map();
lastProceduralSelector = ''; lastProceduralSelector = '';
lastProceduralSelectorCompiled = undefined; lastProceduralSelectorCompiled = undefined;
}; };