Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Match newer selectors API of adblock-rust #12

Merged
merged 1 commit into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Brian R. Bondy <[email protected]>"]
edition = "2018"

[dependencies]
adblock="~0.2.1"
adblock = { version = "~0.2.1", git = "https://github.com/brave/adblock-rust", rev = "fab2ae2b21bb4be052faa1d70dde34c61628a910" }
serde_json = "1.0"
libc = "0.2"

Expand Down
16 changes: 8 additions & 8 deletions examples/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,27 +244,27 @@ void TestClassId() {
std::vector<std::string> classes = std::vector<std::string>();
std::vector<std::string> ids = std::vector<std::string>();
std::vector<std::string> exceptions = std::vector<std::string>();
std::string stylesheet = engine.classIdStylesheet(classes, ids, exceptions);
assert(stylesheet == "");
std::string stylesheet = engine.hiddenClassIdSelectors(classes, ids, exceptions);
assert(stylesheet == "[]");

classes = std::vector<std::string>({"ads", "no-ads"});
ids = std::vector<std::string>({"element"});
exceptions = std::vector<std::string>();
stylesheet = engine.classIdStylesheet(classes, ids, exceptions);
assert(stylesheet == ".ads,#element{display:none !important;}");
stylesheet = engine.hiddenClassIdSelectors(classes, ids, exceptions);
assert(stylesheet == "[\".ads\",\"#element\"]");

classes = std::vector<std::string>({"element", "a"});
ids = std::vector<std::string>({"block", "ads", "a"});
exceptions = std::vector<std::string>({"#block"});
stylesheet = engine.classIdStylesheet(classes, ids, exceptions);
assert(stylesheet == ".element,#block + .child,#ads > #element{display:none !important;}");
stylesheet = engine.hiddenClassIdSelectors(classes, ids, exceptions);
assert(stylesheet == "[\".element\",\"#block + .child\",\"#ads > #element\"]");

// Classes and ids must be passed without the leading `.` or `#`, or they will not be recognized
classes = std::vector<std::string>({".element", ".a"});
ids = std::vector<std::string>({"#block", "#ads", "#a"});
exceptions = std::vector<std::string>({"block"});
stylesheet = engine.classIdStylesheet(classes, ids, exceptions);
assert(stylesheet == "");
stylesheet = engine.hiddenClassIdSelectors(classes, ids, exceptions);
assert(stylesheet == "[]");
}

void TestHostnameCosmetics() {
Expand Down
24 changes: 12 additions & 12 deletions src/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,6 @@ void engine_add_resources(C_Engine *engine, const char *resources);
*/
void engine_add_tag(C_Engine *engine, const char *tag);

/**
* Returns a stylesheet containing all generic cosmetic rules that begin with any of the provided class and id selectors
* The leading '.' or '#' character should not be provided
*/
char *engine_class_id_stylesheet(C_Engine *engine,
const char *const *classes,
size_t classes_size,
const char *const *ids,
size_t ids_size,
const char *const *exceptions,
size_t exceptions_size);

/**
* Create a new `Engine`.
*/
Expand All @@ -72,6 +60,18 @@ bool engine_deserialize(C_Engine *engine, const char *data, size_t data_size);
*/
void engine_destroy(C_Engine *engine);

/**
* Returns a stylesheet containing all generic cosmetic rules that begin with any of the provided class and id selectors
* The leading '.' or '#' character should not be provided
*/
char *engine_hidden_class_id_selectors(C_Engine *engine,
const char *const *classes,
size_t classes_size,
const char *const *ids,
size_t ids_size,
const char *const *exceptions,
size_t exceptions_size);

/**
* Returns a set of cosmetic filtering resources specific to the given hostname, in JSON format
*/
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub unsafe extern "C" fn engine_hostname_cosmetic_resources(
///
/// The leading '.' or '#' character should not be provided
#[no_mangle]
pub unsafe extern "C" fn engine_class_id_stylesheet(
pub unsafe extern "C" fn engine_hidden_class_id_selectors(
engine: *mut Engine,
classes: *const *const c_char,
classes_size: size_t,
Expand All @@ -272,6 +272,6 @@ pub unsafe extern "C" fn engine_class_id_stylesheet(
.collect();
assert!(!engine.is_null());
let engine = Box::leak(Box::from_raw(engine));
let stylesheet = engine.class_id_stylesheet(&classes, &ids, &exceptions);
CString::new(stylesheet.unwrap_or_else(|| String::new())).expect("Error: CString::new()").into_raw()
let stylesheet = engine.hidden_class_id_selectors(&classes, &ids, &exceptions);
CString::new(serde_json::to_string(&stylesheet).unwrap_or_else(|_| "".into())).expect("Error: CString::new()").into_raw()
}
4 changes: 2 additions & 2 deletions src/wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const std::string Engine::hostnameCosmeticResources(const std::string& hostname)
return resources_json;
}

const std::string Engine::classIdStylesheet(const std::vector<std::string>& classes, const std::vector<std::string>& ids, const std::vector<std::string>& exceptions) {
const std::string Engine::hiddenClassIdSelectors(const std::vector<std::string>& classes, const std::vector<std::string>& ids, const std::vector<std::string>& exceptions) {
std::vector<const char*> classes_raw;
classes_raw.reserve(classes.size());
for(size_t i = 0; i < classes.size(); i++) {
Expand All @@ -149,7 +149,7 @@ const std::string Engine::classIdStylesheet(const std::vector<std::string>& clas
exceptions_raw.push_back(exceptions[i].c_str());
}

char* stylesheet_raw = engine_class_id_stylesheet(
char* stylesheet_raw = engine_hidden_class_id_selectors(
raw,
classes_raw.data(), classes.size(),
ids_raw.data(), ids.size(),
Expand Down
2 changes: 1 addition & 1 deletion src/wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Engine {
void removeTag(const std::string& tag);
bool tagExists(const std::string& tag);
const std::string hostnameCosmeticResources(const std::string& hostname);
const std::string classIdStylesheet(
const std::string hiddenClassIdSelectors(
const std::vector<std::string>& classes,
const std::vector<std::string>& ids,
const std::vector<std::string>& exceptions
Expand Down