@@ -18,10 +18,12 @@ function insertStylesheets(
1818/**
1919 * Inserts a single stylesheet element
2020 */
21- function insertStyles ( style : string , document : Document , el : HTMLElement ) {
22- const styleElement = document . createElement ( 'style' )
23- styleElement . appendChild ( document . createTextNode ( style ) )
24- document . body . insertBefore ( styleElement , el )
21+ function insertStyles ( styles : string [ ] , document : Document , el : HTMLElement ) {
22+ styles . forEach ( style => {
23+ const styleElement = document . createElement ( 'style' )
24+ styleElement . appendChild ( document . createTextNode ( style ) )
25+ document . body . insertBefore ( styleElement , el )
26+ } )
2527}
2628
2729function insertSingleCssFile (
@@ -59,26 +61,43 @@ export const injectStylesBeforeElement = (
5961 document : Document ,
6062 el : HTMLElement ,
6163) => {
62- // insert any custom global styles before the component
64+ // first insert all stylesheets as Link elements
65+ const stylesheets = options . stylesheet ? [ options . stylesheet ] : [ ]
66+
6367 if ( typeof options . stylesheets === 'string' ) {
6468 options . stylesheets = [ options . stylesheets ]
6569 }
66- if ( Array . isArray ( options . stylesheets ) ) {
67- insertStylesheets ( options . stylesheets , document , el )
70+ if ( options . stylesheets ) {
71+ stylesheets . concat ( options . stylesheets )
6872 }
6973
70- if ( options . style ) {
71- insertStyles ( options . style , document , el )
74+ insertStylesheets ( stylesheets , document , el )
75+
76+ // insert any styles as <style>...</style> elements
77+ const styles = [ ]
78+ if ( typeof options . style === 'string' ) {
79+ styles . push ( options . style )
80+ }
81+ if ( typeof options . styles === 'string' ) {
82+ styles . push ( options . styles )
83+ } else if ( Array . isArray ( options . styles ) ) {
84+ styles . concat ( options . styles )
7285 }
7386
74- if ( Array . isArray ( options . cssFiles ) ) {
75- return insertLocalCssFiles ( options . cssFiles , document , el )
87+ insertStyles ( styles , document , el )
88+
89+ // now load any css files by path and add their content
90+ // as <style>...</style> elements
91+ const cssFiles = [ ]
92+
93+ if ( typeof options . cssFile === 'string' ) {
94+ cssFiles . push ( options . cssFile )
7695 }
7796
7897 if ( typeof options . cssFiles === 'string' ) {
79- return insertSingleCssFile ( options . cssFiles , document , el )
98+ cssFiles . push ( options . cssFiles )
99+ } else if ( Array . isArray ( options . cssFiles ) ) {
100+ cssFiles . concat ( options . cssFiles )
80101 }
81-
82- // force this function to have a return value from all branches
83- return cy . wrap ( null )
102+ return insertLocalCssFiles ( cssFiles , document , el )
84103}
0 commit comments