@@ -54,6 +54,38 @@ use pyo3::types::{PyString, PyTuple};
54
54
/// 'hi'
55
55
/// >>> nh3.clean("<b><img src='' onerror='alert(\\'hax\\')'>XSS?</b>")
56
56
/// '<b><img src="">XSS?</b>'
57
+ ///
58
+ /// Example of using ``attribute_filter``:
59
+ ///
60
+ /// .. code-block:: pycon
61
+ ///
62
+ /// >>> from copy import deepcopy
63
+ /// >>> attributes = deepcopy(nh3.ALLOWED_ATTRIBUTES)
64
+ /// >>> attributes["a"].add("class")
65
+ /// >>> def attribute_filter(tag, attr, value):
66
+ /// ... if tag == "a" and attr == "class":
67
+ /// ... if "mention" in value.split(" "):
68
+ /// ... return "mention"
69
+ /// ... return None
70
+ /// ... return value
71
+ /// >>> nh3.clean("<a class='mention unwanted'>@foo</a>",
72
+ /// ... attributes=attributes,
73
+ /// ... attribute_filter=attribute_filter)
74
+ /// '<a class="mention" rel="noopener noreferrer">@foo</a>'
75
+ ///
76
+ /// Example of maintaining the ``rel`` attribute:
77
+ ///
78
+ /// .. code-block:: pycon
79
+ ///
80
+ /// >>> from copy import deepcopy
81
+ /// >>> attributes = deepcopy(nh3.ALLOWED_ATTRIBUTES)
82
+ /// >>> attributes["a"].add("rel")
83
+ /// >>> nh3.clean("<a href='https://tag.example' rel='tag'>#tag</a>",
84
+ /// ... link_rel=None, attributes=attributes)
85
+ /// '<a href="https://tag.example" rel="tag">#tag</a>'
86
+
87
+
88
+
57
89
#[ pyfunction( signature = (
58
90
html,
59
91
tags = None ,
0 commit comments