如何使Jsoup白名单接受某些属性内容

如何使Jsoup白名单接受某些属性内容,第1张

如何使Jsoup白名单接受某些属性内容

您可以扩展白名单并覆盖isSafeAttribute以执行自定义检查。由于无法直接扩展Whitelist.relaxed(),因此您必须复制一些代码以设置相同的列表:

public class RelaxedPlusDatabase64Images extends Whitelist {    public RelaxedPlusDatabase64Images() {        //copied from Whitelist.relaxed()        addTags("a", "b", "blockquote", "br", "caption", "cite", "pre", "col",     "colgroup", "dd", "div", "dl", "dt", "em", "h1", "h2", "h3", "h4", "h5", "h6",     "i", "img", "li", "ol", "p", "pre", "q", "small", "strike", "strong",     "sub", "sup", "table", "tbody", "td", "tfoot", "th", "thead", "tr", "u",     "ul");        addAttributes("a", "href", "title");        addAttributes("blockquote", "cite");        addAttributes("col", "span", "width");        addAttributes("colgroup", "span", "width");        addAttributes("img", "align", "alt", "height", "src", "title", "width");        addAttributes("ol", "start", "type");        addAttributes("q", "cite");        addAttributes("table", "summary", "width");        addAttributes("td", "abbr", "axis", "colspan", "rowspan", "width");        addAttributes("th", "abbr", "axis", "colspan", "rowspan", "scope", "width");        addAttributes("ul", "type");        addProtocols("a", "href", "ftp", "http", "https", "mailto");        addProtocols("blockquote", "cite", "http", "https");        addProtocols("cite", "cite", "http", "https");        addProtocols("img", "src", "http", "https");        addProtocols("q", "cite", "http", "https");    }    @Override    protected boolean isSafeAttribute(String tagName, Element el, Attribute attr) {        return ("img".equals(tagName)     && "src".equals(attr.getKey())     && attr.getValue().startsWith("data:;base64")) || super.isSafeAttribute(tagName, el, attr);    }}

由于您没有提供要解析的代码或要消毒的HTML,因此我没有对此进行测试。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5561273.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存