Fix matches of empty elements

Change-Id: I91265b03d5fd30e5c938b60002c70e0cd2d94086
diff --git a/Changes b/Changes
index eb05c00..d3286a4 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.55.7 2016-11-06
+0.55.7 2016-11-11
         - [bugfix] Throw error on optional operands in distance
           queries (diewald)
         - [performance] Remember solved problematic queries in the
@@ -13,6 +13,7 @@
         - [bugfix] Warn on empty elements in positional queries (diewald)
         - [bugfix] Fix wrong deserialization test with negative element in
           positional queries (diewald)
+        - [bugfix] Fix empty element match visualizations (diewald)
 
 0.55.6 2016-08-10
         - [bugfix] distance with key "t" uses default foundry (diewald)
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index 44ac329..dcd59d3 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -142,7 +142,7 @@
 
             this.payloadTypeIdentifier = bb.get(0);
 
-            if (payloadTypeIdentifier == PayloadTypeIdentifier.ELEMENT.value) {
+			if (payloadTypeIdentifier != PayloadTypeIdentifier.MILESTONE.value) {
 				this.matchEndPosition = bb.getInt(9);
 			};
 
diff --git a/src/main/java/de/ids_mannheim/korap/response/Match.java b/src/main/java/de/ids_mannheim/korap/response/Match.java
index 0c89010..4af1618 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Match.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Match.java
@@ -943,7 +943,7 @@
                     pos = clean.length() - 1;
                 };
 
-                snippetArray.addString(clean.substring(oldPos, pos));
+				snippetArray.addString(clean.substring(oldPos, pos));
 
                 oldPos = pos;
             };
@@ -956,7 +956,7 @@
             };
         };
 
-        if (clean.length() > pos) {
+        if (clean.length() > pos && pos >= 0) {
             snippetArray.addString(clean.substring(pos));
         };
     };
@@ -975,52 +975,52 @@
             log.trace("Create HTML Snippet");
 
         StringBuilder sb = new StringBuilder();
+		StringBuilder rightContext = new StringBuilder();
 
         // Snippet stack sizes
         short start = (short) 0;
         short end = this.snippetArray.size();
-        end--;
 
-        // Set levels for highlights 
-        FixedBitSet level = new FixedBitSet(255);
-        level.set(0, 255);
-        byte[] levelCache = new byte[255];
-
-        // First element of sorted array
-        HighlightCombinatorElement elem = this.snippetArray.getFirst();
-
-		// Untested
-		if (elem == null)
-			return null;
-		
-        // Create context
+		// Create context
         sb.append("<span class=\"context-left\">");
         if (this.startMore)
             sb.append("<span class=\"more\"></span>");
 
-        // First element is textual
-        if (elem.type == 0) {
-            sb.append(elem.toHTML(this, level, levelCache));
-            // Move start position
-            start++;
-        };
-        sb.append("</span>");
+		// Set levels for highlights 
+		FixedBitSet level = new FixedBitSet(255);
+		level.set(0, 255);
+		byte[] levelCache = new byte[255];
 
-        // Last element of sorted array
-        elem = this.snippetArray.getLast();
+		HighlightCombinatorElement elem;
 
-        StringBuilder rightContext = new StringBuilder();
+		end--;
+		if (end > 0) {
 
-        // Create right context, if there is any
-        rightContext.append("<span class=\"context-right\">");
+			// First element of sorted array
+			elem = this.snippetArray.getFirst();
 
-        // Last element is textual
-        if (elem != null && elem.type == 0) {
-            rightContext.append(elem.toHTML(this, level, levelCache));
+			// First element is textual
+			if (elem.type == 0) {
+				sb.append(elem.toHTML(this, level, levelCache));
+				// Move start position
+				start++;
+			};
+			sb.append("</span>");
 
-            // decrement end
-            end--;
-        };
+			// Last element of sorted array
+			elem = this.snippetArray.getLast();
+
+			// Create right context, if there is any
+			rightContext.append("<span class=\"context-right\">");
+
+			// Last element is textual
+			if (elem != null && elem.type == 0) {
+				rightContext.append(elem.toHTML(this, level, levelCache));
+
+				// decrement end
+				end--;
+			};
+		};
 
 		if (this.endMore)
             rightContext.append("<span class=\"more\"></span>");
diff --git a/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java b/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
index a749606..9bfbfae 100644
--- a/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
+++ b/src/test/java/de/ids_mannheim/korap/highlight/TestHighlight.java
@@ -445,7 +445,7 @@
 
 		km = kr.getMatch(3);
 		assertEquals(
-			"",
+			"<span class=\"context-left\"><span class=\"match\"></span></span>",
 			km.getSnippetHTML());
 
 	};