Skip to content

Commit

Permalink
fix(editor): Fix Show details summary (#6113)
Browse files Browse the repository at this point in the history
* 🐛 Fix `Show details` summary

* 🚚 Move constants out of sanitizer
  • Loading branch information
ivov authored and netroy committed May 2, 2023
1 parent d3f4bc1 commit e12bafb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
16 changes: 16 additions & 0 deletions packages/editor-ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,19 @@ export const TEMPLATE_EXPERIMENT = {
export const EXPERIMENTS_TO_TRACK = [TEMPLATE_EXPERIMENT.name, AUTO_INSERT_ACTION_EXPERIMENT.name];

export const NODE_TYPES_EXCLUDED_FROM_OUTPUT_NAME_APPEND = [FILTER_NODE_TYPE];

export const ALLOWED_HTML_ATTRIBUTES = ['href', 'name', 'target', 'title', 'class', 'id', 'style'];

export const ALLOWED_HTML_TAGS = [
'p',
'strong',
'b',
'code',
'a',
'br',
'i',
'em',
'small',
'details',
'summary',
];
9 changes: 3 additions & 6 deletions packages/editor-ui/src/utils/htmlUtils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import xss, { friendlyAttrValue } from 'xss';
import { ALLOWED_HTML_ATTRIBUTES, ALLOWED_HTML_TAGS } from '@/constants';

/*
Constants and utility functions that help in HTML, CSS and DOM manipulation
*/

export function sanitizeHtml(dirtyHtml: string) {
const allowedAttributes = ['href', 'name', 'target', 'title', 'class', 'id'];
const allowedTags = ['p', 'strong', 'b', 'code', 'a', 'br', 'i', 'em', 'small'];

const sanitizedHtml = xss(dirtyHtml, {
onTagAttr: (tag, name, value) => {
if (tag === 'img' && name === 'src') {
Expand All @@ -19,16 +17,15 @@ export function sanitizeHtml(dirtyHtml: string) {
}
}

// Allow `allowedAttributes` and all `data-*` attributes
if (allowedAttributes.includes(name) || name.startsWith('data-')) {
if (ALLOWED_HTML_ATTRIBUTES.includes(name) || name.startsWith('data-')) {
return `${name}="${friendlyAttrValue(value)}"`;
}

return;
// Return nothing, means keep the default handling measure
},
onTag: (tag) => {
if (!allowedTags.includes(tag)) return '';
if (!ALLOWED_HTML_TAGS.includes(tag)) return '';
return;
},
});
Expand Down

0 comments on commit e12bafb

Please sign in to comment.