Discard existing lines when importing from file in "My filters"

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/519
This commit is contained in:
Raymond Hill 2019-04-20 18:57:16 -04:00
parent 1de75ced5c
commit f0d5205bd7
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 27 additions and 17 deletions

View File

@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2014-2018 Raymond Hill
Copyright (C) 2014-present 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
@ -117,9 +117,8 @@ const handleImportFilePicker = function() {
let matches = reAbpSubscriptionExtractor.exec(s);
// Not an ABP backup file
if ( matches === null ) { return s; }
//
const out = [];
while ( matches !== null ) {
do {
if ( matches.length === 2 ) {
let filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
if ( filterMatch !== null && filterMatch.length === 2 ) {
@ -127,13 +126,22 @@ const handleImportFilePicker = function() {
}
}
matches = reAbpSubscriptionExtractor.exec(s);
}
} while ( matches !== null );
return out.join('\n');
};
const fileReaderOnLoadHandler = function() {
const sanitized = abpImporter(this.result);
cmEditor.setValue(cmEditor.getValue().trim() + '\n' + sanitized);
let content = abpImporter(this.result);
content = uBlockDashboard.mergeNewLines(
cmEditor.getValue().trim(),
content
);
cmEditor.operation(( ) => {
const cmPos = cmEditor.getCursor();
cmEditor.setValue(`${content}\n`);
cmEditor.setCursor(cmPos);
cmEditor.focus();
});
};
const file = this.files[0];
if ( file === undefined || file.name === '' ) { return; }

View File

@ -34,7 +34,7 @@ self.uBlockDashboard = self.uBlockDashboard || {};
self.uBlockDashboard.mergeNewLines = function(text, newText) {
// Step 1: build dictionary for existing lines.
const fromDict = Object.create(null);
const fromDict = new Map();
let lineBeg = 0;
let textEnd = text.length;
while ( lineBeg < textEnd ) {
@ -45,17 +45,15 @@ self.uBlockDashboard.mergeNewLines = function(text, newText) {
lineEnd = textEnd;
}
}
let line = text.slice(lineBeg, lineEnd).trim();
const line = text.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
if ( line.length === 0 ) {
continue;
}
if ( line.length === 0 ) { continue; }
const hash = line.slice(0, 8);
const bucket = fromDict[hash];
const bucket = fromDict.get(hash);
if ( bucket === undefined ) {
fromDict[hash] = line;
fromDict.set(hash, line);
} else if ( typeof bucket === 'string' ) {
fromDict[hash] = [bucket, line];
fromDict.set(hash, [ bucket, line ]);
} else /* if ( Array.isArray(bucket) ) */ {
bucket.push(line);
}
@ -73,7 +71,7 @@ self.uBlockDashboard.mergeNewLines = function(text, newText) {
lineEnd = textEnd;
}
}
let line = newText.slice(lineBeg, lineEnd).trim();
const line = newText.slice(lineBeg, lineEnd).trim();
lineBeg = lineEnd + 1;
if ( line.length === 0 ) {
if ( out[out.length - 1] !== '' ) {
@ -81,7 +79,7 @@ self.uBlockDashboard.mergeNewLines = function(text, newText) {
}
continue;
}
const bucket = fromDict[line.slice(0, 8)];
const bucket = fromDict.get(line.slice(0, 8));
if ( bucket === undefined ) {
out.push(line);
continue;
@ -96,7 +94,11 @@ self.uBlockDashboard.mergeNewLines = function(text, newText) {
}
}
return text.trim() + '\n' + out.join('\n');
const append = out.join('\n').trim();
if ( text !== '' && append !== '' ) {
text += '\n\n';
}
return text + append;
};
/******************************************************************************/