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. 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 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
@ -117,9 +117,8 @@ const handleImportFilePicker = function() {
let matches = reAbpSubscriptionExtractor.exec(s); let matches = reAbpSubscriptionExtractor.exec(s);
// Not an ABP backup file // Not an ABP backup file
if ( matches === null ) { return s; } if ( matches === null ) { return s; }
//
const out = []; const out = [];
while ( matches !== null ) { do {
if ( matches.length === 2 ) { if ( matches.length === 2 ) {
let filterMatch = reAbpFilterExtractor.exec(matches[1].trim()); let filterMatch = reAbpFilterExtractor.exec(matches[1].trim());
if ( filterMatch !== null && filterMatch.length === 2 ) { if ( filterMatch !== null && filterMatch.length === 2 ) {
@ -127,13 +126,22 @@ const handleImportFilePicker = function() {
} }
} }
matches = reAbpSubscriptionExtractor.exec(s); matches = reAbpSubscriptionExtractor.exec(s);
} } while ( matches !== null );
return out.join('\n'); return out.join('\n');
}; };
const fileReaderOnLoadHandler = function() { const fileReaderOnLoadHandler = function() {
const sanitized = abpImporter(this.result); let content = abpImporter(this.result);
cmEditor.setValue(cmEditor.getValue().trim() + '\n' + sanitized); 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]; const file = this.files[0];
if ( file === undefined || file.name === '' ) { return; } if ( file === undefined || file.name === '' ) { return; }

View file

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