@@ -76,27 +76,38 @@ function getProcessorHelperClass(utilInstance) {
76
76
}
77
77
78
78
async resolveStylesheetElement ( element , stylesheetInfo , stylesheets , baseURI , options , workStyleElement , resources ) {
79
- if ( ! options . inlineStylesheetsRefs . has ( element ) ) {
80
- if ( ! options . blockStylesheets ) {
81
- stylesheets . set ( { element } , stylesheetInfo ) ;
79
+ if ( ! options . blockStylesheets ) {
80
+ stylesheets . set ( { element } , stylesheetInfo ) ;
81
+ if ( ! options . inlineStylesheetsRefs . has ( element ) ) {
82
82
if ( element . tagName . toUpperCase ( ) == "LINK" ) {
83
83
await this . resolveLinkStylesheetURLs ( stylesheetInfo , element , element . href , baseURI , options , workStyleElement , resources , stylesheets ) ;
84
84
} else {
85
85
stylesheetInfo . stylesheet = cssTree . parse ( element . textContent , { context : "stylesheet" , parseCustomProperty : true } ) ;
86
86
await this . resolveImportURLs ( stylesheetInfo , baseURI , options , workStyleElement , resources , stylesheets ) ;
87
87
}
88
+ }
89
+ } else {
90
+ if ( element . tagName . toUpperCase ( ) == "LINK" ) {
91
+ element . href = util . EMPTY_RESOURCE ;
88
92
} else {
89
- if ( element . tagName . toUpperCase ( ) == "LINK" ) {
90
- element . href = util . EMPTY_RESOURCE ;
91
- } else {
92
- element . textContent = "" ;
93
- }
93
+ element . textContent = "" ;
94
94
}
95
95
}
96
96
}
97
97
98
98
replaceStylesheets ( doc , stylesheets , options , resources ) {
99
99
const entries = Array . from ( stylesheets ) . reverse ( ) ;
100
+ const linkElements = new Map ( ) ;
101
+ Array . from ( new Set ( options . inlineStylesheetsRefs . values ( ) ) ) . forEach ( stylesheetRefIndex => {
102
+ const linkElement = doc . createElement ( "link" ) ;
103
+ linkElement . setAttribute ( "rel" , "stylesheet" ) ;
104
+ linkElement . setAttribute ( "type" , "text/css" ) ;
105
+ const name = "stylesheet_" + resources . stylesheets . size + ".css" ;
106
+ linkElement . setAttribute ( "href" , name ) ;
107
+ const content = options . inlineStylesheets . get ( stylesheetRefIndex ) ;
108
+ resources . stylesheets . set ( resources . stylesheets . size , { name, content } ) ;
109
+ linkElements . set ( stylesheetRefIndex , linkElement ) ;
110
+ } ) ;
100
111
for ( const [ key , stylesheetInfo ] of entries ) {
101
112
if ( key . urlNode ) {
102
113
const name = "stylesheet_" + resources . stylesheets . size + ".css" ;
@@ -118,13 +129,17 @@ function getProcessorHelperClass(utilInstance) {
118
129
if ( stylesheetRefIndex === undefined ) {
119
130
styleElement . textContent = this . generateStylesheetContent ( stylesheetInfo . stylesheet , options ) ;
120
131
} else {
121
- styleElement . textContent = options . inlineStylesheets . get ( stylesheetRefIndex ) ;
132
+ const linkElement = linkElements . get ( stylesheetRefIndex ) . cloneNode ( true ) ;
133
+ styleElement . replaceWith ( linkElement ) ;
134
+ key . element = linkElement ;
122
135
}
123
136
}
124
137
}
125
138
}
126
139
for ( const [ , stylesheetResource ] of resources . stylesheets ) {
127
- stylesheetResource . content = this . generateStylesheetContent ( stylesheetResource . stylesheet , options ) ;
140
+ if ( stylesheetResource . stylesheet ) {
141
+ stylesheetResource . content = this . generateStylesheetContent ( stylesheetResource . stylesheet , options ) ;
142
+ }
128
143
stylesheetResource . stylesheet = null ;
129
144
}
130
145
}
0 commit comments