Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return list of selectors instead of a full stylesheet #63

Merged
merged 1 commit into from Jan 10, 2020
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -52,7 +52,7 @@ fn by_classes_ids(c: &mut Criterion) {
let (_, cosmetic_filters) = parse_filters(&rules, false, true, false);
let cfcache = CosmeticFilterCache::new(cosmetic_filters);
let exceptions = Default::default();
b.iter(|| cfcache.class_id_stylesheet(&vec!["ad".to_owned()][..], &vec!["ad".to_owned()][..], &exceptions))
b.iter(|| cfcache.hidden_class_id_selectors(&vec!["ad".to_owned()][..], &vec!["ad".to_owned()][..], &exceptions))
}).with_function("many lists", move |b| {
let rules = rules_from_lists(&vec![
"data/easylist.to/easylist/easylist.txt".to_owned(),
@@ -63,7 +63,7 @@ fn by_classes_ids(c: &mut Criterion) {
let (_, cosmetic_filters) = parse_filters(&rules, false, true, false);
let cfcache = CosmeticFilterCache::new(cosmetic_filters);
let exceptions = Default::default();
b.iter(|| cfcache.class_id_stylesheet(&vec!["ad".to_owned()][..], &vec!["ad".to_owned()][..], &exceptions))
b.iter(|| cfcache.hidden_class_id_selectors(&vec!["ad".to_owned()][..], &vec!["ad".to_owned()][..], &exceptions))
}).with_function("many matching classes and ids", move |b| {
let rules = rules_from_lists(&vec![
"data/easylist.to/easylist/easylist.txt".to_owned(),
@@ -104,7 +104,7 @@ fn by_classes_ids(c: &mut Criterion) {
"header".to_owned(),
"advertisingModule160x600".to_owned(),
];
b.iter(|| cfcache.class_id_stylesheet(&class_list[..], &id_list[..], &exceptions))
b.iter(|| cfcache.hidden_class_id_selectors(&class_list[..], &id_list[..], &exceptions))
})
.throughput(Throughput::Elements(1))
.sample_size(20)
@@ -157,7 +157,7 @@ impl CosmeticFilterCache {
}
}

pub fn class_id_stylesheet(&self, classes: &[String], ids: &[String], exceptions: &HashSet<String>) -> Option<String> {
pub fn hidden_class_id_selectors(&self, classes: &[String], ids: &[String], exceptions: &HashSet<String>) -> Vec<String> {
let mut simple_classes = vec![];
let mut simple_ids = vec![];
let mut complex_selectors = vec![];
@@ -188,16 +188,13 @@ impl CosmeticFilterCache {
});

if simple_classes.is_empty() && simple_ids.is_empty() && complex_selectors.is_empty() {
return None;
return vec![];
}

let stylesheet = simple_classes.into_iter().map(|class| format!(".{}", class))
simple_classes.into_iter().map(|class| format!(".{}", class))
.chain(simple_ids.into_iter().map(|id| format!("#{}", id)))
.chain(complex_selectors.into_iter().cloned())
.collect::<Vec<_>>()
.join(",") + "{display:none !important;}";

Some(stylesheet)
}

pub fn hostname_cosmetic_resources(&self, hostname: &str) -> HostnameSpecificResources {
@@ -575,8 +572,8 @@ mod cosmetic_cache_tests {
}

#[test]
fn matching_class_id_stylesheet() {
let rules = vec![
fn matching_hidden_class_id_selectors() {
let rules = [
"##.a-class",
"###simple-id",
"##.a-class .with .children",
@@ -585,29 +582,29 @@ mod cosmetic_cache_tests {
];
let cfcache = CosmeticFilterCache::new(rules.iter().map(|r| CosmeticFilter::parse(r, false).unwrap()).collect::<Vec<_>>());

let out = cfcache.class_id_stylesheet(&vec!["with".into()], &vec![], &HashSet::default());
assert_eq!(out, None);
let out = cfcache.hidden_class_id_selectors(&["with".into()], &[], &HashSet::default());
assert_eq!(out, Vec::<String>::new());

let out = cfcache.class_id_stylesheet(&vec![], &vec!["with".into()], &HashSet::default());
assert_eq!(out, None);
let out = cfcache.hidden_class_id_selectors(&[], &["with".into()], &HashSet::default());
assert_eq!(out, Vec::<String>::new());

let out = cfcache.class_id_stylesheet(&vec![], &vec!["a-class".into()], &HashSet::default());
assert_eq!(out, None);
let out = cfcache.hidden_class_id_selectors(&[], &["a-class".into()], &HashSet::default());
assert_eq!(out, Vec::<String>::new());

let out = cfcache.class_id_stylesheet(&vec!["simple-id".into()], &vec![], &HashSet::default());
assert_eq!(out, None);
let out = cfcache.hidden_class_id_selectors(&["simple-id".into()], &[], &HashSet::default());
assert_eq!(out, Vec::<String>::new());

let out = cfcache.class_id_stylesheet(&vec!["a-class".into()], &vec![], &HashSet::default());
assert_eq!(out, Some(".a-class,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["a-class".into()], &[], &HashSet::default());
assert_eq!(out, [".a-class", ".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec!["children".into(), "a-class".into()], &vec![], &HashSet::default());
assert_eq!(out, Some(".a-class,.children .including #simple-id,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["children".into(), "a-class".into()], &[], &HashSet::default());
assert_eq!(out, [".a-class", ".children .including #simple-id", ".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec![], &vec!["simple-id".into()], &HashSet::default());
assert_eq!(out, Some("#simple-id{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&[], &["simple-id".into()], &HashSet::default());
assert_eq!(out, ["#simple-id"]);

let out = cfcache.class_id_stylesheet(&vec!["children".into(), "a-class".into()], &vec!["simple-id".into()], &HashSet::default());
assert_eq!(out, Some(".a-class,#simple-id,.children .including #simple-id,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["children".into(), "a-class".into()], &["simple-id".into()], &HashSet::default());
assert_eq!(out, [".a-class", "#simple-id", ".children .including #simple-id", ".a-class .with .children"]);
}

#[test]
@@ -624,25 +621,25 @@ mod cosmetic_cache_tests {
let cfcache = CosmeticFilterCache::new(rules.iter().map(|r| CosmeticFilter::parse(r, false).unwrap()).collect::<Vec<_>>());
let exceptions = cfcache.hostname_cosmetic_resources("example.co.uk").exceptions;

let out = cfcache.class_id_stylesheet(&vec!["a-class".into()], &vec![], &exceptions);
assert_eq!(out, Some(".a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["a-class".into()], &[], &exceptions);
assert_eq!(out, [".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec!["children".into(), "a-class".into()], &vec!["simple-id".into()], &exceptions);
assert_eq!(out, Some("#simple-id,.children .including #simple-id,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["children".into(), "a-class".into()], &["simple-id".into()], &exceptions);
assert_eq!(out, ["#simple-id", ".children .including #simple-id", ".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec![], &vec!["test-element".into()], &exceptions);
assert_eq!(out, Some("#test-element{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&[], &["test-element".into()], &exceptions);
assert_eq!(out, ["#test-element"]);

let exceptions = cfcache.hostname_cosmetic_resources("a1.test.com").exceptions;

let out = cfcache.class_id_stylesheet(&vec!["a-class".into()], &vec![], &exceptions);
assert_eq!(out, Some(".a-class,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["a-class".into()], &[], &exceptions);
assert_eq!(out, [".a-class", ".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec!["children".into(), "a-class".into()], &vec!["simple-id".into()], &exceptions);
assert_eq!(out, Some(".a-class,#simple-id,.children .including #simple-id,.a-class .with .children{display:none !important;}".to_string()));
let out = cfcache.hidden_class_id_selectors(&["children".into(), "a-class".into()], &["simple-id".into()], &exceptions);
assert_eq!(out, [".a-class", "#simple-id", ".children .including #simple-id", ".a-class .with .children"]);

let out = cfcache.class_id_stylesheet(&vec![], &vec!["test-element".into()], &exceptions);
assert_eq!(out, None);
let out = cfcache.hidden_class_id_selectors(&[], &["test-element".into()], &exceptions);
assert_eq!(out, Vec::<String>::new());
}

#[test]
@@ -218,17 +218,18 @@ impl Engine {
// Cosmetic filter functionality

/// If any of the provided CSS classes or ids could cause a certain generic CSS hide rule
/// (i.e. `{ display: none !important; }`) to be required, this method will return a stylesheet
/// including it, providing that the corresponding rule does not have an exception.
/// (i.e. `{ display: none !important; }`) to be required, this method will return a list of
/// CSS selectors corresponding to rules referencing those classes or ids, provided that the
/// corresponding rules are not excepted.
///
/// `exceptions` should be passed directly from `HostnameSpecificResources`.
pub fn class_id_stylesheet(&self, classes: &[String], ids: &[String], exceptions: &HashSet<String>) -> Option<String> {
self.cosmetic_cache.class_id_stylesheet(classes, ids, exceptions)
pub fn hidden_class_id_selectors(&self, classes: &[String], ids: &[String], exceptions: &HashSet<String>) -> Vec<String> {
self.cosmetic_cache.hidden_class_id_selectors(classes, ids, exceptions)
}

/// Returns a set of cosmetic filter resources required for a particular hostname. Once this
/// has been called, all CSS ids and classes on a page should be passed to
/// `class_id_stylesheet` to obtain any stylesheets consisting of generic rules.
/// `hidden_class_id_selectors` to obtain any stylesheets consisting of generic rules.
pub fn hostname_cosmetic_resources(&self, hostname: &str) -> HostnameSpecificResources {
self.cosmetic_cache.hostname_cosmetic_resources(hostname)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.