Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cssText() method to adaptor and handle dynamic rules in HTML output. #1027

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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