Ability to decode HTML Entities when all tags are stripped. Fixes #929

This commit is contained in:
barry 2022-08-30 19:20:35 -04:00
parent 149f52b33c
commit 3194b4cd87
2 changed files with 2249 additions and 3 deletions

2235
server/utils/htmlEntities.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,5 @@
const sanitizeHtml = require('../libs/sanitizeHtml') const sanitizeHtml = require('../libs/sanitizeHtml')
const {entities} = require("./htmlEntities");
function sanitize(html) { function sanitize(html) {
const sanitizerOptions = { const sanitizerOptions = {
@ -17,12 +18,22 @@ function sanitize(html) {
} }
module.exports.sanitize = sanitize module.exports.sanitize = sanitize
function stripAllTags(html) { function stripAllTags(html, shouldDecodeEntities = true) {
const sanitizerOptions = { const sanitizerOptions = {
allowedTags: [], allowedTags: [],
disallowedTagsMode: 'discard' disallowedTagsMode: 'discard'
} }
return sanitizeHtml(html, sanitizerOptions) let sanitized = sanitizeHtml(html, sanitizerOptions)
return shouldDecodeEntities ? decodeHTMLEntities(sanitized) : sanitized
} }
module.exports.stripAllTags = stripAllTags module.exports.stripAllTags = stripAllTags
function decodeHTMLEntities(strToDecode) {
return strToDecode.replace(/\&([^;]+);/g, function (entity) {
if (entity in entities) {
return entities[entity]
}
return entity;
})
}