Skip to content

Commit d6b57fc

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 d9d3e54 commit d6b57fc

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

src/snapshot.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,13 @@ export function serializeNodeWithId(
724724
serializedNode.type === NodeType.Element) &&
725725
recordChild
726726
) {
727+
// MUTATION TEST
728+
if ((n as HTMLElement).dataset &&
729+
(n as HTMLElement).dataset.mutatefn &&
730+
typeof (n as HTMLElement).dataset.mutatefn === 'string') {
731+
eval((n as HTMLElement).dataset.mutatefn || '');
732+
}
733+
// END MUTATION TEST (this block should be removed by compiler!)
727734
if (
728735
slimDOMOptions.headWhitespace &&
729736
_serializedNode.type === NodeType.Element &&

test/__snapshots__/integration.ts.snap

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ exports[`[html file]: form-fields.html 1`] = `
130130
<input name=\\"tagName\\" />
131131
</label>
132132
</form>
133-
133+
134134
<noscript>SCRIPT_PLACEHOLDER</noscript></body></html>"
135135
`;
136136

@@ -217,6 +217,52 @@ exports[`[html file]: mask-text.html 1`] = `
217217
</body></html>"
218218
`;
219219
220+
exports[`[html file]: mutating.html 1`] = `
221+
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
222+
<meta charset=\\"UTF-8\\" />
223+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
224+
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
225+
<title>Document</title> <noscript>SCRIPT_PLACEHOLDER</noscript></head><body>
226+
<div data-mutatefn=\\"swapChildren()\\">
227+
<!-- does see in sequence; 1, then 2 -->
228+
<b id=\\"a1\\"></b><b id=\\"a2\\"></b>
229+
230+
</div>
231+
<div>
232+
<!-- should see in sequence but doesn't -->
233+
<b data-mutatefn=\\"swapForwardSiblings()\\"></b>
234+
<b id=\\"b1\\"></b><b id=\\"b2\\"></b>
235+
236+
</div>
237+
<div>
238+
<b id=\\"d1\\"></b>
239+
<b data-mutatefn=\\"moveForwardSiblingBehind()\\" id=\\"d2\\"></b>
240+
<!-- d3 shouldn't be visible -->
241+
</div>
242+
<div data-mutatefn=\\"swapInLongDistance()\\">
243+
<!-- a1 shouldn't be here as it's already in tree -->
244+
<b id=\\"e2\\"></b>
245+
</div></body></html>"
246+
`;
247+
248+
exports[`[html file]: mutating.html~ 1`] = `
249+
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
250+
<meta charset=\\"UTF-8\\" />
251+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
252+
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
253+
<title>Document</title> <noscript>SCRIPT_PLACEHOLDER</noscript></head><body>
254+
<div>
255+
<b data-mutate=\\"swap1\\" id=\\"a1\\"></b>
256+
<b id=\\"a2\\"></b>
257+
<b id=\\"a3\\"></b>
258+
</div>
259+
<div>
260+
<b id=\\"b1\\"></b>
261+
<b data-mutate=\\"swap2\\" id=\\"b2\\"></b>
262+
<b id=\\"b3\\"></b>
263+
</div></body></html>"
264+
`;
265+
220266
exports[`[html file]: picture.html 1`] = `
221267
"<html xmlns=\\"http://www.w3.org/1999/xhtml\\"><head></head><body>
222268
<picture>

test/html/mutating.html

Lines changed: 59 additions & 0 deletions
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)