Commit 538be1dc authored by Tristan Cavelier's avatar Tristan Cavelier

query: fix some double quote parsing/serialization issues.

- fixes `"\"a b\""`
  parsed to {simple query with value: '"a b"'}
  serialized to ` "\"a b\""`

/bug #20190603-E76ED6
https://nexedijs.erp5.net/#/bug_module/20190603-E76ED6

/reviewed-on nexedi/jio!107
parent b08e246c
......@@ -114,7 +114,7 @@ case 17:
this.$ = mkSimpleQuery('', $$[$0]);
break;
case 18:
this.$ = mkSimpleQuery('', $$[$0-1]);
this.$ = mkSimpleQuery('', parseQuotedString($$[$0-1]));
break;
}
},
......
......@@ -72,6 +72,6 @@ value
string
: WORD { $$ = mkSimpleQuery('', $1); }
| QUOTE QUOTED_STRING QUOTE { $$ = mkSimpleQuery('', $2); }
| QUOTE QUOTED_STRING QUOTE { $$ = mkSimpleQuery('', parseQuotedString($2)); }
;
......@@ -71,6 +71,8 @@ var arrayExtend = function () {
return true;
}
return false;
}, parseQuotedString = function (string) {
return string.replace(/(?:\\(")|(\\[^"]))/g, '$1$2');
},
error_offsets = [],
error_lookaheads = [],
......
......@@ -672,6 +672,16 @@
"Argument 1 is not a search text or a parsable object");
};
function ensureString(value) {
if (value === undefined) { return "undefined"; }
if (value === null) { return "null"; }
return value.toString();
}
function renderSearchTextValue(value) {
return '"' + ensureString(value).replace(/"/g, '\\"') + '"';
}
function objectToSearchText(query) {
var i = 0,
query_list = null,
......@@ -680,7 +690,8 @@
common_key = "";
if (query.type === "simple") {
return (query.key ? query.key + ": " : "") +
(query.operator || "") + ' "' + query.value + '"';
(query.operator || "") + ' ' +
renderSearchTextValue(query.value);
}
if (query.type === "complex") {
query_list = query.query_list;
......@@ -711,7 +722,7 @@
for (i = 0; i < query_list.length; i += 1) {
string_list.push(
(query_list[i].operator || "") +
' "' + query_list[i].value + '"'
' ' + renderSearchTextValue(query_list[i].value)
);
}
} else {
......
......@@ -359,6 +359,25 @@
"{complex query without operator}.toString()"
);
deepEqual(
jIO.QueryFactory.create({
"type": "simple",
"value": '"a b"'
}).toString(),
' "\\"a b\\""',
"{simple query with value: '\"a b\"'}.toString()"
);
deepEqual(
jIO.Query.parseStringToObject('"\\"a b\\""'),
{
"key": "",
"type": "simple",
"value": '"a b"',
},
"parseStringToObject('\"\\\"a b\\\"\"')"
);
});
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