Skip to content

Commit 02cd801

Browse files
replace duplicate style tags with link tags (see gildas-lormeau/SingleFile#1514 and gildas-lormeau/SingleFile#1437)
1 parent 6f78bbe commit 02cd801

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

core/lib/processor-helper.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,38 @@ function getProcessorHelperClass(utilInstance) {
7676
}
7777

7878
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)) {
8282
if (element.tagName.toUpperCase() == "LINK") {
8383
await this.resolveLinkStylesheetURLs(stylesheetInfo, element, element.href, baseURI, options, workStyleElement, resources, stylesheets);
8484
} else {
8585
stylesheetInfo.stylesheet = cssTree.parse(element.textContent, { context: "stylesheet", parseCustomProperty: true });
8686
await this.resolveImportURLs(stylesheetInfo, baseURI, options, workStyleElement, resources, stylesheets);
8787
}
88+
}
89+
} else {
90+
if (element.tagName.toUpperCase() == "LINK") {
91+
element.href = util.EMPTY_RESOURCE;
8892
} else {
89-
if (element.tagName.toUpperCase() == "LINK") {
90-
element.href = util.EMPTY_RESOURCE;
91-
} else {
92-
element.textContent = "";
93-
}
93+
element.textContent = "";
9494
}
9595
}
9696
}
9797

9898
replaceStylesheets(doc, stylesheets, options, resources) {
9999
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+
});
100111
for (const [key, stylesheetInfo] of entries) {
101112
if (key.urlNode) {
102113
const name = "stylesheet_" + resources.stylesheets.size + ".css";
@@ -118,13 +129,17 @@ function getProcessorHelperClass(utilInstance) {
118129
if (stylesheetRefIndex === undefined) {
119130
styleElement.textContent = this.generateStylesheetContent(stylesheetInfo.stylesheet, options);
120131
} else {
121-
styleElement.textContent = options.inlineStylesheets.get(stylesheetRefIndex);
132+
const linkElement = linkElements.get(stylesheetRefIndex).cloneNode(true);
133+
styleElement.replaceWith(linkElement);
134+
key.element = linkElement;
122135
}
123136
}
124137
}
125138
}
126139
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+
}
128143
stylesheetResource.stylesheet = null;
129144
}
130145
}

0 commit comments

Comments
 (0)