Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse and transpile external stylesheets - fixes #78. #242

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ function initPolyfill() {
return;
}

if ([...document.styleSheets].filter((s) => s.href !== null).length) {
console.warn(
'Non-Inline StyleSheets detected: ScrollTimeline polyfill currently only' +
' supports inline styles within style tags'
);
}

if (
!Reflect.defineProperty(window, 'ScrollTimeline', { value: ScrollTimeline })
) {
Expand Down
22 changes: 20 additions & 2 deletions src/scroll-timeline-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,26 @@ function initMutationObserver() {
el.innerHTML = newSrc;
}

function handleLinkedStylesheet(el) {
// TODO
function handleLinkedStylesheet(linkElement) {
// Filter only css links to external stylesheets.
if (linkElement.type != 'text/css' && linkElement.rel != 'stylesheet' || !linkElement.href) {
return;
}
const url = new URL(linkElement.href, document.baseURI);
if (url.origin != location.origin) {
// Most likely we won't be able to fetch resources from other origins.
return;
}
fetch(linkElement.getAttribute('href')).then(async (response) => {
const result = await response.text();
flackr marked this conversation as resolved.
Show resolved Hide resolved
let newSrc = parser.transpileStyleSheet(result, true);
newSrc = parser.transpileStyleSheet(result, false);
if (newSrc != result) {
const blob = new Blob([newSrc], { type: 'text/css' });
const url = URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
}
});
}

document.querySelectorAll("style").forEach((tag) => handleStyleTag(tag));
Expand Down
Loading