Added more tests
This commit is contained in:
parent
1aa2bc2681
commit
05c1bd3f2a
7 changed files with 121 additions and 52 deletions
|
@ -40,6 +40,6 @@ public class EncodeHtmlEntities implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.toHtmlEntities(template.getValueOrAttribute(differentiator));
|
||||
return RenderUtils.htmlEntities(template.getValueOrAttribute(differentiator));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,6 @@ public class EncodeJs implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.encodeJS(template.getValueOrAttribute(differentiator));
|
||||
return RenderUtils.encodeJs(template.getValueOrAttribute(differentiator));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ public final class RenderUtils {
|
|||
* @return the abbreviated String
|
||||
*/
|
||||
public static String abbreviate(String src, int max, String marker) {
|
||||
if (src == null || src.isBlank()) {
|
||||
if (src == null || src.isBlank() || marker == null) {
|
||||
return src;
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ public final class RenderUtils {
|
|||
* @param src the source String
|
||||
* @return the encoded String
|
||||
*/
|
||||
public static String encodeJS(String src) {
|
||||
public static String encodeJs(String src) {
|
||||
if (src == null || src.isBlank()) {
|
||||
return src;
|
||||
}
|
||||
|
@ -172,6 +172,34 @@ public final class RenderUtils {
|
|||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a text string to HTML decimal entities.
|
||||
*
|
||||
* @param src the String to convert
|
||||
* @return the converted String
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
||||
public static String htmlEntities(String src) {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return src;
|
||||
}
|
||||
|
||||
var len = src.length();
|
||||
var sb = new StringBuilder(len * 6);
|
||||
|
||||
// https://stackoverflow.com/a/6766497/8356718
|
||||
int codePoint;
|
||||
for (var i = 0; i < len; i++) {
|
||||
codePoint = src.codePointAt(i);
|
||||
// Skip over the second char in a surrogate pair
|
||||
if (codePoint > 0xffff) {
|
||||
i++;
|
||||
}
|
||||
sb.append(String.format("&#%s;", codePoint));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Masks characters in a String.
|
||||
*
|
||||
|
@ -182,6 +210,10 @@ public final class RenderUtils {
|
|||
* @return the masked String
|
||||
*/
|
||||
public static String mask(String src, String mask, int unmasked, boolean fromStart) {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return src;
|
||||
}
|
||||
|
||||
var len = src.length();
|
||||
var buff = new StringBuilder(len);
|
||||
if (unmasked > 0 && unmasked < len) {
|
||||
|
@ -205,6 +237,10 @@ public final class RenderUtils {
|
|||
* @return The normalized String
|
||||
*/
|
||||
public static String normalize(String src) {
|
||||
if (src == null || src.isBlank()) {
|
||||
return src;
|
||||
}
|
||||
|
||||
var normalized = Normalizer.normalize(src.trim(), Normalizer.Form.NFD);
|
||||
var sb = new StringBuilder(normalized.length());
|
||||
boolean space = false;
|
||||
|
@ -344,33 +380,6 @@ public final class RenderUtils {
|
|||
return new String(buff, 0, offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a text string to HTML decimal entities.
|
||||
*
|
||||
* @param src the String to convert
|
||||
* @return the converted String
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
||||
public static String toHtmlEntities(String src) {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return src;
|
||||
}
|
||||
|
||||
var len = src.length();
|
||||
var sb = new StringBuilder(len * 6);
|
||||
|
||||
// https://stackoverflow.com/a/6766497/8356718
|
||||
for (var i = 0; i < len; i++) {
|
||||
var codePoint = src.codePointAt(i);
|
||||
// Skip over the second char in a surrogate pair
|
||||
if (codePoint > 0xffff) {
|
||||
i++;
|
||||
}
|
||||
sb.append(String.format("&#%s;", codePoint));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the formatted server uptime.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue