1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-14 23:12:28 +02: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.
Copyright (C) 2014-2016 Raymond Hill
Copyright (C) 2014-2017 Raymond Hill
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
@ -444,9 +444,10 @@ Matrix.prototype.desHostnameFromRule = function(rule) {
/******************************************************************************/
Matrix.prototype.toString = function() {
var out = [];
var rule, type, val;
var srcHostname, desHostname;
var out = [],
rule, type, val,
srcHostname, desHostname,
toUnicode = punycode.toUnicode;
for ( rule in this.rules ) {
if ( this.rules.hasOwnProperty(rule) === false ) {
continue;
@ -458,12 +459,16 @@ Matrix.prototype.toString = function() {
continue;
}
val = this.evaluateCell(srcHostname, desHostname, type);
if ( val === 0 ) {
continue;
if ( val === 0 ) { continue; }
if ( srcHostname.indexOf('xn--') !== -1 ) {
srcHostname = toUnicode(srcHostname);
}
if ( desHostname.indexOf('xn--') !== -1 ) {
desHostname = toUnicode(desHostname);
}
out.push(
punycode.toUnicode(srcHostname) + ' ' +
punycode.toUnicode(desHostname) + ' ' +
srcHostname + ' ' +
desHostname + ' ' +
type + ' ' +
actionToNameMap[val]
);
@ -475,26 +480,18 @@ Matrix.prototype.toString = function() {
/******************************************************************************/
Matrix.prototype.fromString = function(text, append) {
var textEnd = text.length;
var lineBeg = 0, lineEnd;
var line, pos, fields;
var srcHostname, desHostname, type, action;
var lineIter = new µBlock.LineIterator(text),
line, pos, fields,
srcHostname, desHostname, type, action,
reNotASCII = /[^\x20-\x7F]/,
toASCII = punycode.toASCII;
if ( append !== true ) {
this.reset();
}
while ( lineBeg < textEnd ) {
lineEnd = text.indexOf('\n', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = text.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
while ( lineIter.eot() === false ) {
line = lineIter.next().trim();
pos = line.indexOf('# ');
if ( pos !== -1 ) {
line = line.slice(0, pos).trim();
@ -527,8 +524,16 @@ Matrix.prototype.fromString = function(text, append) {
continue;
}
srcHostname = punycode.toASCII(fields[0]);
desHostname = punycode.toASCII(fields[1]);
// Performance: avoid punycoding if hostnames are made only of
// 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
// Discard rules with invalid hostnames

View File

@ -1,7 +1,7 @@
/*******************************************************************************
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
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() {
var out = [];
var switchName, val;
var hostname;
var out = [],
switchName, val,
hostname,
toUnicode = punycode.toUnicode;
for ( hostname in this.switches ) {
if ( this.switches.hasOwnProperty(hostname) === false ) {
continue;
@ -270,8 +271,9 @@ HnSwitches.prototype.toString = function() {
continue;
}
val = this.evaluate(switchName, hostname);
if ( val === 0 ) {
continue;
if ( val === 0 ) { continue; }
if ( hostname.indexOf('xn--') !== -1 ) {
hostname = toUnicode(hostname);
}
out.push(switchName + ': ' + hostname + ' ' + switchStateToNameMap[val]);
}
@ -282,25 +284,16 @@ HnSwitches.prototype.toString = function() {
/******************************************************************************/
HnSwitches.prototype.fromString = function(text) {
var textEnd = text.length;
var lineBeg = 0, lineEnd;
var line, pos;
var fields;
var switchName, hostname, state;
var lineIter = new µBlock.LineIterator(text),
line, pos, fields,
switchName, hostname, state,
reNotASCII = /[^\x20-\x7F]/,
toASCII = punycode.toASCII;
this.reset();
while ( lineBeg < textEnd ) {
lineEnd = text.indexOf('\n', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = text.indexOf('\r', lineBeg);
if ( lineEnd < 0 ) {
lineEnd = textEnd;
}
}
line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
while ( lineIter.eot() === false ) {
line = lineIter.next().trim();
pos = line.indexOf('# ');
if ( pos !== -1 ) {
line = line.slice(0, pos).trim();
@ -325,7 +318,12 @@ HnSwitches.prototype.fromString = function(text) {
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];
if ( nameToSwitchStateMap.hasOwnProperty(state) === false ) {