Skip to content

Commit 509850d

Browse files
Correctly handle numbers in set-property and set-attribute commands
1 parent 204210d commit 509850d

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

src/commands/dom_modifiers.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ ${indentString(code.join('\n'), 1)}
9494
function parseSetAttribute(parser) {
9595
return innerParseCssAttribute(parser, 'attribute', 'parseSetAttributeElem', true,
9696
(key, value, kind) => {
97-
if (kind !== 'ident' && kind !== 'boolean') {
98-
return `e.setAttribute("${key}","${value.value}");`;
97+
if (kind !== 'ident') {
98+
if (kind !== 'boolean' && kind !== 'number') {
99+
return `e.setAttribute("${key}","${value.value}");`;
100+
}
101+
return `e.setAttribute("${key}",${value.value});`;
99102
}
100103
return `e.removeAttribute("${key}");`;
101104
}, false);
@@ -111,7 +114,7 @@ function parseSetProperty(parser) {
111114
const k_s = value.key.kind === 'object-path' ? key : `["${key}"]`;
112115
const arg = kind === 'ident' ? 'undefined' :
113116
// eslint-disable-next-line no-extra-parens
114-
(kind === 'boolean' ? `${value.value}` : `"${value.value}"`);
117+
(kind === 'boolean' || kind === 'number' ? `${value.value}` : `"${value.value}"`);
115118
return `setObjValue(e, ${k_s}, ${arg});`;
116119
}, true);
117120
}

tests/api-output/parseSetAttribute/basic-7.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ instructions = [
22
"""const parseSetAttributeElem = await page.$(\"a\");
33
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
44
await page.evaluate(e => {
5-
e.removeAttribute(\"b\");
5+
e.setAttribute(\"b\",true);
66
}, parseSetAttributeElem);""",
77
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instructions = [
2+
"""const parseSetAttributeElem = await page.$(\"a\");
3+
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
e.setAttribute(\"b\",12);
6+
}, parseSetAttributeElem);""",
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instructions = [
2+
"""const parseSetCssElem = await page.$(\"a\");
3+
if (parseSetCssElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
e.style[\"b\"] = \"12\";
6+
}, parseSetCssElem);""",
7+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
instructions = [
2+
"""const parseSetPropertyElem = await page.$(\"a\");
3+
if (parseSetPropertyElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
function setObjValue(object, path, value) {
6+
for (let i = 0; i < path.length - 1; ++i) {
7+
const subPath = path[i];
8+
if (object[subPath] === undefined || object[subPath] === null) {
9+
if (value === undefined) {
10+
return;
11+
}
12+
object[subPath] = {};
13+
}
14+
object = object[subPath];
15+
}
16+
if (value === undefined) {
17+
delete object[path[path.length - 1]];
18+
} else {
19+
object[path[path.length - 1]] = value;
20+
}
21+
}
22+
setObjValue(e, [\"b\"], 12);
23+
}, parseSetPropertyElem);""",
24+
]

tools/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ function checkAttributeProperty(x, func) {
559559
func('("a", {"b": null, "a": "b"})', 'basic-6');
560560
// This one will fail for `set-css`.
561561
func('("a", {"b": true})', 'basic-7');
562+
func('("a", {"b": 12})', 'basic-8');
562563

563564
// XPath
564565
func('("/a", "b", "c")', 'xpath-1');

0 commit comments

Comments
 (0)