Fix VC fragments for redundant constraints

Change-Id: Iff83540e11fd6e3bcb8dba3b789eea9fc7dcb344
diff --git a/Changes b/Changes
index 0da1d51..bbccd2b 100755
--- a/Changes
+++ b/Changes
@@ -1,7 +1,8 @@
-0.40 2020-10-12
+0.40 2020-10-13
         - Modernize ES and fix in-loops.
         - add roll() method to state object.
         - Fix wrong hint-mirror behaviour in Firefox.
+        - Fix VC fragments for redundant constraints.
 
 0.39 2020-10-07
         - Add information on secret file to Readme.
diff --git a/dev/js/spec/vcSpec.js b/dev/js/spec/vcSpec.js
index 5c1fdfd..ce8f48f 100644
--- a/dev/js/spec/vcSpec.js
+++ b/dev/js/spec/vcSpec.js
@@ -3129,5 +3129,23 @@
 
       expect(f.element().lastChild.children.length).toEqual(0);
     });
+
+    it('should respect already set attributes', function () {
+      var f = fragmentClass.create();
+      expect(f.element().lastChild.children.length).toEqual(0);
+
+      expect(f.isEmpty()).toBeTruthy();
+
+      f.add("author", "Peter");
+      f.add("title", "Example");
+
+      expect(f.isEmpty()).toBeFalsy();
+      
+      expect(f.toQuery()).toEqual('author = "Peter" & title = "Example"');
+
+      f.add("author", "Peter");
+
+      expect(f.toQuery()).toEqual('title = "Example" & author = "Peter"');
+    });
   });
 });
diff --git a/dev/js/src/vc/fragment.js b/dev/js/src/vc/fragment.js
index ccc121e..bf1905e 100644
--- a/dev/js/src/vc/fragment.js
+++ b/dev/js/src/vc/fragment.js
@@ -7,6 +7,7 @@
  */
 
 define(['vc/doc', 'util'], function (docClass) {
+
   "use strict";
 
   const loc = KorAP.Locale;
@@ -14,27 +15,31 @@
   
   // Create a VC doc
   function _doc (op) {
-    var doc = document.createElement('div');
+    const doc = document.createElement('div');
     doc.setAttribute('class','doc');
           
-    var key = doc.addE('span');
+    const key = doc.addE('span');
     key.setAttribute('class','key');
     key.addT(op[0]);
 
-    var match = doc.addE('span');
+    const match = doc.addE('span');
     match.setAttribute('class','match');
     match.addT('eq');
 
-    var value = doc.addE('span');
+    const value = doc.addE('span');
     value.setAttribute('class', 'value');
     value.addT(op[1]);
       
     return doc;
   };
 
+
   // Return object
   return {
 
+    /**
+     * Construct a new VC fragment.
+     */
     create : function () {
       const obj = Object.create(this);
       obj._operands = [];
@@ -46,12 +51,11 @@
      * Add document constraint to fragment
      */
     add : function (key, value, type) {
-      for (let i in this._operands) {
-        let op = this._operands[i];
+      this._operands.forEach(function (op,i,arr) {
         if (op[0] === key && op[1] === value) {
-          array.splice(index, 1);
-        };
-      };
+          arr.splice(i,1);
+        }
+      });
       this._operands.push([key, value, type]);
       this.update();
     },
@@ -61,7 +65,7 @@
      * Remove document constraint from fragment
      */
     remove : function (key, value) {
-      for (let i in this._operands) {
+      for (let i = 0; i < this._operands.length; i++) {
         let op = this._operands[i];
         if (op[0] === key && op[1] === value) {
           this._operands.splice(i, 1);
@@ -107,7 +111,7 @@
     documents : function () {
       return this._operands.map(
         function (item) {
-          let doc = docClass.create();
+          const doc = docClass.create();
           doc.key(item[0]);
           doc.matchop("eq");
           doc.value(item[1]);
@@ -135,20 +139,22 @@
       //   </div>
       // </div>
       let root;
-      let l = this._operands.length;
-      if (l > 1) {
+      const l = this._operands.length;
 
+      if (l > 1) {
         root = document.createElement('div');
         root.setAttribute('class','docGroup');
         root.setAttribute('data-operation', 'and');
-
         this._operands.forEach(i => root.appendChild(_doc(i)));
       }
+
       else if (l == 1) {
         root = _doc(this._operands[0]);
       };
 
+      // Init element
       this.element();
+
       const e = this._frag;
       if (l === 0) {
         _removeChildren(e);
@@ -166,6 +172,7 @@
      * Stringification
      */
     toQuery : function () {
+
       if (this._operands.length === 0)
         return '';