Search code examples
htmlcleaner

How to set `invalidAttributeNamePrefix` value in Java?


Suppose I'm cleaning some html using HtmlCleaner (v2.18) and I want to set the property invalidAttributeNamePrefix (see section Cleaner parameters) to some value, i.e.: data-.

This way an attribute my-custom-attr="my-value" in the HTML will be transformed to data-my-custom-attr="my-value".

How can I do that? I wasn't able to find any example for the Java usage.

You can take as reference this piece of code:

HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
properties.setOmitComments(true);
// properties.setInvalidAttributeNamePrefix("data-"); there is no such method

// html is a declared variable which contains some html content
TagNode rootTagNode = cleaner.clean(html);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(properties);
String cleanedHtml = xmlSerializer.getAsString(rootTagNode);

Solution

  • Upgrading to version 2.22 solves this.

    Now it can be done

    // ...
    properties.setInvalidXmlAttributeNamePrefix("data-");
    //...