Skip to content

tag_attribute_values doesn't filter out the attribute if value doesn't match #61

@douglasmiranda

Description

@douglasmiranda

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions