Commit 1ffefba4 authored by Tristan Cavelier's avatar Tristan Cavelier Committed by Romain Courteaud

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 !107
parent f73bb71f
...@@ -114,7 +114,7 @@ case 17: ...@@ -114,7 +114,7 @@ case 17:
this.$ = mkSimpleQuery('', $$[$0]); this.$ = mkSimpleQuery('', $$[$0]);
break; break;
case 18: case 18:
this.$ = mkSimpleQuery('', $$[$0-1]); this.$ = mkSimpleQuery('', parseQuotedString($$[$0-1]));
break; break;
} }
}, },
......
...@@ -72,6 +72,6 @@ value ...@@ -72,6 +72,6 @@ value
string string
: WORD { $$ = mkSimpleQuery('', $1); } : WORD { $$ = mkSimpleQuery('', $1); }
| QUOTE QUOTED_STRING QUOTE { $$ = mkSimpleQuery('', $2); } | QUOTE QUOTED_STRING QUOTE { $$ = mkSimpleQuery('', parseQuotedString($2)); }
; ;
...@@ -71,6 +71,8 @@ var arrayExtend = function () { ...@@ -71,6 +71,8 @@ var arrayExtend = function () {
return true; return true;
} }
return false; return false;
}, parseQuotedString = function (string) {
return string.replace(/(?:\\(")|(\\[^"]))/g, '$1$2');
}, },
error_offsets = [], error_offsets = [],
error_lookaheads = [], error_lookaheads = [],
......
...@@ -675,6 +675,16 @@ ...@@ -675,6 +675,16 @@
"Argument 1 is not a search text or a parsable object"); "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) { function objectToSearchText(query) {
var i = 0, var i = 0,
query_list = null, query_list = null,
...@@ -683,7 +693,8 @@ ...@@ -683,7 +693,8 @@
common_key = ""; common_key = "";
if (query.type === "simple") { if (query.type === "simple") {
return (query.key ? query.key + ": " : "") + return (query.key ? query.key + ": " : "") +
(query.operator || "") + ' "' + query.value + '"'; (query.operator || "") + ' ' +
renderSearchTextValue(query.value);
} }
if (query.type === "complex") { if (query.type === "complex") {
query_list = query.query_list; query_list = query.query_list;
...@@ -714,7 +725,7 @@ ...@@ -714,7 +725,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 || "") +
' "' + query_list[i].value + '"' ' ' + renderSearchTextValue(query_list[i].value)
); );
} }
} else { } else {
......
...@@ -359,6 +359,25 @@ ...@@ -359,6 +359,25 @@
"{complex query without operator}.toString()" "{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 () { 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