From 9a23571b3ec13da68f6e617e348f9d1104b1b6e1 Mon Sep 17 00:00:00 2001 From: Lewis Crichton Date: Fri, 8 Sep 2023 15:54:25 +0100 Subject: [PATCH] feat: resiliency against bad usercss --- src/components/VencordSettings/ThemesTab.tsx | 2 +- src/utils/quickCss.ts | 14 +++++++++++++- src/utils/themes/usercss/compiler.ts | 10 ++++------ src/utils/themes/usercss/index.ts | 2 +- src/utils/themes/usercss/usercss-meta.d.ts | 2 +- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/VencordSettings/ThemesTab.tsx b/src/components/VencordSettings/ThemesTab.tsx index 7b45f33f..95dcc60f 100644 --- a/src/components/VencordSettings/ThemesTab.tsx +++ b/src/components/VencordSettings/ThemesTab.tsx @@ -114,7 +114,7 @@ interface UserCSSCardProps { function UserCSSThemeCard({ theme, enabled, onChange, onDelete }: UserCSSCardProps) { return ( 0 ? "uso" : "default"); + const { vars = {}, preprocessor = Object.keys(vars).length > 0 ? "uso" : "default" } = usercssParse(themeData, fileName); - const preprocessorFn = preprocessors[usedPreprocessor]; + const preprocessorFn = preprocessors[preprocessor]; if (!preprocessorFn) { - UserCSSLogger.error("File", fileName, "requires preprocessor", usedPreprocessor, "which isn't known to Vencord"); + UserCSSLogger.error("File", fileName, "requires preprocessor", preprocessor, "which isn't known to Vencord"); return null; } @@ -82,7 +80,7 @@ export async function compileUsercss(fileName: string) { try { return await preprocessorFn(themeData, varsToPass); } catch (error) { - UserCSSLogger.error("File", fileName, "failed to compile with preprocessor", usedPreprocessor, error); + UserCSSLogger.error("File", fileName, "failed to compile with preprocessor", preprocessor, error); return null; } } diff --git a/src/utils/themes/usercss/index.ts b/src/utils/themes/usercss/index.ts index a5151e75..d5d2f389 100644 --- a/src/utils/themes/usercss/index.ts +++ b/src/utils/themes/usercss/index.ts @@ -10,7 +10,7 @@ import { parse as originalParse, UserstyleHeader } from "usercss-meta"; const UserCSSLogger = new Logger("UserCSS", "#d2acf5"); export function parse(text: string, fileName: string): UserstyleHeader { - const { metadata, errors } = originalParse(text.replace(/\r/g, "")); + var { metadata, errors } = originalParse(text.replace(/\r/g, ""), { allowErrors: true }); if (errors.length) { UserCSSLogger.warn("Parsed", fileName, "with errors:", errors); diff --git a/src/utils/themes/usercss/usercss-meta.d.ts b/src/utils/themes/usercss/usercss-meta.d.ts index 7248cbbb..d5130459 100644 --- a/src/utils/themes/usercss/usercss-meta.d.ts +++ b/src/utils/themes/usercss/usercss-meta.d.ts @@ -102,5 +102,5 @@ declare module "usercss-meta" { vars: Record; } - export function parse(text: string): { metadata: UserstyleHeader; errors: { code: string; args: any; }[] }; + export function parse(text: string, options: { allowErrors: boolean; }): { metadata: UserstyleHeader; errors: { code: string; args: any; }[]; }; }