(Quick Reference)

3 Defining custom whitelists - Reference Documentation

Authors: Sudhir Nimavat

Version: 0.1

3 Defining custom whitelists

Plugin provides a DSL to define custom whitelists in configuration.

Define a custom whitelist sample that will allow just <b>,<i>,<p> and <span> tags.

Config.groovy

htmlcleaner {
    whitelists = {
        whitelist("sample") {
            startwith "none"
            allow "b", "p", "i", "span"
        }
    }
}

The above configuration would define a whitelist with name sample that builds on top of whitelist none and allows additional tags <b>,<i>,<p> and <span>

A whitelist can start with any of the default whitelists or A whitelist can start with any custom whitelists that are defined earlier in configuration as well, but it must start with another whitelist.

Define a whitelist sample2 that starts with whitelist sample we defined above and allows tag <a> with just one attribute href and puts rel="nofollow"

htmlcleaner {
    whitelists = {
        whitelist("sample2") {
            startwith "sample"
            allow("a") {
                attributes "href"
                enforce attribute:"rel", value:"nofollow"
            }
        }
    }
}

Define a whitelist basic-with-tables that starts with whitelist basic and allows tables.

htmlcleaner {
    whitelists = {
        whitelist("basic-with-tables") {
            startwith "basic"
            allow "table", "tr", "td"
        }
    }
}

Restricting attributes

htmlcleaner {
    whitelists = {
        whitelist("sample") {
            allow("div") {
                attributes "id", "class"
            }
        }
    }
}

Enforcing attributes - An enforced attribute will always be added to the element. If the element already has the attribute set, it will be overridden.

htmlcleaner {
    whitelists = {
        whitelist("sample") {
            allow("div") {
                enforce attribute:"class", value:"block"
            }
        }
    }
}

Defining multiple whitelists

htmlcleaner {
    whitelists = {
        whitelist("sample") {
            startwith "none"
            allow "b", "p", "span"
        }

whitelist("sample-with-anchor") { startwith "sample" allow("a") { attributes "href" enforce attribute:"rel", value:"nofollow" } }

whitelist("basic-with-tables") { startwith "basic" allow "table", "tr", "td" }

} }