Skip to content

Commit

Permalink
Sanitize css before sending to anki
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuuuube committed Dec 29, 2024
1 parent 5961c3b commit 74978b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions ext/js/core/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,13 @@ export function deferPromise() {
export function promiseTimeout(delay) {
return delay <= 0 ? Promise.resolve() : new Promise((resolve) => { setTimeout(resolve, delay); });
}

/**
* @param {string} css
* @returns {string}
*/
export function sanitizeCSS(css) {
const sanitizer = new CSSStyleSheet();
sanitizer.replaceSync(css);
return Array.from(sanitizer.cssRules).map(rule => rule.cssText || '').join('\n');
}
4 changes: 2 additions & 2 deletions ext/js/data/anki-note-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import {ExtensionError} from '../core/extension-error.js';
import {deferPromise} from '../core/utilities.js';
import {deferPromise, sanitizeCSS} from '../core/utilities.js';
import {convertHiraganaToKatakana, convertKatakanaToHiragana} from '../language/ja/japanese.js';
import {cloneFieldMarkerPattern, getRootDeckName} from './anki-util.js';

Expand Down Expand Up @@ -192,7 +192,7 @@ export class AnkiNoteBuilder {
for (const dictionary of dictionaries) {
const {name, styles} = dictionary;
if (typeof styles === 'string') {
styleMap.set(name, styles);
styleMap.set(name, sanitizeCSS(styles));
}
}
return styleMap;
Expand Down

0 comments on commit 74978b2

Please sign in to comment.