Rename all cookies to be instance-independent (Requires relogin) - fixes #94

Change-Id: Icd8b58e4b6fbd99a93ee6972485ef77786b5764c
diff --git a/dev/js/runner/all.html b/dev/js/runner/all.html
index 366c755..f0a71ad 100644
--- a/dev/js/runner/all.html
+++ b/dev/js/runner/all.html
@@ -47,7 +47,8 @@
         'spec/corpusByMatchSpec',
         'spec/statSpec',
         'spec/vcSpec',
-        'spec/tourSpec'
+        'spec/tourSpec',
+        'spec/utilSpec'
       ],
       function () {
         window.onload();
diff --git a/dev/js/spec/utilSpec.js b/dev/js/spec/utilSpec.js
new file mode 100644
index 0000000..b42102a
--- /dev/null
+++ b/dev/js/spec/utilSpec.js
@@ -0,0 +1,19 @@
+define(['util'], function () {
+  describe('KorAP.util', function () {
+
+    it('should quote', function () {
+      expect('Baum'.quote()).toEqual('"Baum"');
+      expect('B"a"um'.quote()).toEqual('"B\\"a\\"um"');
+    });
+
+    it('should escape regex', function () {
+      expect('aaa/bbb\/ccc'.escapeRegex()).toEqual('aaa\\/bbb\\/ccc');
+    });
+
+    it('should slugify', function () {
+      expect('/korap/test'.slugify()).toEqual('koraptest');
+      expect('korap test'.slugify()).toEqual('korap-test');
+      expect('Korap Test'.slugify()).toEqual('korap-test');
+    });
+  })
+});
diff --git a/dev/js/src/init.js b/dev/js/src/init.js
index 6de66d8..bafa473 100644
--- a/dev/js/src/init.js
+++ b/dev/js/src/init.js
@@ -44,7 +44,10 @@
 
   const d = document;
 
-  KorAP.session = sessionClass.create('KalamarJS');
+  // Create suffix if KorAP is run in a subfolder
+  KorAP.session = sessionClass.create(
+    KorAP.URL.length > 0 ? 'kalamarJS-' + KorAP.URL.slugify() : 'kalamarJS'
+  );
 
   // Override KorAP.log
   window.alertify = alertifyClass;
diff --git a/dev/js/src/util.js b/dev/js/src/util.js
index cb60c2a..5df346b 100644
--- a/dev/js/src/util.js
+++ b/dev/js/src/util.js
@@ -9,16 +9,22 @@
   };
 };
 
-var _quoteRE = new RegExp("([\"\\\\])", 'g');
+const _quoteRE = new RegExp("([\"\\\\])", 'g');
 String.prototype.quote = function () {
-  return this.replace(_quoteRE, '\\$1');
+  return '"' + this.replace(_quoteRE, '\\$1') + '"';
 };
 
-var _escapeRE = new RegExp("([\/\\\\])", 'g');
+const _escapeRE = new RegExp("([\/\\\\])", 'g');
 String.prototype.escapeRegex = function () {
   return this.replace(_escapeRE, '\\$1');
 };
 
+const _slug1RE = new RegExp("[^-a-zA-Z0-9_\\s]+", 'g');
+const _slug2RE = new RegExp("[-\\s]+", 'g');
+String.prototype.slugify = function () {
+  return this.toLowerCase().replace(_slug1RE, '').replace(_slug2RE, '-');
+};
+
 // Add toggleClass method similar to jquery
 HTMLElement.prototype.toggleClass = function (c1, c2) {
   var cl = this.classList;
diff --git a/dev/js/src/vc/doc.js b/dev/js/src/vc/doc.js
index b012e1f..8ea8364 100644
--- a/dev/js/src/vc/doc.js
+++ b/dev/js/src/vc/doc.js
@@ -667,7 +667,7 @@
         return string + '/' + this.value().escapeRegex() + '/';
       case "string":
       case "text":
-        return string + '"' + this.value().quote() + '"';
+        return string + this.value().quote();
       };
 
       return "";
diff --git a/dev/js/src/vc/docgroupref.js b/dev/js/src/vc/docgroupref.js
index 9425fb3..e72385a 100644
--- a/dev/js/src/vc/docgroupref.js
+++ b/dev/js/src/vc/docgroupref.js
@@ -273,7 +273,7 @@
         return "";
 
       // Build doc string based on key
-      return 'referTo "' + this.ref().quote() + '"';
+      return 'referTo ' + this.ref().quote();
     }
   };
 });
diff --git a/dev/js/src/vc/fragment.js b/dev/js/src/vc/fragment.js
index 2d2504d..f0c1397 100644
--- a/dev/js/src/vc/fragment.js
+++ b/dev/js/src/vc/fragment.js
@@ -176,7 +176,7 @@
           if (item[2] === "date") {
             return item[0] + ' in ' + item[1];
           };
-          return item[0] + ' = "' + new String(item[1]).quote() + '"';
+          return item[0] + ' = ' + new String(item[1]).quote();
         }
       ).join(" & ");
     }
diff --git a/dev/js/src/vc/rewrite.js b/dev/js/src/vc/rewrite.js
index eeec713..150ee87 100644
--- a/dev/js/src/vc/rewrite.js
+++ b/dev/js/src/vc/rewrite.js
@@ -87,14 +87,9 @@
       str += ' of ' + (
 	      this._scope === null ?
 	        'object' :
-	        '"' +
-	        this.scope().quote() +
-	        '"'
+	        this.scope().quote()
       );
-      str += ' by ' +
-	      '"' +
-	      this.src().quote() +
-	      '"';
+      str += ' by ' + this.src().quote();
       return str;
     }
   };