Commit 80ce7258 authored by Manish Jethani's avatar Manish Jethani

Issue 7094 - Keep subscription filters by text only

parent f5751c81
Pipeline #48321378 passed with stage
in 38 seconds
......@@ -32,7 +32,7 @@ const {ElemHideEmulation} = require("./elemHideEmulation");
const {ElemHideExceptions} = require("./elemHideExceptions");
const {Snippets} = require("./snippets");
const {defaultMatcher} = require("./matcher");
const {ActiveFilter, RegExpFilter,
const {Filter, ActiveFilter, RegExpFilter,
ElemHideBase, ElemHideFilter, ElemHideEmulationFilter,
SnippetFilter} = require("./filterClasses");
const {SpecialSubscription} = require("./subscriptionClasses");
......@@ -223,8 +223,8 @@ function onSubscriptionAdded(subscription)
if (!subscription.disabled)
{
for (let filter of subscription.filters())
addFilter(filter);
for (let text of subscription.filterText())
addFilter(Filter.fromText(text));
}
}
......@@ -234,8 +234,8 @@ function onSubscriptionRemoved(subscription)
if (!subscription.disabled)
{
for (let filter of subscription.filters())
removeFilter(filter);
for (let text of subscription.filterText())
removeFilter(Filter.fromText(text));
}
}
......@@ -247,29 +247,29 @@ function onSubscriptionDisabled(subscription, newValue)
{
if (newValue == false)
{
for (let filter of subscription.filters())
addFilter(filter);
for (let text of subscription.filterText())
addFilter(Filter.fromText(text));
}
else
{
for (let filter of subscription.filters())
removeFilter(filter);
for (let text of subscription.filterText())
removeFilter(Filter.fromText(text));
}
}
}
function onSubscriptionUpdated(subscription, oldFilters)
function onSubscriptionUpdated(subscription, oldFilterText)
{
FilterListener.setDirty(1);
if (!subscription.disabled &&
filterStorage.knownSubscriptions.has(subscription.url))
{
for (let filter of oldFilters)
removeFilter(filter);
for (let text of oldFilterText)
removeFilter(Filter.fromText(text));
for (let filter of subscription.filters())
addFilter(filter);
for (let text of subscription.filterText())
addFilter(Filter.fromText(text));
}
}
......@@ -331,8 +331,8 @@ function onLoad()
{
if (!subscription.disabled)
{
for (let filter of subscription.filters())
addFilter(filter);
for (let text of subscription.filterText())
addFilter(Filter.fromText(text));
}
}
}
......
......@@ -193,20 +193,20 @@ class FilterStorage
/**
* Replaces the list of filters in a subscription with a new list.
* @param {Subscription} subscription The subscription to be updated.
* @param {Array.<Filter>} filters The new list of filters.
* @param {Array.<string>} filterText The new filter text.
*/
updateSubscriptionFilters(subscription, filters)
updateSubscriptionFilters(subscription, filterText)
{
let oldFilters = [...subscription.filters()];
disconnectSubscriptionFilters(subscription, oldFilters);
let oldFilterText = [...subscription.filterText()];
disconnectSubscriptionFilters(subscription, oldFilterText);
subscription.clearFilters();
for (let filter of filters)
subscription.addFilter(filter);
for (let text of filterText)
subscription.addFilterText(text);
connectSubscriptionFilters(subscription, filters);
connectSubscriptionFilters(subscription, filterText);
filterNotifier.emit("subscription.updated", subscription, oldFilters);
filterNotifier.emit("subscription.updated", subscription, oldFilterText);
}
/**
......@@ -284,8 +284,9 @@ class FilterStorage
for (let j = positions.length - 1; j >= 0; j--)
{
let currentPosition = positions[j];
let currentFilter = currentSubscription.filterAt(currentPosition);
if (currentFilter && currentFilter.text == filter.text)
let currentFilterText =
currentSubscription.filterTextAt(currentPosition);
if (currentFilterText && currentFilterText == filter.text)
{
currentSubscription.deleteFilterAt(currentPosition);
if (currentSubscription.searchFilter(filter) < 0)
......@@ -311,8 +312,8 @@ class FilterStorage
if (!(subscription instanceof SpecialSubscription))
return;
let currentFilter = subscription.filterAt(oldPosition);
if (!currentFilter || currentFilter.text != filter.text)
let currentFilterText = subscription.filterTextAt(oldPosition);
if (!currentFilterText || currentFilterText != filter.text)
return;
newPosition = Math.min(Math.max(newPosition, 0),
......@@ -504,12 +505,12 @@ class FilterStorage
// Save filter data
for (let subscription of subscriptions)
{
for (let filter of subscription.filters())
for (let text of subscription.filterText())
{
if (!saved.has(filter.text))
if (!saved.has(text))
{
yield* filter.serialize();
saved.add(filter.text);
yield* Filter.fromText(text).serialize();
saved.add(text);
}
}
}
......@@ -654,32 +655,32 @@ exports.filterStorage = filterStorage;
* Connects a subscription to its filters without any notifications.
* @param {Subscription} subscription The subscription that should be
* connected to its filters.
* @param {?Array.<Filter>} [filters] A list of filters to which the
* subscription should be connected. If this is not given, the subscription
* is connected to its own filters.
* @param {?Array.<string>} [filterText] A list of filters (in text form) to
* which the subscription should be connected. If this is not given, the
* subscription is connected to its own filters.
*/
function connectSubscriptionFilters(subscription, filters)
function connectSubscriptionFilters(subscription, filterText)
{
if (!filterStorage.knownSubscriptions.has(subscription.url))
return;
for (let filter of filters || subscription.filters())
filter.addSubscription(subscription);
for (let text of filterText || subscription.filterText())
Filter.fromText(text).addSubscription(subscription);
}
/**
* Disconnects a subscription from its filters without any notifications.
* @param {Subscription} subscription The subscription that should be
* disconnected from its filters.
* @param {?Array.<Filter>} [filters] A list of filters from which the
* subscription should be disconnected. If this is not given, the
* @param {?Array.<string>} [filterText] A list of filters (in text form) from
* which the subscription should be disconnected. If this is not given, the
* subscription is disconnected from its own filters.
*/
function disconnectSubscriptionFilters(subscription, filters)
function disconnectSubscriptionFilters(subscription, filterText)
{
if (!filterStorage.knownSubscriptions.has(subscription.url))
return;
for (let filter of filters || subscription.filters())
filter.removeSubscription(subscription);
for (let text of filterText || subscription.filterText())
Filter.fromText(text).removeSubscription(subscription);
}
......@@ -107,11 +107,7 @@ class INIParser
this.subscriptions.length - 1
];
for (let text of this._curObj)
{
let filter = Filter.fromText(text);
currentSubscription.addFilter(filter);
filter.addSubscription(currentSubscription);
}
currentSubscription.addFilterText(text);
}
break;
}
......
......@@ -38,7 +38,6 @@ function Subscription(url, title)
this.url = url;
this._filterText = [];
this._filters = [];
if (title)
this._title = title;
......@@ -68,13 +67,6 @@ Subscription.prototype =
*/
_filterText: null,
/**
* {@link Filter} objects corresponding to the subscription's filter text.
* @type {Array.<Filter>}
* @private
*/
_filters: null,
_title: null,
_fixedTitle: false,
_disabled: false,
......@@ -142,7 +134,7 @@ Subscription.prototype =
*/
get filterCount()
{
return this._filters.length;
return this._filterText.length;
},
/**
......@@ -155,22 +147,13 @@ Subscription.prototype =
},
/**
* Yields the {@link Filter} object for each filter in the subscription.
* @yields {Filter}
*/
*filters()
{
yield* this._filters;
},
/**
* Returns the {@link Filter} object at the given 0-based index.
* Returns the filter text at the given 0-based index.
* @param {number} index
* @returns {?Filter}
*/
filterAt(index)
filterTextAt(index)
{
return this._filters[index] || null;
return this._filterText[index] || null;
},
/**
......@@ -190,7 +173,6 @@ Subscription.prototype =
clearFilters()
{
this._filterText = [];
this._filters = [];
},
/**
......@@ -200,7 +182,15 @@ Subscription.prototype =
addFilter(filter)
{
this._filterText.push(filter.text);
this._filters.push(filter);
},
/**
* Adds a filter to the subscription.
* @param {string} filterText
*/
addFilterText(filterText)
{
this._filterText.push(filterText);
},
/**
......@@ -211,7 +201,6 @@ Subscription.prototype =
insertFilterAt(filter, index)
{
this._filterText.splice(index, 0, filter.text);
this._filters.splice(index, 0, filter);
},
/**
......@@ -225,7 +214,6 @@ Subscription.prototype =
return;
this._filterText.splice(index, 1);
this._filters.splice(index, 1);
},
/**
......
......@@ -259,15 +259,15 @@ class Synchronizer
// Process filters
lines.shift();
let filters = [];
let filterText = [];
for (let line of lines)
{
line = Filter.normalize(line);
if (line)
filters.push(Filter.fromText(line));
filterText.push(line);
}
filterStorage.updateSubscriptionFilters(subscription, filters);
filterStorage.updateSubscriptionFilters(subscription, filterText);
}
_onDownloadError(downloadable, downloadURL, error, channelStatus,
......
......@@ -224,13 +224,13 @@ exports.testFilterSubscriptionOperations = function(test)
filterStorage.addFilter(filter1);
checkKnownFilters(test, "add filter1", {blacklist: [filter1.text], whitelist: [filter2.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filter7.text], elemhideemulation: [filter6.text]});
filterStorage.updateSubscriptionFilters(subscription, [filter4]);
filterStorage.updateSubscriptionFilters(subscription, [filter4.text]);
checkKnownFilters(test, "change subscription filters to filter4", {blacklist: [filter1.text]});
filterStorage.removeFilter(filter1);
checkKnownFilters(test, "remove filter1", {});
filterStorage.updateSubscriptionFilters(subscription, [filter1, filter2]);
filterStorage.updateSubscriptionFilters(subscription, [filter1.text, filter2.text]);
checkKnownFilters(test, "change subscription filters to filter1, filter2", {blacklist: [filter1.text], whitelist: [filter2.text]});
filter1.disabled = true;
......@@ -320,13 +320,13 @@ exports.testFilterGroupOperations = function(test)
test.ok([...filter2.subscriptions()][0] == subscription3, "@@filter2 added to the default exceptions group");
let subscription4 = Subscription.fromURL("http://test/");
filterStorage.updateSubscriptionFilters(subscription4, [filter3, filter4, filter5]);
filterStorage.updateSubscriptionFilters(subscription4, [filter3.text, filter4.text, filter5.text]);
checkKnownFilters(test, "update subscription not in the list yet", {blacklist: [filter1.text], whitelist: [filter2.text]});
filterStorage.addSubscription(subscription4);
checkKnownFilters(test, "add subscription to the list", {blacklist: [filter1.text, filter3.text], whitelist: [filter2.text, filter4.text]});
filterStorage.updateSubscriptionFilters(subscription4, [filter3, filter2, filter5]);
filterStorage.updateSubscriptionFilters(subscription4, [filter3.text, filter2.text, filter5.text]);
checkKnownFilters(test, "update subscription while in the list", {blacklist: [filter1.text, filter3.text], whitelist: [filter2.text]});
subscription3.disabled = true;
......
......@@ -498,7 +498,7 @@ exports.testFilterSubscriptionRelationship = function(test)
compareFilterSubscriptions(test, "filter2 subscriptions after removing http://test1/", filter2, [subscription2]);
compareFilterSubscriptions(test, "filter3 subscriptions after removing http://test1/", filter3, [subscription2]);
filterStorage.updateSubscriptionFilters(subscription3, [filter3]);
filterStorage.updateSubscriptionFilters(subscription3, [filter3.text]);
compareFilterSubscriptions(test, "filter1 subscriptions after updating http://test3/ filters", filter1, []);
compareFilterSubscriptions(test, "filter2 subscriptions after updating http://test3/ filters", filter2, [subscription2]);
......@@ -510,7 +510,7 @@ exports.testFilterSubscriptionRelationship = function(test)
compareFilterSubscriptions(test, "filter2 subscriptions after adding http://test3/", filter2, [subscription2]);
compareFilterSubscriptions(test, "filter3 subscriptions after adding http://test3/", filter3, [subscription2, subscription3]);
filterStorage.updateSubscriptionFilters(subscription3, [filter1, filter2]);
filterStorage.updateSubscriptionFilters(subscription3, [filter1.text, filter2.text]);
compareFilterSubscriptions(test, "filter1 subscriptions after updating http://test3/ filters", filter1, [subscription3]);
compareFilterSubscriptions(test, "filter2 subscriptions after updating http://test3/ filters", filter2, [subscription2, subscription3]);
......
......@@ -65,15 +65,14 @@ function compareSubscription(test, url, expected, postInit)
function compareSubscriptionFilters(test, subscription, expected)
{
test.deepEqual([...subscription.filterText()], expected);
test.deepEqual([...subscription.filters()], expected.map(f$));
test.equal(subscription.filterCount, expected.length);
for (let i = 0; i < subscription.filterCount; i++)
test.equal(subscription.filterAt(i).text, expected[i]);
test.equal(subscription.filterTextAt(i), expected[i]);
test.ok(!subscription.filterAt(subscription.filterCount));
test.ok(!subscription.filterAt(-1));
test.ok(!subscription.filterTextAt(subscription.filterCount));
test.ok(!subscription.filterTextAt(-1));
}
exports.testSubscriptionClassDefinitions = function(test)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment