【发布时间】:2012-04-27 20:30:41
【问题描述】:
如何配置 jsoup Whitelist 以允许内部锚引用,而不允许任何任意值?
示例 html:
<a href="#section1" target="_self">Jump To Section 1</a>
<!-- ... -->
<a name="section1">Section 1</a>
如果我尝试使用宽松的Whitelist 清理代码,href 将被删除。
Jsoup.clean(html, Whitelist.relaxed().addAttributes("a", "name", "target");
返回以下内容:
<a target="_self">Jump To Section 1</a>
<!-- ... -->
<a name="section1">Section 1</a>
如果我手动构建 Whitelist 并添加我想要的标签和属性,但不要调用 addProtocols(....),我可以让 jsoup 将 href 留在原处,但这看起来不像很好的解决方案,因为它不会过滤掉包含 JavaScript 的 href。例如,我希望从以下内容中删除 a 标签(或至少 href):
<a href="javascript:alert(1111);" target="_self">Jump To Section 1</a>
<a name="section1">Section 1</a>
jsoup 可以做到吗?
我确实看到了以下向 jsoup 提交的补丁,但它看起来并没有进入 jsoup 代码库:https://github.com/jhy/jsoup/pull/77
【问题讨论】: