From f0d5205bd75474f9853abcc1f9c165d42498527f Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 20 Apr 2019 18:57:16 -0400 Subject: [PATCH] Discard existing lines when importing from file in "My filters" Related issue: - https://github.com/uBlockOrigin/uBlock-issues/issues/519 --- src/js/1p-filters.js | 20 ++++++++++++++------ src/js/dashboard-common.js | 24 +++++++++++++----------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/js/1p-filters.js b/src/js/1p-filters.js index e1cee3bda..9c756fdc8 100644 --- a/src/js/1p-filters.js +++ b/src/js/1p-filters.js @@ -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; } diff --git a/src/js/dashboard-common.js b/src/js/dashboard-common.js index 14ad39d51..b97579bf4 100644 --- a/src/js/dashboard-common.js +++ b/src/js/dashboard-common.js @@ -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; }; /******************************************************************************/