Skip to content

Commit 1cc7164

Browse files
committed
fix #56
safe access tagName of form element
1 parent b6ca577 commit 1cc7164

File tree

3 files changed

+75
-66
lines changed

3 files changed

+75
-66
lines changed

src/snapshot.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ function genId(): number {
1818
return _id++;
1919
}
2020

21-
function getValidTagName(tagName: string): string {
22-
const processedTagName = tagName.toLowerCase().trim();
21+
function getValidTagName(element: HTMLElement): string {
22+
if (element instanceof HTMLFormElement) {
23+
return 'form';
24+
}
25+
26+
const processedTagName = element.tagName.toLowerCase().trim();
2327

2428
if (tagNameRegex.test(processedTagName)) {
2529
// if the tag name is odd and we cannot extract
@@ -223,7 +227,7 @@ function serializeNode(
223227
blockClass,
224228
blockSelector,
225229
);
226-
const tagName = getValidTagName((n as HTMLElement).tagName);
230+
const tagName = getValidTagName(n as HTMLElement);
227231
let attributes: attributes = {};
228232
for (const { name, value } of Array.from((n as HTMLElement).attributes)) {
229233
attributes[name] = transformAttribute(doc, name, value);

test/__snapshots__/integration.ts.snap

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,36 @@ exports[`[html file]: dynamic-stylesheet.html 1`] = `
102102

103103
exports[`[html file]: form-fields.html 1`] = `
104104
"<!DOCTYPE html><html xmlns=\\"http://www.w3.org/1999/xhtml\\" lang=\\"en\\"><head>
105-
<meta charset=\\"UTF-8\\" />
106-
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
107-
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
108-
<title>form fields</title>
109-
</head><body>
110-
<form>
111-
<label for=\\"text\\">
112-
<input type=\\"text\\" value=\\"1\\" />
113-
</label>
114-
<label for=\\"radio\\">
115-
<input type=\\"radio\\" checked=\\"\\" />
116-
</label>
117-
<label for=\\"checkbox\\">
118-
<input type=\\"checkbox\\" checked=\\"\\" />
119-
</label>
120-
<label for=\\"textarea\\">
121-
<textarea name=\\"\\" id=\\"\\" cols=\\"30\\" rows=\\"10\\">1234</textarea>
122-
</label>
123-
<label for=\\"select\\">
124-
<select name=\\"\\" id=\\"\\" value=\\"2\\">
125-
<option value=\\"1\\">1</option>
126-
<option value=\\"2\\" selected=\\"\\">2</option>
127-
</select>
128-
</label>
129-
</form><noscript>SCRIPT_PLACEHOLDER</noscript>
130-
</body></html>"
105+
<meta charset=\\"UTF-8\\" />
106+
<meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0\\" />
107+
<meta http-equiv=\\"X-UA-Compatible\\" content=\\"ie=edge\\" />
108+
<title>form fields</title>
109+
</head> <body>
110+
<form>
111+
<label for=\\"text\\">
112+
<input type=\\"text\\" value=\\"1\\" />
113+
</label>
114+
<label for=\\"radio\\">
115+
<input type=\\"radio\\" checked=\\"\\" />
116+
</label>
117+
<label for=\\"checkbox\\">
118+
<input type=\\"checkbox\\" checked=\\"\\" />
119+
</label>
120+
<label for=\\"textarea\\">
121+
<textarea name=\\"\\" id=\\"\\" cols=\\"30\\" rows=\\"10\\">1234</textarea>
122+
</label>
123+
<label for=\\"select\\">
124+
<select name=\\"\\" id=\\"\\" value=\\"2\\">
125+
<option value=\\"1\\">1</option>
126+
<option value=\\"2\\" selected=\\"\\">2</option>
127+
</select>
128+
</label>
129+
<label>
130+
<input name=\\"tagName\\" />
131+
</label>
132+
</form>
133+
134+
<noscript>SCRIPT_PLACEHOLDER</noscript></body></html>"
131135
`;
132136

133137
exports[`[html file]: hover.html 1`] = `

test/html/form-fields.html

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
7+
<title>form fields</title>
8+
</head>
39

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>form fields</title>
9-
</head>
10-
11-
<body>
12-
<form>
13-
<label for="text">
14-
<input type="text">
15-
</label>
16-
<label for="radio">
17-
<input type="radio">
18-
</label>
19-
<label for="checkbox">
20-
<input type="checkbox">
21-
</label>
22-
<label for="textarea">
23-
<textarea name="" id="" cols="30" rows="10"></textarea>
24-
</label>
25-
<label for="select">
26-
<select name="" id="">
27-
<option value="1">1</option>
28-
<option value="2">2</option>
29-
</select>
30-
</label>
31-
</form>
32-
</body>
33-
<script>
34-
document.querySelector('input[type="text"]').value = '1';
35-
document.querySelector('input[type="radio"]').checked = true;
36-
document.querySelector('input[type="checkbox"]').checked = true;
37-
document.querySelector('textarea').value = '1234';
38-
document.querySelector('select').value = '2';
39-
</script>
40-
10+
<body>
11+
<form>
12+
<label for="text">
13+
<input type="text" />
14+
</label>
15+
<label for="radio">
16+
<input type="radio" />
17+
</label>
18+
<label for="checkbox">
19+
<input type="checkbox" />
20+
</label>
21+
<label for="textarea">
22+
<textarea name="" id="" cols="30" rows="10"></textarea>
23+
</label>
24+
<label for="select">
25+
<select name="" id="">
26+
<option value="1">1</option>
27+
<option value="2">2</option>
28+
</select>
29+
</label>
30+
<label>
31+
<input name="tagName" />
32+
</label>
33+
</form>
34+
</body>
35+
<script>
36+
document.querySelector('input[type="text"]').value = '1';
37+
document.querySelector('input[type="radio"]').checked = true;
38+
document.querySelector('input[type="checkbox"]').checked = true;
39+
document.querySelector('textarea').value = '1234';
40+
document.querySelector('select').value = '2';
41+
</script>
4142
</html>

0 commit comments

Comments
 (0)