Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 00912c0

Browse files
authored
Load light theme prior to HTML export to ensure it is present (#7643)
1 parent 79d9a0c commit 00912c0

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/utils/exportUtils/exportCSS.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
/* eslint-disable max-len, camelcase */
18-
1917
import customCSS from "!!raw-loader!./exportCustomCSS.css";
2018

2119
const cssSelectorTextClassesRegex = /\.[\w-]+/g;
@@ -34,15 +32,36 @@ function mutateCssText(css: string): string {
3432
);
3533
}
3634

35+
function isLightTheme(sheet: CSSStyleSheet): boolean {
36+
return (<HTMLStyleElement>sheet.ownerNode).dataset.mxTheme?.toLowerCase() === "light";
37+
}
38+
39+
async function getRulesFromCssFile(path: string): Promise<CSSStyleSheet> {
40+
const doc = document.implementation.createHTMLDocument("");
41+
const styleElement = document.createElement("style");
42+
43+
const res = await fetch(path);
44+
styleElement.textContent = await res.text();
45+
// the style will only be parsed once it is added to a document
46+
doc.body.appendChild(styleElement);
47+
48+
return styleElement.sheet;
49+
}
50+
3751
// naively culls unused css rules based on which classes are present in the html,
3852
// doesn't cull rules which won't apply due to the full selector not matching but gets rid of a LOT of cruft anyway.
3953
const getExportCSS = async (usedClasses: Set<string>): Promise<string> => {
4054
// only include bundle.css and the data-mx-theme=light styling
4155
const stylesheets = Array.from(document.styleSheets).filter(s => {
42-
return s.href?.endsWith("bundle.css") ||
43-
(s.ownerNode as HTMLStyleElement).dataset.mxTheme?.toLowerCase() === "light";
56+
return s.href?.endsWith("bundle.css") || isLightTheme(s);
4457
});
4558

59+
// If the light theme isn't loaded we will have to fetch & parse it manually
60+
if (!stylesheets.some(isLightTheme)) {
61+
const href = document.querySelector<HTMLLinkElement>('link[rel="stylesheet"][href$="theme-light.css"]').href;
62+
stylesheets.push(await getRulesFromCssFile(href));
63+
}
64+
4665
let css = "";
4766
for (const stylesheet of stylesheets) {
4867
for (const rule of stylesheet.cssRules) {

0 commit comments

Comments
 (0)