Fix CSS styles for new/deleted files in preview diff screens

Daisydiff may insert span tags into the inlined CSS to highlight it
with green/red background. As result the CSS is unparseable and is
ignored. Since it's not possible to avoid this, fix it after the fact.

Change-Id: Ib4d7dc105888954ccf149ced0abb0078f032607c
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
index d34abc3..a0fc8c6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/XDocLoader.java
@@ -273,7 +273,27 @@
     postProcess.endElement("", "diffreport", "diffreport");
     postProcess.endDocument();
 
-    return htmlDiff.toString(UTF_8.name());
+    return fixStyles(htmlDiff.toString(UTF_8.name()));
+  }
+
+  /**
+   * The daisydiff formatting may make inlined styles unparsable. Fix it:
+   * <ul>
+   *   <li>Remove span element to highlight addition/deletion inside style elements.</li>
+   *   <li>Replace '&gt;' with '>'.</li>
+   * </ul>
+   */
+  private String fixStyles(String html) {
+    Matcher m =
+        Pattern.compile("(<style[a-zA-Z -=/\"]+>\n)<[a-zA-Z -=\"]+>(.*)</[a-z]+>(\n</style>)")
+               .matcher(html);
+    StringBuffer sb = new StringBuffer();
+    while (m.find()) {
+      m.appendReplacement(sb, m.group(1) + m.group(2).replaceAll("&gt;", ">")
+          + m.group(3));
+    }
+    m.appendTail(sb);
+    return sb.toString();
   }
 
   private TextNodeComparator getComparator(String html) throws IOException,