Index: compiled/ElemHide.h
===================================================================
--- a/compiled/ElemHide.h
+++ b/compiled/ElemHide.h
@@ -92,15 +92,15 @@
void BINDINGS_EXPORTED Clear();
void BINDINGS_EXPORTED Add(ElemHideBase& filter);
void BINDINGS_EXPORTED Remove(ElemHideBase& filter);
ElemHide_SelectorList* BINDINGS_EXPORTED GetSelectorsForDomain(const String& domain,
Criteria criteria) const;
ElemHide_SelectorList* BINDINGS_EXPORTED GetUnconditionalSelectors() const;
- ElemHideException* BINDINGS_EXPORTED GetException(const ElemHideBase& filter,
- DependentString& docDomain) const;
+ ElemHideException* GetException(const ElemHideBase& filter,
+ DependentString& docDomain) const;
private:
void AddToFiltersByDomain(const ElemHideBasePtr & filter);
};
Index: compiled/ElemHideEmulation.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/ElemHideEmulation.cpp
@@ -0,0 +1,49 @@
+/*
+ * This file is part of Adblock Plus ,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see .
+ */
+
+#include "ElemHide.h"
+#include "ElemHideEmulation.h"
+
+void ElemHideEmulation::Add(ElemHideBase& filter)
+{
+ mFilters[filter.GetText()] = ElemHideBasePtr(&filter);
+}
+
+void ElemHideEmulation::Remove(ElemHideBase& filter)
+{
+ mFilters.erase(filter.GetText());
+}
+
+void ElemHideEmulation::Clear()
+{
+ mFilters.clear();
+}
+
+ElemHideEmulation_FilterList* ElemHideEmulation::GetRulesForDomain(const ElemHide& elemHide, DependentString& domain)
+{
+ intrusive_ptr result(new ElemHideEmulation_FilterList());
+ for (const auto& entry: mFilters)
+ {
+ DependentString docDomain(domain);
+ if (!(entry.is_deleted() || entry.is_invalid()) &&
+ entry.second->IsActiveOnDomain(docDomain) &&
+ !elemHide.GetException(*entry.second, domain))
+ result->push_back(entry.second);
+ }
+
+ return result.release();
+}
Index: compiled/ElemHideEmulation.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/ElemHideEmulation.h
@@ -0,0 +1,67 @@
+/*
+ * This file is part of Adblock Plus ,
+ * Copyright (C) 2006-present eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see .
+ */
+
+#pragma once
+
+#include
+
+#include "bindings/runtime.h"
+#include "intrusive_ptr.h"
+#include "filter/ElemHideBase.h"
+
+class ElemHide;
+
+class ElemHideEmulation_FilterList : public ref_counted
+{
+ std::vector mFilters;
+public:
+ size_t BINDINGS_EXPORTED GetFilterCount() const
+ {
+ return mFilters.size();
+ }
+
+ ElemHideBase* BINDINGS_EXPORTED FilterAt(size_t index)
+ {
+ if (index >= mFilters.size())
+ return nullptr;
+
+ ElemHideBasePtr result(mFilters[index]);
+ return result.release();
+ }
+
+ void push_back(const ElemHideBasePtr& filter)
+ {
+ mFilters.push_back(filter);
+ }
+
+};
+
+class ElemHideEmulation : public ref_counted
+{
+ StringMap mFilters;
+
+public:
+ static ElemHideEmulation* BINDINGS_EXPORTED Create()
+ {
+ return new ElemHideEmulation();
+ }
+
+ void BINDINGS_EXPORTED Add(ElemHideBase&);
+ void BINDINGS_EXPORTED Remove(ElemHideBase&);
+ void BINDINGS_EXPORTED Clear();
+ ElemHideEmulation_FilterList* BINDINGS_EXPORTED GetRulesForDomain(const ElemHide&, DependentString&);
+};
Index: compiled/bindings/main.cpp
===================================================================
--- a/compiled/bindings/main.cpp
+++ b/compiled/bindings/main.cpp
@@ -30,16 +30,17 @@
#include "../filter/ElemHideFilter.h"
#include "../filter/ElemHideException.h"
#include "../filter/ElemHideEmulationFilter.h"
#include "../subscription/Subscription.h"
#include "../subscription/DownloadableSubscription.h"
#include "../subscription/UserDefinedSubscription.h"
#include "../storage/FilterStorage.h"
#include "../ElemHide.h"
+#include "../ElemHideEmulation.h"
#include "../FilterNotifier.h"
int main()
{
try
{
class_("Filter")
.property("text", &Filter::GetText)
@@ -148,20 +149,30 @@
.function("selectorAt", &ElemHide_SelectorList::SelectorAt)
.function("filterKeyAt", &ElemHide_SelectorList::FilterKeyAt);
class_("ElemHide")
.class_function("create", &ElemHide::Create)
.function("add", &ElemHide::Add)
.function("remove", &ElemHide::Remove)
.function("clear", &ElemHide::Clear)
- .function("getException", &ElemHide::GetException)
.function("getSelectorsForDomain", &ElemHide::GetSelectorsForDomain)
.function("getUnconditionalSelectors", &ElemHide::GetUnconditionalSelectors);
+ class_("ElemHideEmulation_FilterList")
+ .property("filterCount", &ElemHideEmulation_FilterList::GetFilterCount)
+ .function("filterAt", &ElemHideEmulation_FilterList::FilterAt);
+
+ class_("ElemHideEmulation")
+ .class_function("create", &ElemHideEmulation::Create)
+ .function("add", &ElemHideEmulation::Add)
+ .function("remove", &ElemHideEmulation::Remove)
+ .function("clear", &ElemHideEmulation::Clear)
+ .function("getRulesForDomain", &ElemHideEmulation::GetRulesForDomain);
+
printBindings();
RegExpFilter::GenerateCustomBindings();
FilterNotifier::GenerateCustomBindings();
return 0;
}
catch (const std::exception& e)
{
Index: lib/elemHideEmulation.js
===================================================================
--- a/lib/elemHideEmulation.js
+++ b/lib/elemHideEmulation.js
@@ -12,70 +12,11 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see .
*/
"use strict";
-/**
- * @fileOverview Element hiding emulation implementation.
- */
-
-const {Filter} = require("filterClasses");
-
-let filters = Object.create(null);
-
-/**
- * Container for element hiding emulation filters
- * @class
- */
-let ElemHideEmulation = {
- /**
- * Removes all known filters
- */
- clear()
- {
- filters = Object.create(null);
- },
-
- /**
- * Add a new element hiding emulation filter
- * @param {ElemHideEmulationFilter} filter
- */
- add(filter)
- {
- filters[filter.text] = true;
- },
+const {ElemHideEmulation} = require("compiled");
- /**
- * Removes an element hiding emulation filter
- * @param {ElemHideEmulationFilter} filter
- */
- remove(filter)
- {
- delete filters[filter.text];
- },
-
- /**
- * Returns a list of all rules active on a particular domain
- * @param {string} domain
- * @param {Object} elemHide the ElemHide instance
- * @return {ElemHideEmulationFilter[]}
- */
- getRulesForDomain(domain, elemHide)
- {
- let result = [];
- let keys = Object.getOwnPropertyNames(filters);
- for (let key of keys)
- {
- let filter = Filter.fromText(key);
- if (filter.isActiveOnDomain(domain) &&
- !elemHide.getException(filter, domain))
- {
- result.push(filter);
- }
- }
- return result;
- }
-};
exports.ElemHideEmulation = ElemHideEmulation;
Index: meson.build
===================================================================
--- a/meson.build
+++ b/meson.build
@@ -83,16 +83,17 @@
'compiled/filter/InvalidFilter.cpp',
'compiled/filter/RegExpFilter.cpp',
'compiled/filter/WhitelistFilter.cpp',
'compiled/storage/FilterStorage.cpp',
'compiled/subscription/DownloadableSubscription.cpp',
'compiled/subscription/Subscription.cpp',
'compiled/subscription/UserDefinedSubscription.cpp',
'compiled/ElemHide.cpp',
+ 'compiled/ElemHideEmulation.cpp',
]
# sources specific to core
core_sources = [
'compiled/traceInit.cpp',
]
# sources for the bindings generator
bindings_sources = [
'compiled/bindings/generator.cpp',
Index: test/elemHideEmulation.js
===================================================================
--- a/test/elemHideEmulation.js
+++ b/test/elemHideEmulation.js
@@ -137,35 +137,42 @@
test.done();
};
exports.testDomainRestrictions = function(test)
{
function testSelectorMatches(description, filters, domain, expectedMatches)
{
- withNAD(0, elemHide =>
+ withNAD([0, 1], (elemHide, elemHideEmulation) =>
{
let addFilter = withNAD(0, filter =>
{
if (filter instanceof ElemHideEmulationFilter)
- ElemHideEmulation.add(filter);
+ elemHideEmulation.add(filter);
else
elemHide.add(filter);
});
for (let text of filters)
addFilter(Filter.fromText(text));
- let matches = ElemHideEmulation.getRulesForDomain(domain, elemHide)
- .map(filter => filter.text);
- test.deepEqual(matches.sort(), expectedMatches.sort(), description);
+ withNAD(0, rules =>
+ {
+ let matches = [];
+ let push = withNAD(0, filter => matches.push(filter.text));
- ElemHideEmulation.clear();
- })(ElemHide.create());
+ for (let i = 0; i < rules.filterCount; i++)
+ push(rules.filterAt(i));
+
+ test.deepEqual(matches.sort(), expectedMatches.sort(), description);
+ })(elemHideEmulation.getRulesForDomain(elemHide, domain));
+
+ elemHideEmulation.clear();
+ })(ElemHide.create(), ElemHideEmulation.create());
}
testSelectorMatches(
"Ignore generic filters",
[
"##[-abp-properties='foo']", "example.com##[-abp-properties='foo']",
"~example.com##[-abp-properties='foo']"
],
@@ -209,49 +216,54 @@
["other.example.com##[-abp-properties='foo']"]
);
test.done();
};
exports.testElemHideEmulationFiltersContainer = function(test)
{
- withNAD(0, elemHide =>
+ withNAD([0, 1], (elemHide, elemHideEmulation) =>
{
function compareRules(description, domain, expectedMatches)
{
- let result = ElemHideEmulation.getRulesForDomain(domain, elemHide)
- .map(filter => filter.text);
- expectedMatches = expectedMatches.map(filter => filter.text);
- test.deepEqual(result.sort(), expectedMatches.sort(), description);
+ withNAD(0, rules =>
+ {
+ let result = [];
+ for (let i = 0; i < rules.filterCount; i++)
+ withNAD(0, filter => result.push(filter.text))(rules.filterAt(i));
+
+ expectedMatches = expectedMatches.map(filter => filter.text);
+ test.deepEqual(result.sort(), expectedMatches.sort(), description);
+ })(elemHideEmulation.getRulesForDomain(elemHide, domain));
}
withNAD([0, 1, 2], (domainFilter, subdomainFilter, otherDomainFilter) =>
{
- ElemHideEmulation.add(domainFilter);
- ElemHideEmulation.add(subdomainFilter);
- ElemHideEmulation.add(otherDomainFilter);
+ elemHideEmulation.add(domainFilter);
+ elemHideEmulation.add(subdomainFilter);
+ elemHideEmulation.add(otherDomainFilter);
compareRules(
"Return all matching filters",
"www.example.com",
[domainFilter, subdomainFilter]
);
- ElemHideEmulation.remove(domainFilter);
+ elemHideEmulation.remove(domainFilter);
compareRules(
"Return all matching filters after removing one",
"www.example.com",
[subdomainFilter]
);
- ElemHideEmulation.clear();
+ elemHideEmulation.clear();
compareRules(
"Return no filters after clearing",
"www.example.com",
[]
);
})(Filter.fromText("example.com##filter1"),
Filter.fromText("www.example.com##filter2"),
Filter.fromText("other.example.com##filter3"));
- })(ElemHide.create());
+ })(ElemHide.create(), ElemHideEmulation.create());
test.done();
};