Skip to content

Commit

Permalink
Fix replaceUrlFunctions
Browse files Browse the repository at this point in the history
- Don't prefix data: & blob: urls
- Handle path-absolute urls correctly
  • Loading branch information
manuelmeister committed Aug 9, 2024
1 parent f451d57 commit 24ba542
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/scroll-timeline-css-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,30 @@ export class StyleParser {
this.handleScrollTimelineProps(rule, p);
}

// If this sheet has no srcURL (like from a <style> tag), we are done.
// Otherwise, we have to find `url()` functions and resolve
// relative URLs to absolute URLs.
return this.replaceUrlFunctions(p.sheetSrc, srcUrl);
}


// If this sheet has no srcURL (like from a <style> tag), we are done.
// Otherwise, we have to find `url()` functions and resolve
// relative and path-absolute URLs to absolute URLs.
replaceUrlFunctions(sheetSrc, srcUrl) {
if (!srcUrl) {
return p.sheetSrc;
return sheetSrc;
}

const srcUrlDir = srcUrl.lastIndexOf('/') > location.origin.length
const srcUrlOrigin = new URL(srcUrl).origin
const srcUrlDir = srcUrl.lastIndexOf('/') > srcUrlOrigin.length
? srcUrl.substring(0, srcUrl.lastIndexOf('/'))
: location.origin;
: srcUrlOrigin;

// replace relative paths
sheetSrc = sheetSrc.replace(/url\((?:(['"])(?!https?:\/\/|data:|blob:|\/)|(?!['"]?(?:https?:\/\/|data:|blob:|\/)))(?:\.\/)?/gm, `url($1${srcUrlDir}/`)

// replace path-absolute paths
sheetSrc = sheetSrc.replace(/url\((['"])?\//gm, `url($1${srcUrlOrigin}/`)

return p.sheetSrc.replace(/url\((["'])?(?:\.?\/|(?!https?:\/\/|(?:data|blob):))/gm, `url($1${srcUrlDir}/`);
return sheetSrc;
}

getAnimationTimelineOptions(animationName, target) {
Expand Down

0 comments on commit 24ba542

Please sign in to comment.