1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-06 19:02:30 +01:00

code review: to/from punycode conversion only if needed

This commit is contained in:
gorhill 2017-05-03 19:43:09 -04:00
parent 867b675b5c
commit 42afd0c3d0
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 51 additions and 48 deletions

View File

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uBlock Origin - a browser extension to block requests. uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2016 Raymond Hill Copyright (C) 2014-2017 Raymond Hill
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -444,9 +444,10 @@ Matrix.prototype.desHostnameFromRule = function(rule) {
/******************************************************************************/ /******************************************************************************/
Matrix.prototype.toString = function() { Matrix.prototype.toString = function() {
var out = []; var out = [],
var rule, type, val; rule, type, val,
var srcHostname, desHostname; srcHostname, desHostname,
toUnicode = punycode.toUnicode;
for ( rule in this.rules ) { for ( rule in this.rules ) {
if ( this.rules.hasOwnProperty(rule) === false ) { if ( this.rules.hasOwnProperty(rule) === false ) {
continue; continue;
@ -458,12 +459,16 @@ Matrix.prototype.toString = function() {
continue; continue;
} }
val = this.evaluateCell(srcHostname, desHostname, type); val = this.evaluateCell(srcHostname, desHostname, type);
if ( val === 0 ) { if ( val === 0 ) { continue; }
continue; if ( srcHostname.indexOf('xn--') !== -1 ) {
srcHostname = toUnicode(srcHostname);
}
if ( desHostname.indexOf('xn--') !== -1 ) {
desHostname = toUnicode(desHostname);
} }
out.push( out.push(
punycode.toUnicode(srcHostname) + ' ' + srcHostname + ' ' +
punycode.toUnicode(desHostname) + ' ' + desHostname + ' ' +
type + ' ' + type + ' ' +
actionToNameMap[val] actionToNameMap[val]
); );
@ -475,26 +480,18 @@ Matrix.prototype.toString = function() {
/******************************************************************************/ /******************************************************************************/
Matrix.prototype.fromString = function(text, append) { Matrix.prototype.fromString = function(text, append) {
var textEnd = text.length; var lineIter = new µBlock.LineIterator(text),
var lineBeg = 0, lineEnd; line, pos, fields,
var line, pos, fields; srcHostname, desHostname, type, action,
var srcHostname, desHostname, type, action; reNotASCII = /[^\x20-\x7F]/,
toASCII = punycode.toASCII;
if ( append !== true ) { if ( append !== true ) {
this.reset(); this.reset();
} }
while ( lineBeg < textEnd ) { while ( lineIter.eot() === false ) {
lineEnd = text.indexOf('\n', lineBeg); line = lineIter.next().trim();
if ( lineEnd < 0 ) {
lineEnd = text.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
pos = line.indexOf('# '); pos = line.indexOf('# ');
if ( pos !== -1 ) { if ( pos !== -1 ) {
line = line.slice(0, pos).trim(); line = line.slice(0, pos).trim();
@ -527,8 +524,16 @@ Matrix.prototype.fromString = function(text, append) {
continue; continue;
} }
srcHostname = punycode.toASCII(fields[0]); // Performance: avoid punycoding if hostnames are made only of
desHostname = punycode.toASCII(fields[1]); // ASCII characters.
srcHostname = fields[0];
if ( reNotASCII.test(srcHostname) ) {
srcHostname = toASCII(srcHostname);
}
desHostname = fields[1];
if ( reNotASCII.test(desHostname) ) {
desHostname = toASCII(desHostname);
}
// https://github.com/chrisaljoudi/uBlock/issues/1082 // https://github.com/chrisaljoudi/uBlock/issues/1082
// Discard rules with invalid hostnames // Discard rules with invalid hostnames

View File

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
uBlock Origin - a Chromium browser extension to black/white list requests. uBlock Origin - a Chromium browser extension to black/white list requests.
Copyright (C) 2015-2016 Raymond Hill Copyright (C) 2015-2017 Raymond Hill
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@ -258,9 +258,10 @@ HnSwitches.prototype.toResultString = function() {
/******************************************************************************/ /******************************************************************************/
HnSwitches.prototype.toString = function() { HnSwitches.prototype.toString = function() {
var out = []; var out = [],
var switchName, val; switchName, val,
var hostname; hostname,
toUnicode = punycode.toUnicode;
for ( hostname in this.switches ) { for ( hostname in this.switches ) {
if ( this.switches.hasOwnProperty(hostname) === false ) { if ( this.switches.hasOwnProperty(hostname) === false ) {
continue; continue;
@ -270,8 +271,9 @@ HnSwitches.prototype.toString = function() {
continue; continue;
} }
val = this.evaluate(switchName, hostname); val = this.evaluate(switchName, hostname);
if ( val === 0 ) { if ( val === 0 ) { continue; }
continue; if ( hostname.indexOf('xn--') !== -1 ) {
hostname = toUnicode(hostname);
} }
out.push(switchName + ': ' + hostname + ' ' + switchStateToNameMap[val]); out.push(switchName + ': ' + hostname + ' ' + switchStateToNameMap[val]);
} }
@ -282,25 +284,16 @@ HnSwitches.prototype.toString = function() {
/******************************************************************************/ /******************************************************************************/
HnSwitches.prototype.fromString = function(text) { HnSwitches.prototype.fromString = function(text) {
var textEnd = text.length; var lineIter = new µBlock.LineIterator(text),
var lineBeg = 0, lineEnd; line, pos, fields,
var line, pos; switchName, hostname, state,
var fields; reNotASCII = /[^\x20-\x7F]/,
var switchName, hostname, state; toASCII = punycode.toASCII;
this.reset(); this.reset();
while ( lineBeg < textEnd ) { while ( lineIter.eot() === false ) {
lineEnd = text.indexOf('\n', lineBeg); line = lineIter.next().trim();
if ( lineEnd < 0 ) {
lineEnd = text.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
pos = line.indexOf('# '); pos = line.indexOf('# ');
if ( pos !== -1 ) { if ( pos !== -1 ) {
line = line.slice(0, pos).trim(); line = line.slice(0, pos).trim();
@ -325,7 +318,12 @@ HnSwitches.prototype.fromString = function(text) {
continue; continue;
} }
hostname = punycode.toASCII(fields[1]); // Performance: avoid punycoding if hostname is made only of
// ASCII characters.
hostname = fields[1];
if ( reNotASCII.test(hostname) ) {
hostname = toASCII(hostname);
}
state = fields[2]; state = fields[2];
if ( nameToSwitchStateMap.hasOwnProperty(state) === false ) { if ( nameToSwitchStateMap.hasOwnProperty(state) === false ) {