@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- /* eslint-disable max-len, camelcase */
18
-
19
17
import customCSS from "!!raw-loader!./exportCustomCSS.css" ;
20
18
21
19
const cssSelectorTextClassesRegex = / \. [ \w - ] + / g;
@@ -34,15 +32,36 @@ function mutateCssText(css: string): string {
34
32
) ;
35
33
}
36
34
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
+
37
51
// naively culls unused css rules based on which classes are present in the html,
38
52
// doesn't cull rules which won't apply due to the full selector not matching but gets rid of a LOT of cruft anyway.
39
53
const getExportCSS = async ( usedClasses : Set < string > ) : Promise < string > => {
40
54
// only include bundle.css and the data-mx-theme=light styling
41
55
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 ) ;
44
57
} ) ;
45
58
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
+
46
65
let css = "" ;
47
66
for ( const stylesheet of stylesheets ) {
48
67
for ( const rule of stylesheet . cssRules ) {
0 commit comments