Skip to content

Commit

Permalink
KNL-1580 - Add method for FormattedText to just return HTML document …
Browse files Browse the repository at this point in the history
  • Loading branch information
jonespm authored Jan 8, 2018
1 parent cff2aa2 commit 4149f3c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -451,5 +451,12 @@ public String processFormattedText(final String strFromBrowser, StringBuilder er
* @return the number format
*/
public NumberFormat getNumberFormat();

/**
* Method that will return only the body content, removing all of the tags outside of the body element of an HTML document
*
* @return the all contents within the HTML body
*/
public String getHtmlBody(String text);

}
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,11 @@ public NumberFormat getNumberFormat(Integer maxFractionDigits, Integer minFracti
public NumberFormat getNumberFormat() {
return getNumberFormat(null,null,null);
}

@Override
public String getHtmlBody(String text) {
// TODO Auto-generated method stub
return getHtmlBody(null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,21 @@ public NumberFormat getNumberFormat(Integer maxFractionDigits, Integer minFracti
public NumberFormat getNumberFormat() {
return getNumberFormat(null,null,null);
}

/* (non-Javadoc)
* @see org.sakaiproject.util.api.FormattedText#getHTMLBody(java.lang.String)
*/
public String getHtmlBody(String text) {
if (StringUtils.isBlank(text)) return text;
org.jsoup.nodes.Document document = org.jsoup.Jsoup.parse(text);
document.outputSettings().prettyPrint(false);
return document.body().html();
}

public String getDecimalSeparator() {
return ((DecimalFormat)getNumberFormat()).getDecimalFormatSymbols().getDecimalSeparator()+"";
}


/**
* SAK-23567 Gets the shortened version of the title
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,21 @@ public void testKNL_1530() {
Assert.assertTrue( errorMessages.length() == 0 );
Assert.assertEquals(result, expectedAnchor);
}

@Test
public void getHtmlBodyTest() {
String result;
StringBuilder errorMessages = new StringBuilder();

result = formattedText.getHtmlBody("<html><body><div>Text</div></body></html>");
Assert.assertEquals("<div>Text</div>", result);

result = formattedText.getHtmlBody("<div>Text</div>");
Assert.assertEquals("<div>Text</div>", result);

result = formattedText.getHtmlBody("");
Assert.assertEquals("", result);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,11 @@ public static NumberFormat getNumberFormat() {
return getFormattedText().getNumberFormat();
}

/**
* @see org.sakaiproject.util.api.FormattedText#getHtmlBody(String)
*/
public static String getHtmlBody(String text) {
return getFormattedText().getHtmlBody(text);
}

}

0 comments on commit 4149f3c

Please sign in to comment.