From 57b6eed99b6d3f174aa512c2ff66c03f3d94a794 Mon Sep 17 00:00:00 2001
From: Timm Fitschen <t.fitschen@indiscale.com>
Date: Thu, 14 Apr 2022 12:21:02 +0200
Subject: [PATCH] BUG: fix for
 [webui#170](https://gitlab.com/caosdb/caosdb-webui/-/issues/170)

---
 CHANGELOG.md                                |  3 +++
 src/core/js/ext_autocomplete.js             |  2 +-
 test/core/js/modules/ext_autocomplete.js.js | 20 ++++++++++++++++++++
 3 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd9f452f..e79c0662 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
 
 ### Fixed
 
+* [webui#170](https://gitlab.com/caosdb/caosdb-webui/-/issues/170)
+  Autocompletion for "IS REFERENCED BY" leads to query syntax error
+
 ### Security
 
 ### Documentation
diff --git a/src/core/js/ext_autocomplete.js b/src/core/js/ext_autocomplete.js
index 2f6fa0dd..932a9466 100644
--- a/src/core/js/ext_autocomplete.js
+++ b/src/core/js/ext_autocomplete.js
@@ -159,7 +159,7 @@ var ext_autocomplete = new function () {
         var end = origJQElement[0].value.slice(cursorpos);
         var result = resultsFromServer.map(x => {
             var x_quoted = x;
-            if (x.indexOf(" ") > -1) {
+            if (ext_autocomplete.CQL_WORDS.indexOf(x) == -1 && x.indexOf(" ") > -1) {
               if(x.indexOf("\"") > -1) {
                 x_quoted = `'${x}'`;
               } else {
diff --git a/test/core/js/modules/ext_autocomplete.js.js b/test/core/js/modules/ext_autocomplete.js.js
index 96cab766..aaefd228 100644
--- a/test/core/js/modules/ext_autocomplete.js.js
+++ b/test/core/js/modules/ext_autocomplete.js.js
@@ -88,6 +88,26 @@ QUnit.test("searchPost", async function(assert) {
   assert.propEqual(result, expected);
 });
 
+QUnit.test("searchPost webui#170", async function(assert) {
+  // https://gitlab.com/caosdb/caosdb-webui/-/issues/170
+  // Autocompletion for "IS REFERENCED BY" leads to query syntax error
+  const resultsFromServer = ["REFERENCED BY"];
+  const origJQElement = [{
+    selectionEnd: 24,
+    value: "FIND Event WHICH IS REFE",
+  }];
+
+  const expected = [
+      {
+        "html": "REFERENCED BY",
+        "text": "FIND Event WHICH IS REFERENCED BY"
+      },
+  ];
+
+  const result = ext_autocomplete.searchPost(resultsFromServer, origJQElement);
+  assert.propEqual(result, expected);
+});
+
 QUnit.test("class", function(assert) {
     assert.ok(ext_autocomplete.switch_on_completion , "toggle available");
     assert.ok(ext_autocomplete.switch_on_completion() , "toggle runs");
-- 
GitLab