diff --git a/ts/adaptors/HTMLAdaptor.ts b/ts/adaptors/HTMLAdaptor.ts index 23a7b2f3a..f1cda5c6b 100644 --- a/ts/adaptors/HTMLAdaptor.ts +++ b/ts/adaptors/HTMLAdaptor.ts @@ -69,8 +69,7 @@ export interface MinHTMLElement { className: string; classList: DOMTokenList; style: OptionList; - sheet?: {insertRule: (rule: string, index?: number) => void}; - + sheet?: {insertRule: (rule: string, index?: number) => void, cssRules: Array<{cssText: string}>}; childNodes: (N | T)[] | NodeList; firstChild: N | T | Node; lastChild: N | T | Node; @@ -522,15 +521,25 @@ AbstractDOMAdaptor implements MinHTMLAdaptor { * @override */ public insertRules(node: N, rules: string[]) { - for (const rule of rules.reverse()) { + for (const rule of rules) { try { - node.sheet.insertRule(rule, 0); + node.sheet.insertRule(rule, node.sheet.cssRules.length); } catch (e) { console.warn(`MathJax: can't insert css rule '${rule}': ${e.message}`); } } } + /** + * @override + */ + public cssText(node: N) { + if (this.kind(node) !== 'style') { + return ''; + } + return Array.from(node.sheet.cssRules).map(rule => rule.cssText).join('\n'); + } + /** * @override */ diff --git a/ts/adaptors/liteAdaptor.ts b/ts/adaptors/liteAdaptor.ts index 8abf21913..9eae89cdb 100644 --- a/ts/adaptors/liteAdaptor.ts +++ b/ts/adaptors/liteAdaptor.ts @@ -34,7 +34,6 @@ import {OptionList} from '../util/Options.js'; /************************************************************/ - /** * Implements a lightweight DOMAdaptor on liteweight HTML elements */ @@ -549,7 +548,7 @@ export class LiteBase extends AbstractDOMAdaptor { */ insertRules(node: N, rules: string[]): void; + /** + * @param {N} node The stylesheet node whose rules are to be returned + * @return {string} The string version of the stylesheet rules + */ + cssText(node: N): string; + /** * @param {N} node The HTML node whose font size is to be determined * @return {number} The font size (in pixels) of the node @@ -656,6 +662,13 @@ export abstract class AbstractDOMAdaptor implements DOMAdaptor */ public abstract insertRules(node: N, rules: string[]): void; + /** + * @override + */ + public cssText(node: N) { + return (this.kind(node) === 'style' ? this.textContent(node) : ''); + }; + /** * @override */