ignore style sheet changes before the target DOM was serialized

The serialized DOM will contains all the styles, so this looks safe.
This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent 79a7191916
commit bd5aa59589
5 changed files with 37 additions and 23 deletions

View File

@@ -399,18 +399,6 @@ exports[`stylesheet-rules 1`] = `
}
}
},
{
\\"type\\": 3,
\\"data\\": {
\\"source\\": 8,
\\"id\\": -1,
\\"adds\\": [
{
\\"rule\\": \\"body { background: #000; }\\"
}
]
}
},
{
\\"type\\": 3,
\\"data\\": {

View File

@@ -11,6 +11,8 @@ interface ISuite extends Suite {
}
describe('record integration tests', function(this: ISuite) {
this.timeout(10_000);
const getHtml = (fileName: string, options: recordOptions = {}): string => {
const filePath = path.resolve(__dirname, `./html/${fileName}`);
const html = fs.readFileSync(filePath, 'utf8');
@@ -214,4 +216,4 @@ describe('record integration tests', function(this: ISuite) {
const snapshots = await page.evaluate('window.snapshots');
assertSnapshot(snapshots, __filename, 'react-styled-components');
});
}).timeout(10000);
});

View File

@@ -9,6 +9,8 @@ import {
listenerHandler,
eventWithTime,
EventType,
IncrementalSource,
styleSheetRuleData,
} from '../src/types';
import { assertSnapshot, launchPuppeteer } from './utils';
import { Suite } from 'mocha';
@@ -207,6 +209,8 @@ describe('record', function(this: ISuite) {
const styleSheet = <CSSStyleSheet>styleElement.sheet;
const ruleIdx0 = styleSheet.insertRule('body { background: #000; }');
const ruleIdx1 = styleSheet.insertRule('body { background: #111; }');
styleSheet.deleteRule(ruleIdx1);
setTimeout(() => {
styleSheet.insertRule('body { color: #fff; }');
}, 0);
@@ -218,7 +222,20 @@ describe('record', function(this: ISuite) {
}, 10);
});
await this.page.waitFor(10);
expect(this.events.length).to.equal(7);
const styleSheetRuleEvents = this.events.filter(
e =>
e.type === EventType.IncrementalSnapshot &&
e.data.source === IncrementalSource.StyleSheetRule,
);
const addRuleCount = styleSheetRuleEvents.filter(e =>
Boolean((e.data as styleSheetRuleData).adds),
).length;
const removeRuleCount = styleSheetRuleEvents.filter(e =>
Boolean((e.data as styleSheetRuleData).removes),
).length;
// sync insert/delete should be ignored
expect(addRuleCount).to.equal(2);
expect(removeRuleCount).to.equal(1);
assertSnapshot(this.events, __filename, 'stylesheet-rules');
});
});