【问题标题】:How to use DOMPurify package with NuxtJS? Error: "default.a.sanitize is not a function"如何在 NuxtJS 中使用 DOMPurify 包?错误:“default.a.sanitize 不是函数”
【发布时间】:2021-11-03 10:23:09
【问题描述】:

我正在尝试在我的 NuxtJS 应用程序中使用 DOMPurify 包将 HTML 解析为干净且安全的字符串,以便在 UI 中呈现。在渲染使用包的页面时,出现以下错误:

dompurify__WEBPACK_IMPORTED_MODULE_1___default.a.sanitize is not a function

关于如何解决此问题的任何建议?我在此处的代码框中有此代码: https://codesandbox.io/s/reverent-bardeen-kh4wg?file=/pages/index.vue:0-2868

我已将包导入到我的单个文件组件中,如下所示:

<template>
  ......cut unnecessary code for this example...
        <textarea
          id="title_input"
          v-model.trim="formValues.title"
          class="form-control form-control-lg border-0 fw-bold lh-1 fs-1 mb-3"
          placeholder="New post title here..."
          maxlength="80"
          rows="2"
          minlength="6"
          autocomplete="off"
          required
          aria-describedby="titleHelpBlock"
        ></textarea>
        <textarea
          id="content_input"
          v-model.trim="formValues.content"
          class="form-control border-0 h-100"
          rows="3"
          minlength="30"
          maxlength="1000"
          autocomplete="off"
          placeholder="Write your post here..."
          required
          aria-describedby="contentHelpBlock"
        ></textarea>
      
  .....
</template>

<script>
import { debounce } from "lodash";
import DOMPurify from "dompurify";
import marked from "marked";
export default {
  name: "UploadForm",
  data() {
    return {
      formValues: {
        title: "New post title here...",
        content: "Write your post here...",
      },
    };
  },
  computed: {
    compiledMarkdown() {
      // only need the HTML profile, not SVG andMathML stuff
      const clean = DOMPurify.sanitize(this.formValues.title, {
        USE_PROFILES: { html: true },
      });
      return marked(clean);
    },
  },
  methods: {
    update: debounce(function (e) {
      this.input = e.target.value;
    }, 300),
    updateTitle: debounce(function (e) {
      this.formValues.title = e.target.value;
    }, 300),
    updateContent: debounce(function (e) {
      this.formValues.content = e.target.value;
    }, 300),
  },
};
</script>

【问题讨论】:

  • 我从未直接使用过dompurify。同时,Vue 包装器的效果非常好:npmjs.com/package/vue-dompurify-html 试一试,让我们知道它是否更好。
  • 好的!我会回复你的
  • 同样的问题! VueDOMPurifyHTML is not defined 。更新了我的代码框。我没有正确访问插件吗?
  • 现在好像没事了。我以为我需要将 VueDOMPurifyHTML 添加到计算中,但删除它似乎可以解决它。我只需要在v-dompurify-html 指令中使用它。
  • 是的,import + 指令足以让它工作。

标签: nuxt.js dompurify


【解决方案1】:

你不能直接使用v-html作为指令,因为它会导致XSS:https://vuejs.org/v2/guide/syntax.html#Raw-HTML

因此,您需要清理其中一些用户输入。
为了在 Vue/Nuxt 中实现这一点,我建议使用 vue-dompurify-html

需要遵循的一些步骤

  • yarn add vue-dompurify-html安装它
  • 像这样将它导入你的.vue文件中
import Vue from 'vue'
import VueDOMPurifyHTML from 'vue-dompurify-html'
 
Vue.use(VueDOMPurifyHTML)
  • 像这样在您的模板中使用它
<div>
  <div v-dompurify-html="rawHtml"></div>
</div>

【讨论】:

  • 经过进一步测试,似乎如果禁用javascript,则无法加载纯化的内容,因为vue-dompurify需要JS。你能确认一下吗?
  • @redshift 是的,看起来合法!
  • 我只需要在客户端的用户表单输入期间清理降价,使用 vue dompurify 就可以了)。为了在 SSR 期间阅读内容,我只是使用 v-html 来处理关闭 javascript 的情况(禁用 eslint XSS 警告)。
  • @redshift 不确定禁用警告是个好主意,因为 XSS 可能很严重,尤其是因为它来自最终用户的输入。
  • 我只是在一个页面上禁用它,该页面在内容经过事先清理后出现。
猜你喜欢
  • 2019-10-26
  • 2020-12-12
  • 2023-03-15
  • 2016-12-26
  • 2021-05-28
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多