Skip to content

Commit feb0397

Browse files
committed
Use style elements in head rather than blob links
1 parent a06dfb9 commit feb0397

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

Diff for: src/parse.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -684,28 +684,30 @@ export async function parseCSS(styleData: StyleData[]) {
684684
}
685685
}
686686

687-
for (const styleObj of styleData) {
688-
if (styleObj.changed) {
689-
let changed = false;
690-
const ast = getAST(styleObj.css);
691-
walk(ast, {
692-
visit: 'Atrule',
693-
enter(node) {
694-
changed = makeImportUrlAbsolute(node);
695-
},
696-
});
697-
walk(ast, {
698-
visit: 'Declaration',
699-
enter(node) {
700-
changed = makeDeclarationValueUrlAbsolute(node) || changed;
701-
},
702-
});
703-
if (changed) {
704-
// Update CSS
705-
styleObj.css = generateCSS(ast);
706-
}
707-
}
708-
}
687+
// for (const styleObj of styleData) {
688+
// if (styleObj.changed) {
689+
// let changed = false;
690+
// const ast = getAST(styleObj.css);
691+
// // @import rules need to be reparsed, as atrule preludes are not parsed
692+
// // by default
693+
// walk(ast, {
694+
// visit: 'Atrule',
695+
// enter(node) {
696+
// changed = makeImportUrlAbsolute(node);
697+
// },
698+
// });
699+
// walk(ast, {
700+
// visit: 'Declaration',
701+
// enter(node) {
702+
// changed = makeDeclarationValueUrlAbsolute(node) || changed;
703+
// },
704+
// });
705+
// if (changed) {
706+
// // Update CSS
707+
// styleObj.css = generateCSS(ast);
708+
// }
709+
// }
710+
// }
709711

710712
// Store inline style custom property mappings for each target element
711713
const inlineStyles = new Map<HTMLElement, Record<string, string>>();

Diff for: src/transform.ts

+6-14
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,27 @@ export async function transformCSS(
2121
el.innerHTML = css;
2222
} else if (el instanceof HTMLLinkElement) {
2323
// Create new link
24-
const blob = new Blob([css], { type: 'text/css' });
25-
const url = URL.createObjectURL(blob);
26-
const link = document.createElement('link');
24+
const styleEl = document.createElement('style');
25+
styleEl.textContent = css;
2726
for (const name of el.getAttributeNames()) {
2827
if (!name.startsWith('on') && !excludeAttributes.includes(name)) {
2928
const attr = el.getAttribute(name);
3029
if (attr !== null) {
31-
link.setAttribute(name, attr);
30+
styleEl.setAttribute(name, attr);
3231
}
3332
}
3433
}
35-
link.setAttribute('href', url);
36-
const promise = new Promise((res) => {
37-
link.onload = res;
38-
});
3934
if (!created) {
4035
// This is an existing stylesheet, so we replace it.
41-
el.insertAdjacentElement('beforebegin', link);
36+
el.insertAdjacentElement('beforebegin', styleEl);
4237
// Wait for new stylesheet to be loaded
43-
await promise;
4438
el.remove();
4539
} else {
4640
// This is a new stylesheet, so we append it.
47-
link.rel = 'stylesheet';
48-
document.head.insertAdjacentElement('beforeend', link);
41+
document.head.insertAdjacentElement('beforeend', styleEl);
4942
// Wait for new stylesheet to be loaded
50-
await promise;
5143
}
52-
updatedObject.el = link;
44+
updatedObject.el = styleEl;
5345
} else if (el.hasAttribute('data-has-inline-styles')) {
5446
// Handle inline styles
5547
const attr = el.getAttribute('data-has-inline-styles');

0 commit comments

Comments
 (0)