From ad32a15bb19c7754c2615176046eaeb2ff07433d Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Sat, 25 Nov 2023 09:31:31 -0500 Subject: [PATCH 1/2] Add cssText() method to adaptor and handle dynamic rules in HTML output. --- ts/adaptors/HTMLAdaptor.ts | 17 +++++++++++++---- ts/adaptors/liteAdaptor.ts | 3 +-- ts/core/DOMAdaptor.ts | 13 +++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) 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 versions 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 */ From c324aa685395f1556e8e3fa6be65be4111653eee Mon Sep 17 00:00:00 2001 From: "Davide P. Cervone" Date: Tue, 28 Nov 2023 08:38:16 -0500 Subject: [PATCH 2/2] Update ts/core/DOMAdaptor.ts Fix typo in comments Co-authored-by: Volker Sorge --- ts/core/DOMAdaptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ts/core/DOMAdaptor.ts b/ts/core/DOMAdaptor.ts index 364baa3c7..ea1d12b50 100644 --- a/ts/core/DOMAdaptor.ts +++ b/ts/core/DOMAdaptor.ts @@ -345,7 +345,7 @@ export interface DOMAdaptor { /** * @param {N} node The stylesheet node whose rules are to be returned - * @return {string} The string versions of the stylesheet rules + * @return {string} The string version of the stylesheet rules */ cssText(node: N): string;