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
2020-03-22 00:36:20 +08:00
parent 02a33faaf9
commit 197526fbe8
5 changed files with 37 additions and 23 deletions

View File

@@ -523,19 +523,25 @@ function initInputObserver(
function initStyleSheetObserver(cb: styleSheetRuleCallback): listenerHandler { function initStyleSheetObserver(cb: styleSheetRuleCallback): listenerHandler {
const insertRule = CSSStyleSheet.prototype.insertRule; const insertRule = CSSStyleSheet.prototype.insertRule;
CSSStyleSheet.prototype.insertRule = function(rule: string, index?: number) { CSSStyleSheet.prototype.insertRule = function(rule: string, index?: number) {
cb({ const id = mirror.getId(this.ownerNode as INode);
id: mirror.getId(this.ownerNode as INode), if (id !== -1) {
adds: [{ rule, index }], cb({
}); id,
adds: [{ rule, index }],
});
}
return insertRule.apply(this, arguments); return insertRule.apply(this, arguments);
}; };
const deleteRule = CSSStyleSheet.prototype.deleteRule; const deleteRule = CSSStyleSheet.prototype.deleteRule;
CSSStyleSheet.prototype.deleteRule = function(index: number) { CSSStyleSheet.prototype.deleteRule = function(index: number) {
cb({ const id = mirror.getId(this.ownerNode as INode);
id: mirror.getId(this.ownerNode as INode), if (id !== -1) {
removes: [{ index }], cb({
}); id,
removes: [{ index }],
});
}
return deleteRule.apply(this, arguments); return deleteRule.apply(this, arguments);
}; };

View File

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

View File

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

View File

@@ -9,6 +9,8 @@ import {
listenerHandler, listenerHandler,
eventWithTime, eventWithTime,
EventType, EventType,
IncrementalSource,
styleSheetRuleData,
} from '../src/types'; } from '../src/types';
import { assertSnapshot, launchPuppeteer } from './utils'; import { assertSnapshot, launchPuppeteer } from './utils';
import { Suite } from 'mocha'; import { Suite } from 'mocha';
@@ -207,6 +209,8 @@ describe('record', function(this: ISuite) {
const styleSheet = <CSSStyleSheet>styleElement.sheet; const styleSheet = <CSSStyleSheet>styleElement.sheet;
const ruleIdx0 = styleSheet.insertRule('body { background: #000; }'); const ruleIdx0 = styleSheet.insertRule('body { background: #000; }');
const ruleIdx1 = styleSheet.insertRule('body { background: #111; }');
styleSheet.deleteRule(ruleIdx1);
setTimeout(() => { setTimeout(() => {
styleSheet.insertRule('body { color: #fff; }'); styleSheet.insertRule('body { color: #fff; }');
}, 0); }, 0);
@@ -218,7 +222,20 @@ describe('record', function(this: ISuite) {
}, 10); }, 10);
}); });
await this.page.waitFor(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'); assertSnapshot(this.events, __filename, 'stylesheet-rules');
}); });
}); });

View File

@@ -15,7 +15,8 @@
"check-format", "check-format",
"allow-leading-underscore" "allow-leading-underscore"
], ],
"arrow-parens": false "arrow-parens": false,
"only-arrow-functions": false
}, },
"rulesDirectory": [] "rulesDirectory": []
} }