diff options
| -rw-r--r-- | src/lib.rs | 50 |
1 files changed, 49 insertions, 1 deletions
| @@ -12,6 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | use adblock::blocker::BlockerError as RustBlockerError; | 13 | use adblock::blocker::BlockerError as RustBlockerError; |
| 14 | use adblock::blocker::BlockerResult as RustBlockerResult; | 14 | use adblock::blocker::BlockerResult as RustBlockerResult; |
| 15 | use adblock::cosmetic_filter_cache::HostnameSpecificResources as RustHostnameSpecificResources; | ||
| 15 | use adblock::engine::Engine as RustEngine; | 16 | use adblock::engine::Engine as RustEngine; |
| 16 | use failure::Fail; | 17 | use failure::Fail; |
| 17 | use pyo3::class::PyObjectProtocol; | 18 | use pyo3::class::PyObjectProtocol; |
| @@ -19,6 +20,7 @@ use pyo3::exceptions::ValueError as PyValueError; | |||
| 19 | use pyo3::prelude::*; | 20 | use pyo3::prelude::*; |
| 20 | use pyo3::PyErr; | 21 | use pyo3::PyErr; |
| 21 | 22 | ||
| 23 | use std::collections::HashMap; | ||
| 22 | use std::collections::HashSet; | 24 | use std::collections::HashSet; |
| 23 | use std::fs; | 25 | use std::fs; |
| 24 | use std::io::{Read, Write}; | 26 | use 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] | ||
| 120 | pub 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 | |||
| 141 | impl 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] |
| 116 | pub struct Engine { | 155 | pub 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 |
