简体   繁体   中英

How to get list of valid tags of Jsoup whitelist?

How do I get a list of all the valid tags of a given Jsoup Whitelist ?

I can't find such a function in the docs at Jsoup whitelist docs . I use ColdFusion, but a java solution or hint would be fine. I guess I could translate it.

You can check here that, what you are asking for is the tagNames set. The class doesn't provide any getter.

What you can do is:

  1. Download the source code of jsoup and just edit the Whitelist class and add a getter. You can even make a pull request after that.
  2. Take the default tags of each whitelist category and keep them in variables that are accessible to you.
  3. The last alternative would be to use reflection in order to gain access to a private variable, but that's not a good practice, since there are other, cleaner ways to achieve what you want.

If you want to go the reflection route, you can do something like below which grabs access to the tagNames set, converts it to an array of org.jsoup.safety.Whitelist$TagName objects (which contain the tag names) and then appends the toString() values of those objects to another array.

<cfscript>

whitelist = createObject("java", "org.jsoup.safety.Whitelist");
collection = [];
tags = whitelist.getClass().getDeclaredField("tagNames");
tags.setAccessible(true);

// this portion uses the relaxed whitelist as an example
for (tag in tags.get(whitelist.relaxed()).toArray()) {
    arrayAppend(collection, tag.toString());
}

writeDump(collection);

</cfscript>

If you need the attributes and/or protocols fields, it's a similar approach but there is more to iterate through since they are maps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM