renderer.js 1.18 KB
Newer Older
1 2
const { JSDOM } = require('jsdom')
const createDOMPurify = require('dompurify')
3

4 5 6
module.exports = {
  async init(input, config) {
    if (config.safeHTML) {
7 8 9
      const window = new JSDOM('').window
      const DOMPurify = createDOMPurify(window)

10
      const allowedAttrs = ['v-pre', 'v-slot:tabs', 'v-slot:content', 'target']
11 12
      const allowedTags = ['tabset', 'template']

13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
      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]
                )
              }
            }
          }
        })
      }

30 31
      if (config.allowIFrames) {
        allowedTags.push('iframe')
32
        allowedAttrs.push('allow')
33 34 35 36 37
      }

      input = DOMPurify.sanitize(input, {
        ADD_ATTR: allowedAttrs,
        ADD_TAGS: allowedTags
38 39 40
      })
    }
    return input
41 42
  }
}