package net.bavrd.core;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.safety.Whitelist;
import org.vertx.java.core.json.JsonObject;

public abstract class Face extends BavrdVerticle {

  protected static final Whitelist FORMATTED_TEXT_WHITELIST = new Whitelist()
      .addTags("b", "br", "code", "i", "img")
        .addAttributes("img", "alt", "src")
        .addProtocols("img", "src", "http", "https")
        ;

  @Override
  public BavrdComponent getType() {
    return BavrdComponent.FACE;
  }

  protected String sanitizeRichText(String htmlBody) {
    String cleanHtml = Jsoup.clean(htmlBody, FORMATTED_TEXT_WHITELIST);
    Document bodyFragment = Jsoup.parseBodyFragment(cleanHtml);
    StringBuffer output = new StringBuffer();
    for (Element e : bodyFragment.getAllElements()) {
      if (e.tagName().equals("b")) {
        output.append(formatBold(e.text()));
      } else if (e.tagName().equals("br")) {
        output.append(formatNewLine());
      }  else if (e.tagName().equals("i")) {
        output.append(formatItalic(e.text()));
      }  else if (e.tagName().equals("code")) {
        output.append(formatCode(e.text()));
      }  else if (e.tagName().equals("img")) {
        output.append(formatImg(e.attr("abs:src"), e.attr("alt")));
      } else {
        output.append(e.text());
      }
    }
    return output.toString();
  }

  /**
   * Override this method to allow formatting of bold text with b html tag
   * @param text the text that was detected as bold
   * @return the text formatted accordingly to the distant chat rules for displaying bold text
   */
  protected String formatBold(String text) {
    return text;
  }

  /**
   * Override this method to allow formatting of italic text with i html tag
   * @param text the text that was detected as italic
   * @return the text formatted accordingly to the distant chat rules for displaying italic text
   */
  protected String formatItalic(String text) {
    return text;
  }

  /**
   * Override this method to allow formatting of code text with b html tag
   * @param text the text that was detected as code
   * @return the text formatted accordingly to the distant chat rules for displaying code preformatted text
   */
  protected String formatCode(String text) {
    return text;
  }

  /**
   * Override this method to allow formatting of images with img html tag
   * @param imageSrc the url that was detected as an image source
   * @param imageAlt the optionnal alt text detected in the image tag (or empty string if none)
   * @return a text formatted accordingly to the distant chat rules for displaying images in text
   */
  protected String formatImg(String imageSrc, String imageAlt) {
    return imageSrc;
  }

  /**
   * Override this method to allow introduction of new lines in text
   * @return a string that creates a new line accordingly to the distant chat rules for displaying formatted text
   */
  protected String formatNewLine() {
    return "\n";
  }

}

@@ -128,67 +128,85 @@
 oup.
-safety.Whitelist;%0Aimport org.vertx.java.core
+nodes.Node;%0Aimport org.jsoup.nodes.TextNode;%0Aimport org
 .jso
-n.JsonObjec
+up.safety.Whitelis
 t;%0A%0A
@@ -573,32 +573,29 @@
 CE;%0A  %7D%0A%0A  p
-rotected
+ublic
  String sani
@@ -821,17 +821,14 @@
 or (
-Element e
+Node n
  : b
@@ -843,27 +843,398 @@
 ent.
-getAllElements()) %7B
+body().childNodes()) %7B%0A      output.append(sanitize(n));%0A    %7D%0A    return output.toString();%0A  %7D%0A%0A  private String sanitize(Node n) %7B%0A    String output;%0A    if (n instanceof Element) %7B%0A      StringBuffer inner = new StringBuffer();%0A      for (Node child : n.childNodes()) %7B%0A        inner.append(sanitize(child));%0A      %7D%0A      String text = inner.toString();%0A%0A      Element e = (Element) n;
 %0A   
@@ -1277,32 +1277,27 @@
       output
-.append(
+ = 
 formatBold(e
@@ -1295,25 +1295,20 @@
 matBold(
-e.
 text
-())
 );%0A     
@@ -1357,32 +1357,27 @@
       output
-.append(
+ = 
 formatNewLin
@@ -1379,17 +1379,16 @@
 ewLine()
-)
 ;%0A      
@@ -1436,32 +1436,27 @@
       output
-.append(
+ = 
 formatItalic
@@ -1456,25 +1456,20 @@
 tItalic(
-e.
 text
-())
 );%0A     
@@ -1521,32 +1521,27 @@
       output
-.append(
+ = 
 formatCode(e
@@ -1539,25 +1539,20 @@
 matCode(
-e.
 text
-())
 );%0A     
@@ -1607,24 +1607,19 @@
   output
-.append(
+ = 
 formatIm
@@ -1653,17 +1653,16 @@
 (%22alt%22))
-)
 ;%0A      
@@ -1688,69 +1688,543 @@
 tput
-.append(e
+ = text;%0A      %7D%0A    %7D else if (n instanceof TextNode) %7B%0A      output = ((TextNode) n)
 .text()
-)
 ;%0A    
-  %7D%0A    %7D%0A    return output.toString()
+%7D else%0A      output = %22%22;%0A%0A    //jsoup tends to add some whitespaces before and after %3Cbr%3E, let's get rid of them%0A    if (n.nextSibling() instanceof Element && ((Element) n.nextSibling()).tagName().equals(%22br%22))%0A      output = output.replaceFirst(%22%5C%5Cs+$%22, %22%22);%0A%0A    if (n.previousSibling() instanceof  Element && ((Element) n.previousSibling()).tagName().equals(%22br%22))%0A      output = output.replaceFirst(%22%5E%5C%5Cs+%22, %22%22);%0A%0A    return output
 ;%0A  

fix formatted text parse Nodes recursively, not just tags