By default Whitelist class removes style, but you can easily modify this behaviour and add support for style
with addAttributes("p", "style")
.
Whitelist.relaxed().addAttributes("p", "style");
Explanation
This set attribute style
to element p
as ignored under cleaning. Only style
from p
will be not removed!
Verification code
Simply copy paste this code and invoke from main
.
public static void main(String[] args) {
String text = "<p style=\"text-align: center;\">Center Aligned Text</p>";
String clean = Jsoup.clean(text, Whitelist.relaxed()
.addAttributes("p", "style"));
System.out.println(clean);
}
Result
<p style="text-align: center;">Center Aligned Text</p>
Dependency
org.jsoup:jsoup:1.7.3