-
-
Notifications
You must be signed in to change notification settings - Fork 14
Description
I may be doing something wrong or tag_attribute_values it's not working properly.
From docs:
tag_attribute_values (dict[str, dict[str, set[str]]], optional) – 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.
So, if allow a specific attr in my tag and then use tag_attribute_values, nh3 will filter that attr out of my tag, right?
The following code:
import nh3
print(
nh3.clean(
"<p my-attr='my-WRONG-attr-value'>text</p>",
tags={"p"},
attributes={"p": {"my-attr"}},
tag_attribute_values={"p": {"my-attr": {"my-attr-value"}}},
)
)returns: <p my-attr="my-WRONG-attr-value">text</p>
A more real world example:
Allow p tag to have style, but only with text-align.
import nh3
print(
nh3.clean(
"<p style='color: #fff;'>text</p>",
tags={"p"},
attributes={"p": {"style"}},
tag_attribute_values={"p": {"style": {"text-align"}}},
)
)PS: Since I'm not even sure if this should work for a attr like style that have multiple options as values. But that was just what I was trying to do when I caught this.
Since there's not much to work with the docs, I did my experiments looking at the tests.
The test is only covering the positive case, maybe that's why we have this not working.
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>'
)This is assuming I understood how tag_attribute_values should work.