Skip to content

Commit

Permalink
sanitizer: reworked
Browse files Browse the repository at this point in the history
  • Loading branch information
Batorian committed Dec 7, 2024
1 parent fb229da commit 5e0f6c8
Showing 1 changed file with 7 additions and 53 deletions.
60 changes: 7 additions & 53 deletions src/screens/reader/utils/sanitizeChapterText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,20 @@ export const sanitizeChapterText = (
chapterName: string,
html: string,
): string => {
// List of disallowed CSS properties
const disallowedCSSProperties: RegExp[] = [
/^color$/,
/^font.*$/,
/^line-height$/,
/^text-align.*$/,
/^text-indent$/,
];

// Create a transform function for the specified tags
const createTransformFunction = () => {
return (tagName: string, attribs: any) => {
if (attribs.style) {
const styles = attribs.style.split(';');
const allowedStyles = styles.filter((style: string) => {
const [property] = style.split(':');
return !disallowedCSSProperties.some(regex =>
regex.test(property.trim()),
);
});
return {
tagName: tagName,
attribs: {
...attribs,
style: allowedStyles.join(';'),
},
};
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { _style, ...rest } = attribs;
return {
tagName: tagName,
attribs: rest,
};
};
};

// List of styled tags
const styledTags = sanitizeHtml.defaults.allowedTags;

// Generate the transformTags object
const transformTags: {
[key: string]: (tagName: string, attribs: any) => any;
} = {};
styledTags.forEach(tag => {
transformTags[tag] = createTransformFunction();
});

let text = sanitizeHtml(html, {
allowedTags: sanitizeHtml.defaults.allowedTags.concat(['img', 'title']),
allowedAttributes: {
'a': ['href', 'name', 'target', 'class', 'id'],
'div': ['class', 'id', 'style'],
'div': ['class', 'id'],
'img': ['src', 'srcset', 'alt', 'title', 'class', 'id'],
'ol': ['reversed', 'start'],
'p': ['class', 'id', 'style'],
'span': ['class', 'id', 'style'],
'ol': ['reversed', 'start', 'type'],
'p': ['class', 'id'],
'span': ['class', 'id'],
},
allowedSchemes: ['data', 'http', 'https', 'file'],
transformTags: transformTags,
allowedSchemesByTag: {
img: ['http', 'https', 'data', 'file', 'blob'],
},
});
return (
text ||
Expand Down

0 comments on commit 5e0f6c8

Please sign in to comment.