Skip to content

fix: improve nested CSS rule handling and add related tests#1775

Merged
Juice10 merged 7 commits intorrweb-io:masterfrom
alailsonko:fix/nested-css-rule-positions
Feb 6, 2026
Merged

fix: improve nested CSS rule handling and add related tests#1775
Juice10 merged 7 commits intorrweb-io:masterfrom
alailsonko:fix/nested-css-rule-positions

Conversation

@alailsonko
Copy link
Contributor

This pull request improves the handling and testing of nested CSS rule manipulations in rrweb. It fixes the traversal logic for nested CSS rules, enhances the utility function for retrieving nested rules, and adds comprehensive tests to ensure correctness when dealing with complex CSS structures (such as rules inside @media and @supports blocks). Additionally, new test events and integration tests are introduced to verify that style changes on nested rules are correctly replayed.

Bug fixes and core logic improvements:

  • Fixed traversal in getNestedCSSRulePositions to ensure correct recursive parent rule lookup for nested CSS rules.
  • Corrected the logic in getNestedRule to properly handle position arrays when accessing nested rules, ensuring accurate retrieval for any nesting depth.

Testing enhancements:

  • Added a new test event file (nested-style-declaration.ts) that simulates style property changes (set/remove) on nested CSS rules, including deep nesting scenarios.
  • Integrated these nested style declaration events into the main replay test suite, and added a comprehensive test that verifies rrweb's ability to replay style changes on nested rules at various points in time. [1] [2]
  • Expanded unit tests for the getNestedRule and getPositionsAndIndex utility functions to cover a wide range of nesting and edge cases, ensuring robustness of these utilities. [1] [2]

Copilot AI review requested due to automatic review settings January 26, 2026 11:56
@changeset-bot
Copy link

changeset-bot bot commented Jan 26, 2026

🦋 Changeset detected

Latest commit: d8b10d4

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

This PR includes changesets to release 19 packages
Name Type
rrweb Patch
rrweb-snapshot 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
Contributor

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 fixes critical bugs in nested CSS rule handling within rrweb's recording and replay functionality. The changes ensure that style modifications on rules nested inside CSS at-rules (like @media and @supports) are correctly captured during recording and accurately replayed.

Changes:

  • Fixed recursive traversal bug in getNestedCSSRulePositions that caused incorrect position tracking for nested CSS rules
  • Corrected indexing logic in getNestedRule that was skipping nesting levels when retrieving deeply nested rules
  • Added comprehensive unit and integration tests to verify nested CSS rule manipulation at various depths

Reviewed changes

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

Show a summary per file
File Description
packages/rrweb/src/record/observer.ts Adds missing return statement in recursive call to fix position tracking for nested CSS rules
packages/rrweb/src/utils.ts Fixes incorrect array slicing that caused getNestedRule to skip nesting levels
packages/rrweb/test/events/nested-style-declaration.ts Adds comprehensive test event data covering setProperty/removeProperty on nested rules at multiple depths
packages/rrweb/test/replayer.test.ts Adds integration test verifying correct replay of style changes on nested CSS rules
packages/rrweb/test/util.test.ts Adds unit tests for getNestedRule and getPositionsAndIndex utility functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Juice10
Juice10 previously approved these changes Feb 6, 2026
@Juice10 Juice10 merged commit b149cf3 into rrweb-io:master Feb 6, 2026
6 checks passed
pauldambra added a commit to PostHog/posthog-rrweb that referenced this pull request Mar 24, 2026
## Changes

Adopts upstream rrweb [PR #1775](rrweb-io/rrweb#1775).

Fixes bugs in how rrweb handles CSS rules nested within at-rules (`@media`, `@supports`):

- **`getNestedRule` (utils.ts)**: Fixed recursive logic that was skipping nesting levels (`slice(2)` → `slice(1)`). Added null safety so missing rules return `null` instead of throwing.
- **`getNestedCSSRulePositions` (observer.ts)**: Added missing recursive call so deeply nested rules get correct position arrays.
- **`applyStyleSheetRule` (replay/index.ts)**: Added optional chaining for `nestedRule?.insertRule()` and `nestedRule?.deleteRule()` since `getNestedRule` can now return `null`.

Part of #147.
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.

3 participants