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 @@ ...@@ -681,9 +681,16 @@
"Argument 1 is not a search text or a parsable object"); "Argument 1 is not a search text or a parsable object");
}; };
function sanitizeQueryValue(value) { function ensureString(value) {
if (typeof value === "string") { if (value === undefined) { return "undefined"; }
return value.replace(/((?:\\\\)*)\\$/, "$1"); 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; return value;
} }
...@@ -697,7 +704,7 @@ ...@@ -697,7 +704,7 @@
if (query.type === "simple") { if (query.type === "simple") {
return (query.key ? query.key + ": " : "") + return (query.key ? query.key + ": " : "") +
(query.operator || "") + (query.operator || "") +
' "' + sanitizeQueryValue(query.value) + '"'; ' ' + renderSearchTextValue(query.value);
} }
if (query.type === "complex") { if (query.type === "complex") {
query_list = query.query_list; query_list = query.query_list;
...@@ -728,7 +735,7 @@ ...@@ -728,7 +735,7 @@
for (i = 0; i < query_list.length; i += 1) { for (i = 0; i < query_list.length; i += 1) {
string_list.push( string_list.push(
(query_list[i].operator || "") + (query_list[i].operator || "") +
' "' + sanitizeQueryValue(query_list[i].value) + '"' ' ' + renderSearchTextValue(query_list[i].value)
); );
} }
} else { } else {
......
...@@ -321,7 +321,7 @@ ...@@ -321,7 +321,7 @@
"NOT(a:=b OR c:% AND d:<2)" "NOT(a:=b OR c:% AND d:<2)"
) )
).toString(), ).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();" "create(create(\"NOT(a:=b OR c:% AND d:<2)\")).toString();"
); );
...@@ -334,13 +334,13 @@ ...@@ -334,13 +334,13 @@
deepEqual( deepEqual(
jIO.QueryFactory.create("a:(b OR c)").toString(), jIO.QueryFactory.create("a:(b OR c)").toString(),
"a: ( \"b\" OR \"c\" )", "a: ( b OR c )",
"create( \"a:(b OR c)\" ).toString()" "create( \"a:(b OR c)\" ).toString()"
); );
deepEqual( deepEqual(
jIO.QueryFactory.create("(a:b OR a:c)").toString(), jIO.QueryFactory.create("(a:b OR a:c)").toString(),
"a: ( \"b\" OR \"c\" )", "a: ( b OR c )",
"create( \"(a:b OR a:c)\" ).toString()" "create( \"(a:b OR a:c)\" ).toString()"
); );
...@@ -355,7 +355,7 @@ ...@@ -355,7 +355,7 @@
"value": "b" "value": "b"
}] }]
}).toString(), }).toString(),
"( \"a\" \"b\" )", "( a b )",
"{complex query without operator}.toString()" "{complex query without operator}.toString()"
); );
...@@ -364,8 +364,8 @@ ...@@ -364,8 +364,8 @@
"type": "simple", "type": "simple",
"value": "b\\a" "value": "b\\a"
}).toString(), }).toString(),
" \"b\\a\"", " b\\a",
"{simple query with backslash}.toString()" "{simple query with value: \"b\\\\a\"}.toString()"
); );
deepEqual( deepEqual(
...@@ -373,8 +373,35 @@ ...@@ -373,8 +373,35 @@
"type": "simple", "type": "simple",
"value": "b\\" "value": "b\\"
}).toString(), }).toString(),
" \"b\"", " b\\",
"{simple query ending with backslash}.toString()" "{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( 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