Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to enforce having mutliple attributes on tag? #337

Open
tom-mayer opened this issue Apr 16, 2024 · 0 comments
Open

Possible to enforce having mutliple attributes on tag? #337

tom-mayer opened this issue Apr 16, 2024 · 0 comments

Comments

@tom-mayer
Copy link

tom-mayer commented Apr 16, 2024

Hi,

I am running into a case where I would like to enforce having two specific attributes on a tag, else the tag is illegal.
As an example, <bar> should only be allowed if both attributes argh and blubb are present:

<bar argh="test"></bar>  // disallowed
<bar blubb="something" ></bar> // disallowed
<bar></bar> // disallowed
<bar argh="test" blubb="something"></bar> // allowed

I tried something like:

    private fun barRule(): PolicyFactory {
        return HtmlPolicyBuilder()
            .allowElements("bar")
            .disallowWithoutAttributes("bar")
            .allowAttributes("blubb", "argh").onElements("bar")
            .toFactory()
    }

This leads to both attributes being allowed solo, which I don't want. Is there a way to write this constraint in the builder syntax or would I have to use the allowElements with lambda function interface and implement this full manual?

I tried implementing the rule like this:

    private fun barRule(): PolicyFactory {
        return HtmlPolicyBuilder()
            .allowElements(object : ElementPolicy {
                override fun apply(name: String, attributes: MutableList<String>): String? {
                    return if (attributes.containsAll(listOf("argh", "blubb"))) {
                        name
                    } else {
                        null
                    }
                }
            }, "bar")
            .allowAttributes("argh", "blubb").onElements("bar")
            .toFactory()
    }

The one thing that is very weird is, if I sanitize <bar argh="test"></bar> with this policy, the attributes list contains two elements, argh and test (which is a value and not an attribute). Is this by design? Is there any reason why this is a string list with key/value after each other instead of a key-value Map?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant