Skip to content

Commit

Permalink
Merge pull request #1027 from mathjax/adaptor-csstext
Browse files Browse the repository at this point in the history
Add cssText() method to adaptor and handle dynamic rules in HTML output.
  • Loading branch information
dpvc authored Nov 28, 2023
2 parents 18a078b + c324aa6 commit ae389de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
17 changes: 13 additions & 4 deletions ts/adaptors/HTMLAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ export interface MinHTMLElement<N, T> {
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;
Expand Down Expand Up @@ -522,15 +521,25 @@ AbstractDOMAdaptor<N, T, D> implements MinHTMLAdaptor<N, T, D> {
* @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
*/
Expand Down
3 changes: 1 addition & 2 deletions ts/adaptors/liteAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {OptionList} from '../util/Options.js';

/************************************************************/


/**
* Implements a lightweight DOMAdaptor on liteweight HTML elements
*/
Expand Down Expand Up @@ -549,7 +548,7 @@ export class LiteBase extends AbstractDOMAdaptor<LiteElement, LiteText, LiteDocu
* @override
*/
public insertRules(node: LiteElement, rules: string[]) {
node.children = [this.text(rules.join('\n\n') + '\n\n' + this.textContent(node))];
node.children = [this.text(this.textContent(node) + '\n\n' + rules.join('\n\n'))];
}

/*******************************************************************/
Expand Down
13 changes: 13 additions & 0 deletions ts/core/DOMAdaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ export interface DOMAdaptor<N, T, D> {
*/
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
Expand Down Expand Up @@ -656,6 +662,13 @@ export abstract class AbstractDOMAdaptor<N, T, D> implements DOMAdaptor<N, T, D>
*/
public abstract insertRules(node: N, rules: string[]): void;

/**
* @override
*/
public cssText(node: N) {
return (this.kind(node) === 'style' ? this.textContent(node) : '');
};

/**
* @override
*/
Expand Down

0 comments on commit ae389de

Please sign in to comment.