vue项目中html文本安全性过滤

vue项目中可能会使用到 **v-html="htmlContent"**来展示富文本内容,对html文本即htmlContent进行安全性过滤:

javascript 复制代码
// html安全性过滤
<template>
    <div v-html="safeHtmlContent(htmlContent)"></div
</template>
<script>
import sanitizeHtml from 'sanitize-html';

export default {
  data() {
    return {
        htmlContent: '', // html文本
    }
  },
  methods: {
    safeHtmlContent(html) {
      const allowedTags = [
        'br',
        'p',
        'span',
        'strong'
      ];
      const rule = {
        allowedTags,
        allowedAttributes: {
        '*': ['data-id', 'class', 'style', 'contenteditable'],
        }
      };
      let realHtml = html?.replace(/\n/gm, '<br />');
      const safeContent = sanitizeHtml(realHtml, rule);
      return safeContent;
    }
  }
}
</script>

记录于2024-08-17

相关推荐
夕水24 分钟前
我给我的诗写了个后台,顺手写了 一个组件库ew-solid-ui
前端·javascript·typescript
狗头大军之江苏分军1 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬1 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust
parade岁月2 小时前
VTable Guild 3.0.0 发布,面向 Vue 3 的 ProTable 式业务表格
前端
xurileidian2 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python
掘金安东尼3 小时前
更多审查智能体,更少盲区:GitHub Copilot 代码审查是怎么做的
前端·github copilot
Hilaku5 小时前
2026 年了,jQuery 真的该死了吗?
前端·javascript·程序员
zach5 小时前
吃透 React setState 同步异步之谜:从批量更新、事件循环到版本演进的完整拆解
前端·react.js
不服老的小黑哥6 小时前
02-从 pnpm dev 到 Spring Boot 启动:后端服务到底怎么跑起来?
前端
htzyl2066 小时前
javaScript——this 的四种绑定:为什么箭头函数改不了它
前端·javascript