Commit d08b150f authored by Tristan Cavelier's avatar Tristan Cavelier

query: fix ending backslash in query serialization

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 b08e246c
......@@ -672,6 +672,13 @@
"Argument 1 is not a search text or a parsable object");
};
function sanitizeQueryValue(value) {
if (typeof value === "string") {
return value.replace(/((?:\\\\)*)\\$/, "$1");
}
return value;
}
function objectToSearchText(query) {
var i = 0,
query_list = null,
......@@ -680,7 +687,8 @@
common_key = "";
if (query.type === "simple") {
return (query.key ? query.key + ": " : "") +
(query.operator || "") + ' "' + query.value + '"';
(query.operator || "") +
' "' + sanitizeQueryValue(query.value) + '"';
}
if (query.type === "complex") {
query_list = query.query_list;
......@@ -711,7 +719,7 @@
for (i = 0; i < query_list.length; i += 1) {
string_list.push(
(query_list[i].operator || "") +
' "' + query_list[i].value + '"'
' "' + sanitizeQueryValue(query_list[i].value) + '"'
);
}
} else {
......
......@@ -359,6 +359,24 @@
"{complex query without operator}.toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "simple",
"value": "b\\a"
}).toString(),
" \"b\\a\"",
"{simple query with backslash}.toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "simple",
"value": "b\\"
}).toString(),
" \"b\"",
"{simple query ending with backslash}.toString()"
);
});
test('Docs with space, tab, and newline', function () {
......
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