Skip to content

Commit e3a73e9

Browse files
committed
Reliably generate mutations while the snapshot is iterating in order to test rare occurrences. Have added what I believe should be the correct parsing
1 parent c1c687a commit e3a73e9

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

src/snapshot.ts

+7
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,13 @@ export function serializeNodeWithId(
525525
serializedNode.type === NodeType.Element) &&
526526
recordChild
527527
) {
528+
// MUTATION TEST
529+
if ((n as HTMLElement).dataset &&
530+
(n as HTMLElement).dataset.mutatefn &&
531+
typeof (n as HTMLElement).dataset.mutatefn === 'string') {
532+
eval((n as HTMLElement).dataset.mutatefn || '');
533+
}
534+
// END MUTATION TEST (this block should be removed by compiler!)
528535
if (
529536
slimDOMOptions.headWhitespace &&
530537
_serializedNode.type === NodeType.Element &&

test/__snapshots__/integration.ts.snap

+46
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,52 @@ exports[`[html file]: invalid-tagname.html 1`] = `
202202
</body></html>"
203203
`;
204204

205+
exports[`[html file]: mutating.html 1`] = `
206+
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
207+
<meta charset=\\"UTF-8\\" />
208+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
209+
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
210+
<title>Document</title> <noscript>SCRIPT_PLACEHOLDER</noscript></head><body>
211+
<div data-mutatefn=\\"swapChildren()\\">
212+
<!-- does see in sequence; 1, then 2 -->
213+
<b id=\\"a1\\"></b><b id=\\"a2\\"></b>
214+
215+
</div>
216+
<div>
217+
<!-- should see in sequence but doesn't -->
218+
<b data-mutatefn=\\"swapForwardSiblings()\\"></b>
219+
<b id=\\"b1\\"></b><b id=\\"b2\\"></b>
220+
221+
</div>
222+
<div>
223+
<b id=\\"d1\\"></b>
224+
<b data-mutatefn=\\"moveForwardSiblingBehind()\\" id=\\"d2\\"></b>
225+
<!-- d3 shouldn't be visible -->
226+
</div>
227+
<div data-mutatefn=\\"swapInLongDistance()\\">
228+
<!-- a1 shouldn't be here as it's already in tree -->
229+
<b id=\\"e2\\"></b>
230+
</div></body></html>"
231+
`;
232+
233+
exports[`[html file]: mutating.html~ 1`] = `
234+
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
235+
<meta charset=\\"UTF-8\\" />
236+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
237+
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
238+
<title>Document</title> <noscript>SCRIPT_PLACEHOLDER</noscript></head><body>
239+
<div>
240+
<b data-mutate=\\"swap1\\" id=\\"a1\\"></b>
241+
<b id=\\"a2\\"></b>
242+
<b id=\\"a3\\"></b>
243+
</div>
244+
<div>
245+
<b id=\\"b1\\"></b>
246+
<b data-mutate=\\"swap2\\" id=\\"b2\\"></b>
247+
<b id=\\"b3\\"></b>
248+
</div></body></html>"
249+
`;
250+
205251
exports[`[html file]: picture.html 1`] = `
206252
"<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><head></head><body>
207253
<picture>

test/html/mutating.html

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
10+
<script>
11+
function swapChildren() {
12+
var a1 = document.getElementById('a1');
13+
var a2 = document.getElementById('a2');
14+
a1.parentNode.insertBefore(a1, a2);
15+
}
16+
function swapForwardSiblings() {
17+
var b2 = document.getElementById('b2');
18+
var b1 = document.getElementById('b1');
19+
b2.parentNode.insertBefore(b1, b2);
20+
}
21+
function moveForwardSiblingBehind() {
22+
var d1 = document.getElementById('d1');
23+
var d3 = document.getElementById('d3');
24+
d1.parentNode.insertBefore(d3, d1);
25+
}
26+
function swapInLongDistance() {
27+
var a1 = document.getElementById('a1');
28+
var e2 = document.getElementById('e2');
29+
e2.parentNode.insertBefore(a1, e2);
30+
}
31+
</script>
32+
33+
</head>
34+
35+
<body>
36+
<div data-mutatefn="swapChildren()">
37+
<!-- does see in sequence; 1, then 2 -->
38+
<b id="a2"></b>
39+
<b id="a1"></b>
40+
</div>
41+
<div>
42+
<!-- should see in sequence but doesn't -->
43+
<b data-mutatefn="swapForwardSiblings()"></b>
44+
<b id="b2"></b>
45+
<b id="b1"></b>
46+
</div>
47+
<div>
48+
<b id="d1"></b>
49+
<b data-mutatefn="moveForwardSiblingBehind()" id="d2"></b>
50+
<!-- d3 shouldn't be visible -->
51+
<b id="d3"></b>
52+
</div>
53+
<div data-mutatefn="swapInLongDistance()">
54+
<!-- a1 shouldn't be here as it's already in tree -->
55+
<b id="e2"></b>
56+
</div>
57+
</body>
58+
59+
</html>

0 commit comments

Comments
 (0)