renderer.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { JSDOM } = require('jsdom')
  2. const createDOMPurify = require('dompurify')
  3. module.exports = {
  4. async init(input, config) {
  5. if (config.safeHTML) {
  6. const window = new JSDOM('').window
  7. const DOMPurify = createDOMPurify(window)
  8. const allowedAttrs = ['v-pre', 'v-slot:tabs', 'v-slot:content', 'target']
  9. const allowedTags = ['tabset', 'template']
  10. if (config.allowDrawIoUnsafe) {
  11. allowedTags.push('foreignObject')
  12. DOMPurify.addHook('uponSanitizeElement', (elm) => {
  13. if (elm.querySelectorAll) {
  14. const breaks = elm.querySelectorAll('foreignObject br, foreignObject p')
  15. if (breaks && breaks.length) {
  16. for (let i = 0; i < breaks.length; i++) {
  17. breaks[i].parentNode.replaceChild(
  18. window.document.createElement('div'),
  19. breaks[i]
  20. )
  21. }
  22. }
  23. }
  24. })
  25. }
  26. if (config.allowIFrames) {
  27. allowedTags.push('iframe')
  28. allowedAttrs.push('allow')
  29. }
  30. input = DOMPurify.sanitize(input, {
  31. ADD_ATTR: allowedAttrs,
  32. ADD_TAGS: allowedTags
  33. })
  34. }
  35. return input
  36. }
  37. }