Skip to content

Commit

Permalink
Merge pull request #575 from GuillaumeGomez/validator
Browse files Browse the repository at this point in the history
Command input validator - part 16
  • Loading branch information
GuillaumeGomez authored Mar 10, 2024
2 parents f1b87a2 + 3535ac4 commit 807d5b9
Show file tree
Hide file tree
Showing 81 changed files with 129 additions and 193 deletions.
138 changes: 62 additions & 76 deletions src/commands/dom_modifiers.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@
// Commands making changes the current page's DOM.

const { getAndSetElements, indentString, validateJson } = require('./utils.js');
const { getAndSetElements, indentString } = require('./utils.js');
const { validator } = require('../validator.js');
// Not the same `utils.js`!
const { hasError } = require('../utils.js');

function innerParseCssAttribute(parser, argName, varName, allowNullIdent, callback) {
const elems = parser.elems;

if (elems.length === 0) {
return {
'error': 'expected `("CSS selector" or "XPath", JSON)`, found nothing',
};
} else if (elems.length !== 1 || elems[0].kind !== 'tuple') {
return {
'error': `expected \`("CSS selector" or "XPath", JSON)\`, found \`\
${parser.getRawArgs()}\` (${parser.getArticleKind()})`,
const jsonValidator = {
kind: 'json',
keyTypes: {
'string': [],
},
valueTypes: {
'string': {},
'number': {
allowNegative: true,
allowFloat: true,
},
},
};
if (allowNullIdent) {
jsonValidator.valueTypes.ident = {
allowed: ['null'],
};
}
const tuple = elems[0].getRaw();
if (tuple.length !== 2) {
return {
'error': `expected a tuple of two elements, found ${tuple.length}`,
};
} else if (tuple[0].kind !== 'string') {
return {
'error': `expected a string as first argument of the tuple, found \`\
${tuple[0].getErrorText()}\` (${tuple[1].getArticleKind()})`,
};
} else if (tuple[1].kind !== 'json') {
return {
'error': `expected JSON as second argument, found \`${tuple[1].getErrorText()}\` \
(${tuple[1].getArticleKind()})`,
};
const ret = validator(parser,
{
kind: 'tuple',
elements: [
{ kind: 'selector' },
jsonValidator,
],
},
);
if (hasError(ret)) {
return ret;
}

const selector = tuple[0].getSelector('(first argument)');
if (selector.error !== undefined) {
return selector;
}
const tuple = ret.value.entries;
const attributes = tuple[1].value.entries;
const selector = tuple[0].value;

const json = tuple[1].getRaw();
const validators = {'string': [], 'number': []};
if (allowNullIdent) {
validators['ident'] = ['null'];
}
const entries = validateJson(json, validators, argName);
if (entries.error !== undefined) {
return entries;
}
if (Object.entries(entries.values).length === 0) {
if (attributes.size === 0) {
return {
'instructions': [],
'wait': false,
'warnings': entries.warnings,
};
}

const code = [];
for (const [key, value] of Object.entries(entries.values)) {
if (key === '') {
return {
'error': 'empty strings cannot be used as keys',
};
}
for (const [key, value] of attributes) {
code.push(callback(key, value.value, value.kind === 'ident'));
}

return {
'instructions': [
`\
Expand All @@ -71,7 +60,6 @@ await page.evaluate(e => {
${indentString(code.join('\n'), 1)}
}, ${varName});`,
],
'warnings': entries.warnings,
};
}

Expand Down Expand Up @@ -123,36 +111,34 @@ function parseSetCss(parser) {
// * ("CSS selector", "text")
// * ("XPath", "text")
function parseSetText(parser) {
const elems = parser.elems;

if (elems.length === 0) {
return {'error': 'expected `("CSS selector" or "XPath", "text")`, found nothing'};
} else if (elems.length !== 1 || elems[0].kind !== 'tuple') {
return {
'error': 'expected `("CSS selector" or "XPath", "text")`, found' +
` \`${parser.getRawArgs()}\``,
};
}
const tuple = elems[0].getRaw();
if (tuple.length !== 2 || tuple[0].kind !== 'string' || tuple[1].kind !== 'string') {
return {'error': 'expected `("CSS selector" or "XPath", "text")`'};
}
const selector = tuple[0].getSelector();
if (selector.error !== undefined) {
return selector;
const ret = validator(parser,
{
kind: 'tuple',
elements: [
{ kind: 'selector' },
{ kind: 'string' },
],
},
);
if (hasError(ret)) {
return ret;
}
const value = tuple[1].getStringValue();

const tuple = ret.value.entries;
const selector = tuple[0].value;
const text = tuple[1].value.getStringValue();
const varName = 'parseSetTextElem';

return {
'instructions': [
getAndSetElements(selector, varName, false) + '\n' +
'await page.evaluate(e => {\n' +
'if (["input", "textarea"].indexOf(e.tagName.toLowerCase()) !== -1) {\n' +
`e.value = "${value}";\n` +
'} else {\n' +
`e.innerText = "${value}";\n` +
'}\n' +
`}, ${varName});`,
'instructions': [`\
${getAndSetElements(selector, varName, false)}
await page.evaluate(e => {
if (["input", "textarea"].indexOf(e.tagName.toLowerCase()) !== -1) {
e.value = "${text}";
} else {
e.innerText = "${text}";
}
}, ${varName});`,
],
};
}
Expand Down
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.setAttribute(\"\\\"b\",\"c\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.setAttribute(\"b\",\"\\\"c\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.setAttribute(\"b\",\"c\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.setAttribute(\"d\",\"e\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-5.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.removeAttribute(\"b\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/basic-6.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.setAttribute(\"a\",\"b\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-10.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string, number and ident types are allowed as value, found `{\"a\": \"x\"}` (a JSON dict)"""
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-11.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string, number and ident types are allowed as value, found `[\"a\"]` (an array)"""
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-12.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """Forbidden `ident` used (`a`). Allowed idents: [null]"""
error = """unexpected ident `a`. Allowed idents are: [`null`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-13.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-14.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-16.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """Forbidden `ident` used (`id`). Allowed idents: [null]"""
error = """unexpected ident `id`. Allowed idents are: [`null`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-3.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-4.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-5.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected JSON as second argument, found `\"b\"` (a string)"""
error = """expected second element of the tuple to be a JSON dict, found `\"b\"` (a string)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-6.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-8.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected JSON as second argument, found `\"b\"` (a string)"""
error = """expected second element of the tuple to be a JSON dict, found `\"b\"` (a string)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/err-9.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 4"""
error = """expected a tuple of 2 elements, found 4"""
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """empty name of properties (\"\" or '') are not allowed"""
error = """empty keys are not allowed in JSON dict (second element of the tuple)"""
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/multiline-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.setAttribute(\"b\",\"c\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/multiline-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.setAttribute(\"b\",\"c\\n\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/xpath-1.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetAttribute/xpath-2.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetAttribute/xpath-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.setAttribute(\"b\",\"c\");
}, parseSetAttributeElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/basic-1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.style[\"\\\"b\"] = \"c\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/basic-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.style[\"b\"] = \"\\\"c\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/basic-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e.style[\"b\"] = \"c\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/basic-4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.style[\"d\"] = \"e\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/basic-5.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `null` (an ident)"""
error = """type \"ident\" (`null`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/basic-6.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `null` (an ident)"""
error = """type \"ident\" (`null`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-10.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `{\"a\": \"x\"}` (a JSON dict)"""
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-11.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `[\"a\"]` (an array)"""
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-12.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `a` (an ident)"""
error = """type \"ident\" (`a`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-13.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-14.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-16.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """only string and number types are allowed as value, found `id` (an ident)"""
error = """type \"ident\" (`id`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-3.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-4.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-5.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected JSON as second argument, found `\"b\"` (a string)"""
error = """expected second element of the tuple to be a JSON dict, found `\"b\"` (a string)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-6.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 1"""
error = """expected a tuple of 2 elements, found 1"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-8.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected JSON as second argument, found `\"b\"` (a string)"""
error = """expected second element of the tuple to be a JSON dict, found `\"b\"` (a string)"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/err-9.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 4"""
error = """expected a tuple of 2 elements, found 4"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/multiline-1.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """empty name of properties (\"\" or '') are not allowed"""
error = """empty keys are not allowed in JSON dict (second element of the tuple)"""
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/multiline-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.style[\"b\"] = \"c\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/multiline-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.style[\"b\"] = \"c\\n\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/xpath-1.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 1 addition & 1 deletion tests/test-js/api-output/parseSetCss/xpath-2.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """expected a tuple of two elements, found 3"""
error = """expected a tuple of 2 elements, found 3"""
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetCss/xpath-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e.style[\"b\"] = \"c\";
}, parseSetCssElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetProperty/basic-1.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e[\"\\\"b\"] = \"c\";
}, parseSetPropertyElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetProperty/basic-2.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e[\"b\"] = \"\\\"c\";
}, parseSetPropertyElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetProperty/basic-3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parseSetPropertyElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetProperty/basic-4.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ await page.evaluate(e => {
e[\"d\"] = \"e\";
}, parseSetPropertyElem);""",
]
warnings = [
]
2 changes: 0 additions & 2 deletions tests/test-js/api-output/parseSetProperty/basic-5.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ await page.evaluate(e => {
delete e[\"b\"];
}, parseSetPropertyElem);""",
]
warnings = [
]
Loading

0 comments on commit 807d5b9

Please sign in to comment.