@Test
  public void testWrap() {
    Document doc = Jsoup.parse("<div><p>Hello</p><p>There</p></div>");
    Element p = doc.select("p").first();
    p.wrap("<div class='head'></div>");
    assertEquals(
        "<div><div class=\"head\"><p>Hello</p></div><p>There</p></div>",
        TextUtil.stripNewlines(doc.body().html()));

    Element ret = p.wrap("<div><div class=foo></div><p>What?</p></div>");
    assertEquals(
        "<div><div class=\"head\"><div><div class=\"foo\"><p>Hello</p></div><p>What?</p></div></div><p>There</p></div>",
        TextUtil.stripNewlines(doc.body().html()));

    assertEquals(ret, p);
  }
 @Test
 public void testWrapWithRemainder() {
   Document doc = Jsoup.parse("<div><p>Hello</p></div>");
   Element p = doc.select("p").first();
   p.wrap("<div class='head'></div><p>There!</p>");
   assertEquals(
       "<div><div class=\"head\"><p>Hello</p><p>There!</p></div></div>",
       TextUtil.stripNewlines(doc.body().html()));
 }
  public static String getHighlightedText_math(
      String text, String color, String apiElement) // for math after merging with recodoc
      {
    String highlightBeginning = "<SPAN style=\"BACKGROUND-COLOR: " + color + "\">";
    String highlightEnding = "</SPAN>";

    Document doc = Jsoup.parse(text);
    Elements apiElements = doc.select("clt[api=" + apiElement + "]");

    for (Element apielement : apiElements) {
      Document tmp = new Document("");
      String[] apis = apielement.text().split("\\.");
      if (apis.length == 2)
        apielement.html(highlightBeginning + apis[0] + highlightEnding + "." + apis[1]);
      else apielement.wrap(highlightBeginning);
    }

    // highlight code snippet
    Elements codesnippets = doc.getElementsByTag("pre");
    for (Element codesnippet : codesnippets) {
      String html = codesnippet.html();
      Pattern apielementPattern = Pattern.compile("(?<=\\W)" + apiElement + "(?=\\W)");
      Matcher matcher = apielementPattern.matcher(html);

      codesnippet.html(matcher.replaceAll(highlightBeginning + apiElement + highlightEnding));
    }

    // remove clt tags for display
    Elements clts = doc.getElementsByTag("clt");
    for (Element clt : clts) {
      clt.unwrap();
      //			clt.replaceWith(new TextNode(clt.text(), ""));
    }

    return doc.html();
  }
  private void parseRecord(String record) throws ExecutionException, InterruptedException {
    Element root = Jsoup.parse(record).getElementsByTag("div").first();

    Comment comment = new Comment();

    if (root.className().contains("new")) comment.setNew(true);

    String commentId = root.id();
    comment.setLepraId(commentId);

    if (commentId.equals(commentToSelectId)) commentToSelect = commentsCount;

    Matcher level = patternLevel.matcher(root.className());
    if (level.find()) comment.setLevel(Short.valueOf(level.group(1)));

    Element element = root.getElementsByClass("c_body").first();
    if (element.className().contains("hidden")) return;

    boolean containsImages = false;
    Elements images = element.getElementsByTag("img");
    for (Element image : images) {
      String src = image.attr("src");
      if (isImagesEnabled && !TextUtils.isEmpty(src)) {
        if (!image.parent().tag().getName().equalsIgnoreCase("a"))
          image.wrap("<a href=" + "\"" + src + "\"></a>");

        image.removeAttr("width");
        image.removeAttr("height");
        image.removeAttr("style");

        image.attr("style", "max-width:100%");

        containsImages = true;
      } else image.remove();
    }

    String html = Utils.wrapLepraTags(element);
    comment.setHtml(html);
    comment.setOnlyText(!containsImages && !html.contains("leprosorium.ru"));

    Element authorElement = root.getElementsByClass("ddi").first();
    if (authorElement != null) {
      Elements a = authorElement.getElementsByTag("a");
      String url = Commons.PREFIX_URL + a.first().attr("href");
      url = url.replace("\n", "");
      comment.setUrl(url);

      String author = a.size() > 1 ? a.get(1).text() : a.get(0).text();
      if (postAuthor.equals(author)) comment.setPostAuthor(true);

      String color = "black";
      if (comment.isPostAuthor()) color = "red";
      else if (author.equals(userName)) color = "#3270FF";

      comment.setAuthor(author);

      String signature =
          authorElement.text().split(author)[0]
              + "<b><font color=\""
              + color
              + "\">"
              + author
              + "</font></b>";

      String epochDate =
          authorElement.getElementsByClass("js-date").first().attr("data-epoch_date");
      Date date = new Date(Long.valueOf(epochDate) * 1000);

      signature =
          signature
              + " "
              + date
                  .toLocaleString(); // DateUtils.getRelativeTimeSpanString(date.getTime(), new
                                     // Date().getTime(), DateUtils.FORMAT_ABBREV_RELATIVE);

      comment.setSignature(signature);
    }

    if (!post.isInbox()) {
      Element vote = root.getElementsByClass("vote").first();
      if (vote != null) {
        if (!vote.select(".vote_button.vote_button_plus.vote_voted").isEmpty())
          comment.setPlusVoted(true);
        else if (!vote.select(".vote_button.vote_button_minus.vote_voted").isEmpty())
          comment.setMinusVoted(true);

        Element rating = vote.getElementsByClass("vote_result").first();
        comment.setRating(Short.valueOf(rating.text()));
      }
    }

    comment.setNum(commentsCount);

    ServerWorker.Instance().addNewComment(post.getId(), comment);

    commentsCount++;

    if (commentToSelectId != null) {
      if (commentToSelect != -1 && commentsCount >= 50 + commentToSelect) {
        notifyAboutFirstCommentsUpdate();

        commentToSelectId = null;
        commentToSelect = -1;
      }
    } else {
      if (commentsCount == 50) {
        notifyAboutFirstCommentsUpdate();
      } else if (commentsCount != 0 && commentsCount % 100 == 0) {
        notifyAboutCommentsUpdate();
      }
    }
  }