2020-05-08 21:00:02 +00:00
|
|
|
const { JSDOM } = require('jsdom')
|
|
|
|
const createDOMPurify = require('dompurify')
|
2018-09-16 04:35:03 +00:00
|
|
|
|
2019-12-12 04:35:54 +00:00
|
|
|
module.exports = {
|
|
|
|
async init(input, config) {
|
|
|
|
if (config.safeHTML) {
|
2020-05-08 21:00:02 +00:00
|
|
|
const window = new JSDOM('').window
|
|
|
|
const DOMPurify = createDOMPurify(window)
|
|
|
|
|
2020-06-07 23:23:33 +00:00
|
|
|
const allowedAttrs = ['v-pre', 'v-slot:tabs', 'v-slot:content', 'target']
|
2020-05-08 21:00:02 +00:00
|
|
|
const allowedTags = ['tabset', 'template']
|
|
|
|
|
2020-09-12 18:05:24 +00:00
|
|
|
if (config.allowDrawIoUnsafe) {
|
|
|
|
allowedTags.push('foreignObject')
|
|
|
|
DOMPurify.addHook('uponSanitizeElement', (elm) => {
|
|
|
|
if (elm.querySelectorAll) {
|
|
|
|
const breaks = elm.querySelectorAll('foreignObject br, foreignObject p')
|
|
|
|
if (breaks && breaks.length) {
|
|
|
|
for (let i = 0; i < breaks.length; i++) {
|
|
|
|
breaks[i].parentNode.replaceChild(
|
|
|
|
window.document.createElement('div'),
|
|
|
|
breaks[i]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-08 21:00:02 +00:00
|
|
|
if (config.allowIFrames) {
|
|
|
|
allowedTags.push('iframe')
|
|
|
|
}
|
|
|
|
|
|
|
|
input = DOMPurify.sanitize(input, {
|
|
|
|
ADD_ATTR: allowedAttrs,
|
|
|
|
ADD_TAGS: allowedTags
|
2019-12-12 04:35:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return input
|
2018-09-16 04:35:03 +00:00
|
|
|
}
|
|
|
|
}
|