Commit 82662091 authored by Tristan Cavelier's avatar Tristan Cavelier

fixup : query: fix ending backslash in query serialization

The commit should now be rephrased to : query: fix some query value serialization issues

* Double quotes are not escaped :

When bulding a query from a JSON object, if a simple query
value contained double quotes, then they were not escaped.

Stringifying `{type: "simple", value: "\"hello\""}` results :
- before ` ""hello""`, which is a weird query ;
- now ` "\"hello\""`, which is ok.

* Ending backslash causes invalid query :

We now lose the ending backslash during the serialization
in order to build a valid query string.

Stringifying `{type: "simple", value: "hello\\"}` results :
- before ` "hello\"`, which is an invalid query string ;
- now ` hello\`, which is ok.
parent 21e0295d
......@@ -681,9 +681,16 @@
"Argument 1 is not a search text or a parsable object");
};
function sanitizeQueryValue(value) {
if (typeof value === "string") {
return value.replace(/((?:\\\\)*)\\$/, "$1");
function ensureString(value) {
if (value === undefined) { return "undefined"; }
if (value === null) { return "null"; }
return value.toString();
}
function renderSearchTextValue(value) {
value = ensureString(value);
if (/(?:^[=!><]|[\s":])/.test(value)) {
return '"' + value.replace(/((?:\\\\)*)\\$/, "$1").replace(/"/g, '\\"') + '"';
}
return value;
}
......@@ -697,7 +704,7 @@
if (query.type === "simple") {
return (query.key ? query.key + ": " : "") +
(query.operator || "") +
' "' + sanitizeQueryValue(query.value) + '"';
' ' + renderSearchTextValue(query.value);
}
if (query.type === "complex") {
query_list = query.query_list;
......@@ -728,7 +735,7 @@
for (i = 0; i < query_list.length; i += 1) {
string_list.push(
(query_list[i].operator || "") +
' "' + sanitizeQueryValue(query_list[i].value) + '"'
' ' + renderSearchTextValue(query_list[i].value)
);
}
} else {
......
......@@ -321,7 +321,7 @@
"NOT(a:=b OR c:% AND d:<2)"
)
).toString(),
"NOT ( ( a: = \"b\" OR ( c: \"%\" AND d: < \"2\" ) ) )",
"NOT ( ( a: = b OR ( c: % AND d: < 2 ) ) )",
"create(create(\"NOT(a:=b OR c:% AND d:<2)\")).toString();"
);
......@@ -334,13 +334,13 @@
deepEqual(
jIO.QueryFactory.create("a:(b OR c)").toString(),
"a: ( \"b\" OR \"c\" )",
"a: ( b OR c )",
"create( \"a:(b OR c)\" ).toString()"
);
deepEqual(
jIO.QueryFactory.create("(a:b OR a:c)").toString(),
"a: ( \"b\" OR \"c\" )",
"a: ( b OR c )",
"create( \"(a:b OR a:c)\" ).toString()"
);
......@@ -355,7 +355,7 @@
"value": "b"
}]
}).toString(),
"( \"a\" \"b\" )",
"( a b )",
"{complex query without operator}.toString()"
);
......@@ -364,8 +364,8 @@
"type": "simple",
"value": "b\\a"
}).toString(),
" \"b\\a\"",
"{simple query with backslash}.toString()"
" b\\a",
"{simple query with value: \"b\\\\a\"}.toString()"
);
deepEqual(
......@@ -373,8 +373,35 @@
"type": "simple",
"value": "b\\"
}).toString(),
" \"b\"",
"{simple query ending with backslash}.toString()"
" b\\",
"{simple query with value: \"b\\\\\"}.toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "simple",
"value": '"a b"'
}).toString(),
" \"\\\"a b\\\"\"",
"{simple query with value: '\"a b\"'}.toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "simple",
"value": "a b\\"
}).toString(),
" \"a b\"", // ending backslash is lost to avoid to create an invalid query
"{simple query with value: \"a b\\\\\"}.toString() -> XXX Is this really expected behavior ?"
);
deepEqual(
jIO.Query.parseStringToObject('"\"a b\""'),
{
"type": "simple",
"value": "\"a b\"",
},
"parseStringToObject('\"\\\"a b\\\"\"')"
);
deepEqual(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment