Skip to content

Commit ed064c8

Browse files
authored
Add two additional examples for the documentation (#72)
This should help with issues: - #36 - #70
1 parent 9d73a46 commit ed064c8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,38 @@ use pyo3::types::{PyString, PyTuple};
5454
/// 'hi'
5555
/// >>> nh3.clean("<b><img src='' onerror='alert(\\'hax\\')'>XSS?</b>")
5656
/// '<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+
5789
#[pyfunction(signature = (
5890
html,
5991
tags = None,

0 commit comments

Comments
 (0)