mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-02-03 12:47:35 +01:00
Use recursive stylesheet sanitation
This commit is contained in:
parent
1bc45d2869
commit
8726def6e0
@ -465,18 +465,7 @@ export function decodeStyleTags(text) {
|
||||
const styleDecodeRegex = /<custom-style>(.+?)<\/custom-style>/gms;
|
||||
const mediaAllowed = isExternalMediaAllowed();
|
||||
|
||||
return text.replaceAll(styleDecodeRegex, (_, style) => {
|
||||
try {
|
||||
let styleCleaned = unescape(style).replaceAll(/<br\/>/g, '');
|
||||
const ast = css.parse(styleCleaned);
|
||||
const rules = ast?.stylesheet?.rules;
|
||||
if (rules) {
|
||||
for (const rule of rules) {
|
||||
if (rule.type === 'import') {
|
||||
rules.splice(rules.indexOf(rule), 1);
|
||||
}
|
||||
|
||||
if (rule.type === 'rule') {
|
||||
function sanitizeRule(rule) {
|
||||
if (rule.selectors) {
|
||||
for (let i = 0; i < rule.selectors.length; i++) {
|
||||
let selector = rule.selectors[i];
|
||||
@ -500,7 +489,28 @@ export function decodeStyleTags(text) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function sanitizeRuleSet(ruleSet) {
|
||||
if (ruleSet.type === 'rule') {
|
||||
sanitizeRule(ruleSet);
|
||||
}
|
||||
|
||||
if (Array.isArray(ruleSet.rules)) {
|
||||
ruleSet.rules = ruleSet.rules.filter(rule => rule.type !== 'import');
|
||||
|
||||
for (const mediaRule of ruleSet.rules) {
|
||||
sanitizeRuleSet(mediaRule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return text.replaceAll(styleDecodeRegex, (_, style) => {
|
||||
try {
|
||||
let styleCleaned = unescape(style).replaceAll(/<br\/>/g, '');
|
||||
const ast = css.parse(styleCleaned);
|
||||
const sheet = ast?.stylesheet;
|
||||
if (sheet) {
|
||||
sanitizeRuleSet(ast.stylesheet);
|
||||
}
|
||||
return `<style>${css.stringify(ast)}</style>`;
|
||||
} catch (error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user