Skip to content

Commit

Permalink
Add tag_attribute_values option
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Apr 22, 2023
1 parent 809365a commit a2ec808
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions nh3.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def clean(
strip_comments: bool = True,
link_rel: Optional[str] = "noopener noreferrer",
generic_attribute_prefixes: Optional[Set[str]] = None,
tag_attribute_values: Optional[Dict[str, Dict[str, Set[str]]]] = None,
) -> str: ...
def clean_text(html: str) -> str: ...
def is_html(html: str) -> bool: ...
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ use pyo3::types::{PyString, PyTuple};
/// :type link_rel: ``str``
/// :param generic_attribute_prefixes: Sets the prefix of attributes that are allowed on any tag.
/// :type generic_attribute_prefixes: ``set[str]``, optional
/// :param tag_attribute_values: Sets the values of HTML attributes that are allowed on specific tags.
/// The value is structured as a map from tag names to a map from attribute names to a set of attribute values.
/// If a tag is not itself whitelisted, adding entries to this map will do nothing.
/// :type tag_attribute_values: ``dict[str, dict[str, set[str]]]``, optional
/// :return: Sanitized HTML fragment
/// :rtype: ``str``
#[pyfunction(signature = (
Expand All @@ -43,6 +47,7 @@ use pyo3::types::{PyString, PyTuple};
strip_comments = true,
link_rel = "noopener noreferrer",
generic_attribute_prefixes = None,
tag_attribute_values = None,
))]
fn clean(
py: Python,
Expand All @@ -54,6 +59,7 @@ fn clean(
strip_comments: bool,
link_rel: Option<&str>,
generic_attribute_prefixes: Option<HashSet<&str>>,
tag_attribute_values: Option<HashMap<&str, HashMap<&str, HashSet<&str>>>>,
) -> PyResult<String> {
if let Some(callback) = attribute_filter.as_ref() {
if !callback.as_ref(py).is_callable() {
Expand All @@ -69,6 +75,7 @@ fn clean(
|| !strip_comments
|| link_rel != Some("noopener noreferrer")
|| generic_attribute_prefixes.is_some()
|| tag_attribute_values.is_some()
{
let mut cleaner = ammonia::Builder::default();
if let Some(tags) = tags {
Expand All @@ -86,6 +93,9 @@ fn clean(
if let Some(prefixes) = generic_attribute_prefixes {
cleaner.generic_attribute_prefixes(prefixes);
}
if let Some(values) = tag_attribute_values {
cleaner.tag_attribute_values(values);
}
if let Some(callback) = attribute_filter {
cleaner.attribute_filter(move |element, attribute, value| {
Python::with_gil(|py| {
Expand Down
9 changes: 9 additions & 0 deletions tests/test_nh3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_clean():
== '<div data-v="foo"></div>'
)

assert (
nh3.clean(
"<my-tag my-attr=val>",
tags={"my-tag"},
tag_attribute_values={"my-tag": {"my-attr": {"val"}}},
)
== '<my-tag my-attr="val"></my-tag>'
)


def test_clean_with_attribute_filter():
html = "<a href=/><img alt=Home src=foo></a>"
Expand Down

0 comments on commit a2ec808

Please sign in to comment.