1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import org.jsoup.safety.Whitelist
fun main() {
// 使用预置的白名单
//val whitelist: Whitelist = Whitelist.relaxed() // 常见的 HTML 标签
// .addAttributes(":all", "id", "class", "style") // 所有标签都允许的属性
// 自定义白名单
val whitelist: Whitelist = Whitelist()
.addTags("a", "b", "blockquote", "br", "caption", "cite", "code", "col", "colgroup", "dd", "div", "dl", "dt",
"em", "h1", "h2", "h3", "h4", "h5", "h6", "i", "img", "li", "ol", "p", "pre", "q", "small", "span", "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")
.addAttributes(":all", "id", "class", "style")
.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");
val bodyHtml = """
<div class="div-test"><img src="123" onerror="javascript:alert(123);"></div>
""".trimIndent()
println(Jsoup.clean(bodyHtml, whitelist))
println("--------------")
// 不格式化输出
println(Jsoup.clean(bodyHtml, "", whitelist, Document.OutputSettings().prettyPrint(false)))
}
|
评论