html: Take a reference to SanitizerConfig for Html::sanitize_with
Allows to reuse the configuration without cloning it.
This commit is contained in:
parent
8ecbc47e55
commit
18244143ca
@ -4,6 +4,7 @@ Breaking Changes:
|
|||||||
|
|
||||||
- Do not export `Node` in the public API, it is not usable on its own and it is
|
- Do not export `Node` in the public API, it is not usable on its own and it is
|
||||||
not in the output of any public method.
|
not in the output of any public method.
|
||||||
|
- `Html::sanitize_with` now takes a reference to `SanitizerConfig`.
|
||||||
|
|
||||||
Improvements:
|
Improvements:
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ pub fn sanitize_html(
|
|||||||
config = config.remove_reply_fallback();
|
config = config.remove_reply_fallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
sanitize_inner(s, config)
|
sanitize_inner(s, &config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// What HTML [tags and attributes] should be kept by the sanitizer.
|
/// What HTML [tags and attributes] should be kept by the sanitizer.
|
||||||
@ -62,10 +62,10 @@ pub enum RemoveReplyFallback {
|
|||||||
/// [rich reply fallback]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies
|
/// [rich reply fallback]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies
|
||||||
pub fn remove_html_reply_fallback(s: &str) -> String {
|
pub fn remove_html_reply_fallback(s: &str) -> String {
|
||||||
let config = SanitizerConfig::new().remove_reply_fallback();
|
let config = SanitizerConfig::new().remove_reply_fallback();
|
||||||
sanitize_inner(s, config)
|
sanitize_inner(s, &config)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sanitize_inner(s: &str, config: SanitizerConfig) -> String {
|
fn sanitize_inner(s: &str, config: &SanitizerConfig) -> String {
|
||||||
let mut html = Html::parse(s);
|
let mut html = Html::parse(s);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(config);
|
||||||
html.to_string()
|
html.to_string()
|
||||||
|
@ -44,11 +44,11 @@ impl Html {
|
|||||||
/// `SanitizerConfig::compat().remove_reply_fallback()`.
|
/// `SanitizerConfig::compat().remove_reply_fallback()`.
|
||||||
pub fn sanitize(&mut self) {
|
pub fn sanitize(&mut self) {
|
||||||
let config = SanitizerConfig::compat().remove_reply_fallback();
|
let config = SanitizerConfig::compat().remove_reply_fallback();
|
||||||
self.sanitize_with(config);
|
self.sanitize_with(&config);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sanitize this HTML according to the given configuration.
|
/// Sanitize this HTML according to the given configuration.
|
||||||
pub fn sanitize_with(&mut self, config: SanitizerConfig) {
|
pub fn sanitize_with(&mut self, config: &SanitizerConfig) {
|
||||||
config.clean(self);
|
config.clean(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ impl SanitizerConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Clean the given HTML with this sanitizer.
|
/// Clean the given HTML with this sanitizer.
|
||||||
pub(crate) fn clean(self, html: &mut Html) {
|
pub(crate) fn clean(&self, html: &mut Html) {
|
||||||
let root = html.root();
|
let root = html.root();
|
||||||
let mut next_child = root.first_child;
|
let mut next_child = root.first_child;
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ fn valid_input() {
|
|||||||
<code class=\"language-html\"><mx-reply>This is a fake reply</mx-reply></code>\
|
<code class=\"language-html\"><mx-reply>This is a fake reply</mx-reply></code>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -41,7 +41,7 @@ fn tags_remove() {
|
|||||||
<p>But this is inside a tag</p>\
|
<p>But this is inside a tag</p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -77,7 +77,7 @@ fn tags_remove_without_reply() {
|
|||||||
<p>But this is inside a tag</p>\
|
<p>But this is inside a tag</p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -105,7 +105,7 @@ fn tags_remove_only_reply_fallback() {
|
|||||||
<p>But this is inside a tag</p>\
|
<p>But this is inside a tag</p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -125,7 +125,7 @@ fn attrs_remove() {
|
|||||||
<p class=\"important\">Look at <span data-mx-color=\"#0000ff\" size=20>me!</span></p>\
|
<p class=\"important\">Look at <span data-mx-color=\"#0000ff\" size=20>me!</span></p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -145,7 +145,7 @@ fn img_remove_scheme() {
|
|||||||
<img src=\"https://notareal.hs/abcdef\">\
|
<img src=\"https://notareal.hs/abcdef\">\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(html.to_string(), "<p>Look at that picture:</p>");
|
assert_eq!(html.to_string(), "<p>Look at that picture:</p>");
|
||||||
}
|
}
|
||||||
@ -158,7 +158,7 @@ fn link_remove_scheme() {
|
|||||||
<p>Go see <a href=\"file://local/file.html\">my local website</a></p>\
|
<p>Go see <a href=\"file://local/file.html\">my local website</a></p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -177,7 +177,7 @@ fn link_compat_scheme() {
|
|||||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
"\
|
"\
|
||||||
@ -193,7 +193,7 @@ fn link_compat_scheme() {
|
|||||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
"\
|
"\
|
||||||
@ -214,7 +214,7 @@ fn class_remove() {
|
|||||||
<p>What do you think of the name <code class=\"fake-language-rust\">StringList</code>?</p>\
|
<p>What do you think of the name <code class=\"fake-language-rust\">StringList</code>?</p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
@ -240,7 +240,7 @@ fn depth_remove() {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut html = Html::parse(&deeply_nested_html);
|
let mut html = Html::parse(&deeply_nested_html);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
let res = html.to_string();
|
let res = html.to_string();
|
||||||
assert!(res.contains("I should be fine."));
|
assert!(res.contains("I should be fine."));
|
||||||
@ -255,7 +255,7 @@ fn replace_deprecated() {
|
|||||||
<p>Look at <strike>you </strike><font data-mx-bg-color=\"#ff0000\" color=\"#0000ff\">me!</span></p>\
|
<p>Look at <strike>you </strike><font data-mx-bg-color=\"#ff0000\" color=\"#0000ff\">me!</span></p>\
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
html.sanitize_with(config);
|
html.sanitize_with(&config);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
html.to_string(),
|
html.to_string(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user