summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib.rs50
1 files changed, 49 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7d21f9e..87c72fd 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -12,6 +12,7 @@
12 12
13use adblock::blocker::BlockerError as RustBlockerError; 13use adblock::blocker::BlockerError as RustBlockerError;
14use adblock::blocker::BlockerResult as RustBlockerResult; 14use adblock::blocker::BlockerResult as RustBlockerResult;
15use adblock::cosmetic_filter_cache::HostnameSpecificResources as RustHostnameSpecificResources;
15use adblock::engine::Engine as RustEngine; 16use adblock::engine::Engine as RustEngine;
16use failure::Fail; 17use failure::Fail;
17use pyo3::class::PyObjectProtocol; 18use pyo3::class::PyObjectProtocol;
@@ -19,6 +20,7 @@ use pyo3::exceptions::ValueError as PyValueError;
19use pyo3::prelude::*; 20use pyo3::prelude::*;
20use pyo3::PyErr; 21use pyo3::PyErr;
21 22
23use std::collections::HashMap;
22use std::collections::HashSet; 24use std::collections::HashSet;
23use std::fs; 25use std::fs;
24use std::io::{Read, Write}; 26use std::io::{Read, Write};
@@ -112,6 +114,43 @@ impl Into<BlockerError> for RustBlockerError {
112 } 114 }
113} 115}
114 116
117/// Contains cosmetic filter information intended to be injected into a
118/// particular hostname.
119#[pyclass]
120pub struct HostnameSpecificResources {
121 /// A set of any CSS selector on the page that should be hidden, i.e.
122 /// styled as `{ display: none !important; }`.
123 #[pyo3(get)]
124 pub hide_selectors: Vec<String>,
125 /// A map of CSS selectors on the page to respective non-hide style rules,
126 /// i.e. any required styles other than `display: none`.
127 #[pyo3(get)]
128 pub style_selectors: HashMap<String, Vec<String>>,
129 /// A set of any class or id CSS selectors that should not have generic
130 /// rules applied.
131 // In practice, these should be passed to `class_id_stylesheet` and not
132 // used otherwise.
133 #[pyo3(get)]
134 pub exceptions: Vec<String>,
135 /// Javascript code for any scriptlets that should be injected into the
136 /// page.
137 #[pyo3(get)]
138 pub injected_script: String,
139}
140
141impl Into<HostnameSpecificResources> for RustHostnameSpecificResources {
142 fn into(self) -> HostnameSpecificResources {
143 let hide_selectors = Vec::from_iter(self.hide_selectors.into_iter());
144 let exceptions = Vec::from_iter(self.exceptions.into_iter());
145 HostnameSpecificResources {
146 hide_selectors,
147 style_selectors: self.style_selectors,
148 exceptions,
149 injected_script: self.injected_script,
150 }
151 }
152}
153
115#[pyclass] 154#[pyclass]
116pub struct Engine { 155pub struct Engine {
117 engine: RustEngine, 156 engine: RustEngine,
@@ -166,6 +205,7 @@ impl Engine {
166 blocker_result.into() 205 blocker_result.into()
167 } 206 }
168 207
208 #[allow(clippy::too_many_arguments)]
169 pub fn check_network_urls_with_hostnames_subset( 209 pub fn check_network_urls_with_hostnames_subset(
170 &self, 210 &self,
171 url: &str, 211 url: &str,
@@ -213,7 +253,7 @@ impl Engine {
213 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> { 253 pub fn deserialize(&mut self, serialized: &[u8]) -> PyResult<()> {
214 let result = self.engine.deserialize(serialized); 254 let result = self.engine.deserialize(serialized);
215 match result { 255 match result {
216 Ok(x) => Ok(x), 256 Ok(_) => Ok(()),
217 Err(error) => { 257 Err(error) => {
218 let my_blocker_error: BlockerError = error.into(); 258 let my_blocker_error: BlockerError = error.into();
219 Err(my_blocker_error.into()) 259 Err(my_blocker_error.into())
@@ -248,6 +288,14 @@ impl Engine {
248 self.engine.tag_exists(tag) 288 self.engine.tag_exists(tag)
249 } 289 }
250 290
291 /// Returns a set of cosmetic filter resources required for a particular
292 /// hostname. Once this has been called, all CSS ids and classes on a
293 /// page should be passed to hidden_class_id_selectors to obtain any
294 /// stylesheets consisting of generic rules.
295 pub fn hostname_cosmetic_resources(&self, hostname: &str) -> HostnameSpecificResources {
296 self.engine.hostname_cosmetic_resources(hostname).into()
297 }
298
251 /// If any of the provided CSS classes or ids could cause a certain generic 299 /// If any of the provided CSS classes or ids could cause a certain generic
252 /// CSS hide rule (i.e. `{ display: none !important; }`) to be required, this 300 /// CSS hide rule (i.e. `{ display: none !important; }`) to be required, this
253 /// method will return a list of CSS selectors corresponding to rules 301 /// method will return a list of CSS selectors corresponding to rules