mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-21 06:54:09 +00:00
Fixed #3. Added support for htmlEncode function
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -897,6 +897,17 @@ public abstract class Strman {
|
||||
return Arrays.stream(entities).map(e -> HtmlEntities.decodedEntities.get(e)).collect(joining());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert all applicable characters to HTML entities.
|
||||
*
|
||||
* @param html The HTML to encode
|
||||
* @return The encoded data
|
||||
*/
|
||||
public static String htmlEncode(final String html) {
|
||||
validate(html, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return html.chars().mapToObj(c -> "\\u" + String.format("%04x", c).toUpperCase()).map(e -> HtmlEntities.encodedEntities.get(e)).collect(joining());
|
||||
}
|
||||
|
||||
public static String decode(final String value, final int digits, final int radix) {
|
||||
validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER);
|
||||
return Arrays
|
||||
|
||||
@@ -654,4 +654,13 @@ public class StrmanTest {
|
||||
assertThat(htmlDecode("Ж"), equalTo("Ж"));
|
||||
assertThat(htmlDecode("┐"), equalTo("┐"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void htmlEncode_shouldBeEncodedToHtmlEntities() throws Exception {
|
||||
assertThat(htmlEncode("á"), equalTo("á"));
|
||||
assertThat(htmlEncode("áéíóú"), equalTo("áéíóú"));
|
||||
assertThat(htmlEncode("Ш"), equalTo("Ш"));
|
||||
assertThat(htmlEncode("Ж"), equalTo("Ж"));
|
||||
assertThat(htmlEncode("┐"), equalTo("┐"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user