Skip to content

fix: use document.baseURI for sheetHref when handling inline styles #1700

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

Conversation

megboehlert
Copy link

Prioritize using the href from base tag (if present) when stringifying urls in inline stylesheets. Document.baseURI does just this. If a base tag is not present, it falls back to location.href.

@Copilot Copilot AI review requested due to automatic review settings June 5, 2025 18:26
Copy link

changeset-bot bot commented Jun 5, 2025

🦋 Changeset detected

Latest commit: 43689b7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 19 packages
Name Type
rrweb-snapshot Patch
rrweb Patch
rrdom Patch
rrdom-nodejs Patch
rrweb-player Patch
@rrweb/all Patch
@rrweb/replay Patch
@rrweb/record Patch
@rrweb/types Patch
@rrweb/packer Patch
@rrweb/utils Patch
@rrweb/web-extension Patch
rrvideo Patch
@rrweb/rrweb-plugin-console-record Patch
@rrweb/rrweb-plugin-console-replay Patch
@rrweb/rrweb-plugin-sequential-id-record Patch
@rrweb/rrweb-plugin-sequential-id-replay Patch
@rrweb/rrweb-plugin-canvas-webrtc-record Patch
@rrweb/rrweb-plugin-canvas-webrtc-replay Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the logic for handling inline stylesheets by using document.baseURI to resolve URLs in inline styles.

  • Updated utils.ts to use baseURI over location.href for inline <style> elements.
  • Added comprehensive test cases in utils.test.ts to cover scenarios when rules are missing, using cssRules, and verifying URL absolutification with baseURI.
  • Included a changeset file detailing the patch update for rrweb-snapshot.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/rrweb-snapshot/test/utils.test.ts Added test cases for the new stringifyStylesheet behavior
packages/rrweb-snapshot/src/utils.ts Modified inline stylesheet URL resolution to use ownerDocument.baseURI instead of location.href
.changeset/twelve-nails-occur.md Documented the changes and the rationale behind prioritizing the base href resolution

@megboehlert megboehlert changed the title use document.baseURI for sheetHref when handling inline styles fix: use document.baseURI for sheetHref when handling inline styles Jun 5, 2025
pauldambra added a commit to PostHog/posthog-rrweb that referenced this pull request Jun 11, 2025
pauldambra added a commit to PostHog/posthog-rrweb that referenced this pull request Jun 11, 2025
@@ -120,7 +120,7 @@ export function stringifyStylesheet(s: CSSStyleSheet): string | null {
let sheetHref = s.href;
if (!sheetHref && s.ownerNode && s.ownerNode.ownerDocument) {
// an inline <style> element
sheetHref = s.ownerNode.ownerDocument.location.href;
sheetHref = s.ownerNode.ownerDocument.baseURI;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sheetHref = s.ownerNode.ownerDocument.baseURI;
sheetHref = s.ownerNode.baseURI;

I don't think the ownerDocument bit is necessary anymore!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call out! I opened a new PR with this change.

eoghanmurray
eoghanmurray previously approved these changes Jun 13, 2025
@megboehlert megboehlert force-pushed the mb-fix-stylesheet-href-when-base-tag-present branch from 5e2d383 to 43689b7 Compare June 17, 2025 14:07
@megboehlert
Copy link
Author

new PR with requested changes here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants