/
HtmlCleanerGrailsPlugin.groovy
100 lines (80 loc) · 2.93 KB
/
HtmlCleanerGrailsPlugin.groovy
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import grails.plugin.htmlcleaner.HtmlCleaner
import grails.plugin.htmlcleaner.WhitelistBuilder
import org.jsoup.safety.Whitelist
import org.springframework.util.Assert
class HtmlCleanerGrailsPlugin {
def version = "0.3"
def grailsVersion = "1.3.7 > *"
def title = "Html Cleaner"
def author = "Sudhir Nimavat"
def authorEmail = "sudhir_nimavat@yahoo.com"
def description = "whitelist based html cleaner based on jsoup"
def developers = [ [ name: "Igor Shults", email: "igor.shults@gmail.com" ] ]
def documentation = "http://snimavat.github.com/html-cleaner/guide/index.html"
def license = 'APACHE'
def issueManagement = [system: 'GitHub', url: 'https://github.com/snimavat/html-cleaner/issues']
def scm = [system: 'GitHub', url: 'https://github.com/snimavat/html-cleaner']
def observe = ['controllers']
private final Map<String, Whitelist> whitelists = [:]
def doWithSpring = {
buildWhitelists(application)
HtmlCleaner.metaClass.getWhitelists = {
return Collections.unmodifiableMap(whitelists)
}
def pluginConfig = application.config.htmlcleaner
String defaultWhiteListStr = application.config.htmlcleaner?.defaultWhiteList
//create a spring bean
htmlCleaner(HtmlCleaner) {
if(defaultWhiteListStr) {
defaultWhiteList = defaultWhiteListStr
}
}
}
def doWithDynamicMethods = { ctx ->
//add cleanHtml method to all controllers
application.controllerClasses.each { controllerClass ->
addCleanHtmlMethod(controllerClass, application)
}
}
def onChange = { event ->
if (!(event.source instanceof Class)) {
return
}
if (application.isControllerClass(event.source)) {
def controllerClass = application.getControllerClass(event.source.name)
if (controllerClass) {
addCleanHtmlMethod(controllerClass, event.application)
}
}
}
def onConfigChange = { event ->
buildWhitelists(event.application)
doWithDynamicMethods()
}
private void buildWhitelists(application) {
WhitelistBuilder builder = new WhitelistBuilder()
def pluginConfig = application.config.htmlcleaner
def whitelistsClosure = pluginConfig?.whitelists
//Clear all existing whitelists
whitelists.clear()
if(whitelistsClosure && whitelistsClosure instanceof Closure) {
whitelists.putAll(builder.build(whitelistsClosure))
}
//Add default whitelists
whitelists['none'] = Whitelist.none()
whitelists['basic'] = Whitelist.basic()
whitelists['simpleText'] = Whitelist.simpleText()
whitelists['basicWithImages'] = Whitelist.basicWithImages()
whitelists['relaxed'] = Whitelist.relaxed()
}
private String addCleanHtmlMethod(clazz, application) {
HtmlCleaner cleaner = application.mainContext.getBean("htmlCleaner")
Assert.notNull(cleaner, "HtmlCleaner spring bean should have been configured at this point")
clazz.metaClass.cleanHtml = {String unsafe, String whitelist ->
return cleaner.cleanHtml(unsafe, whitelist)
}
clazz.metaClass.cleanHtml = {String unsafe ->
return cleaner.cleanHtml(unsafe)
}
}
}