-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize-element.html
55 lines (54 loc) · 1.73 KB
/
sanitize-element.html
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
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="sanitize-import.html">
<!--
Element wrapper for the [Sanitize.js](https://github.com/gbirke/Sanitize.js) library (a whitelist-based HTML sanitizer),
to use [`<marked-element>`](https://github.com/polymerelements/marked-element).
-->
<dom-module id="sanitize-element">
<script>
class SanitizeElement extends Polymer.Element {
static get is() {
return 'sanitize-element'
}
static get properties() {
return {
/**
* Custom configuration object. ([parameters](https://github.com/gbirke/Sanitize.js#configuration-object-parameters))
*/
config: {
type: Object,
value: null
},
/**
* Function used for `<marked-element>`
*
* without `config`, sanitize all elements and attributes.
*/
sanitizer: {
type: Function,
readOnly: true,
notify: true,
computed: "_sanitizer(config)"
}
}
}
/**
* Return function of sanitizer for marked-element.
*/
_sanitizer(config) {
const sa = new Sanitize(config)
return i=>{
const isEnd = i.match(/^<\//),
start = isEnd ? i.replace(/^<\//,'<') : i,
temp = document.createElement('div')
temp.innerHTML = start
const clean = sa.clean_node(temp)
return clean.firstElementChild ?
isEnd ? i : clean.firstElementChild.outerHTML.replace(/<\/[^>]+>$/,'')
: ''
}
}
}
customElements.define(SanitizeElement.is, SanitizeElement)
</script>
</dom-module>