Improved the renderers encoding by using the template encoding or encoding specified in a property
This commit is contained in:
parent
538a8d35ae
commit
9738ecb0ba
34 changed files with 197 additions and 61 deletions
|
@ -13,7 +13,7 @@ plugins {
|
|||
id("com.github.ben-manes.versions") version "0.46.0"
|
||||
}
|
||||
|
||||
val rifeVersion by rootProject.extra { "1.5.5" }
|
||||
val rifeVersion by rootProject.extra { "1.5.6" }
|
||||
|
||||
group = "com.uwyn.rife2"
|
||||
version = "1.0.1-SNAPSHOT"
|
||||
|
|
|
@ -57,6 +57,8 @@ public class Abbreviate implements ValueRenderer {
|
|||
// do nothing
|
||||
}
|
||||
}
|
||||
return RenderUtils.abbreviate(template.getValueOrAttribute(differentiator), max, mark);
|
||||
|
||||
return template.getEncoder().encode(
|
||||
RenderUtils.abbreviate(template.getValueOrAttribute(differentiator), max, mark));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -42,6 +42,6 @@ public class BeatTime implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.beatTime(ZonedDateTime.now());
|
||||
return template.getEncoder().encode(RenderUtils.beatTime(ZonedDateTime.now()));
|
||||
}
|
||||
}
|
|
@ -41,6 +41,6 @@ public class Capitalize implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.capitalize(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(StringUtils.capitalize(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,6 @@ public class DateIso implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_DATE_FORMATTER);
|
||||
return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_DATE_FORMATTER));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -61,6 +61,6 @@ public class DateTimeIso implements ValueRenderer {
|
|||
}
|
||||
|
||||
}
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_FORMATTER);
|
||||
return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_FORMATTER));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,6 @@ public class DateTimeRfc2822 implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(RenderUtils.RFC_2822_FORMATTER);
|
||||
return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.RFC_2822_FORMATTER));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,7 +44,9 @@ public class EncodeBase64 implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.encodeBase64(template.getValueOrAttribute(differentiator)
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
|
||||
return RenderUtils.encode(
|
||||
StringUtils.encodeBase64(template.getValueOrAttribute(differentiator).getBytes(StandardCharsets.UTF_8)),
|
||||
properties);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ public class EncodeJs implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.encodeJs(template.getValueOrAttribute(differentiator));
|
||||
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
|
||||
return RenderUtils.encode(RenderUtils.encodeJs(template.getValueOrAttribute(differentiator)), properties);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ public class EncodeJson implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.encodeJson(template.getValueOrAttribute(differentiator));
|
||||
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
|
||||
return RenderUtils.encode(StringUtils.encodeJson(template.getValueOrAttribute(differentiator)), properties);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ public class EncodeUnicode implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.encodeUnicode(template.getValueOrAttribute(differentiator));
|
||||
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
|
||||
return RenderUtils.encode(StringUtils.encodeUnicode(template.getValueOrAttribute(differentiator)), properties);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,7 @@ public class EncodeUrl implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.encodeUrl(template.getValueOrAttribute(differentiator));
|
||||
var properties = RenderUtils.parsePropertiesString(template.getDefaultValue(valueId));
|
||||
return RenderUtils.encode(StringUtils.encodeUrl(template.getValueOrAttribute(differentiator)), properties);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,6 @@ public class FormatCreditCard implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.formatCreditCard(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(RenderUtils.formatCreditCard(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,6 @@ public class Lowercase implements ValueRenderer {
|
|||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.toLowerCase(Localization.getLocale());
|
||||
return template.getEncoder().encode(value.toLowerCase(Localization.getLocale()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -59,6 +59,7 @@ public class Mask implements ValueRenderer {
|
|||
// do nothing
|
||||
}
|
||||
}
|
||||
return RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart);
|
||||
return template.getEncoder().encode(
|
||||
RenderUtils.mask(template.getValueOrAttribute(differentiator), mask, unmasked, fromStart));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,6 +40,6 @@ public class Normalize implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.normalize(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(RenderUtils.normalize(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import rife.tools.Localization;
|
|||
import rife.tools.StringUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -39,6 +40,11 @@ import java.util.concurrent.TimeUnit;
|
|||
* @since 1.0
|
||||
*/
|
||||
public final class RenderUtils {
|
||||
/**
|
||||
* The encoding property.
|
||||
*/
|
||||
public static final String ENCODING_PROPERTY = "encoding";
|
||||
|
||||
/**
|
||||
* ISO 8601 date formatter.
|
||||
*
|
||||
|
@ -82,12 +88,12 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Abbreviates a String to the given length using a replacement marker.
|
||||
* Abbreviates a {@code String} to the given length using a replacement marker.
|
||||
*
|
||||
* @param src the source String
|
||||
* @param max the maximum length of the resulting String
|
||||
* @param marker the String used as a replacement marker
|
||||
* @return the abbreviated String
|
||||
* @param src the source {@code String}
|
||||
* @param max the maximum length of the resulting {@code String}
|
||||
* @param marker the {@code String} used as a replacement marker
|
||||
* @return the abbreviated {@code String}
|
||||
*/
|
||||
public static String abbreviate(String src, int max, String marker) {
|
||||
if (src == null || src.isBlank() || marker == null) {
|
||||
|
@ -117,10 +123,59 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Encodes a String to JavaScript/ECMAScript.
|
||||
* <p>Encodes the source {@code String} to the specified encoding.</p>
|
||||
*
|
||||
* @param src the source String
|
||||
* @return the encoded String
|
||||
* <p>The supported encodings are:</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@code html}</li>
|
||||
* <li>{@code js}</li>
|
||||
* <li>{@code json}</li>
|
||||
* <li>{@code unicode}</li>
|
||||
* <li>{@code url}</li>
|
||||
* <li>{@code xml}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param src the source {@code String} to encode
|
||||
* @param properties the properties containing the {@link #ENCODING_PROPERTY encoding property}.
|
||||
* @return the encoded {@code String}
|
||||
*/
|
||||
public static String encode(String src, Properties properties) {
|
||||
if (src == null || src.isBlank() || properties.isEmpty()) {
|
||||
return src;
|
||||
}
|
||||
|
||||
var encoding = properties.getProperty(ENCODING_PROPERTY, "");
|
||||
switch (encoding) {
|
||||
case "html" -> {
|
||||
return StringUtils.encodeHtml(src);
|
||||
}
|
||||
case "js" -> {
|
||||
return RenderUtils.encodeJs(src);
|
||||
}
|
||||
case "json" -> {
|
||||
return StringUtils.encodeJson(src);
|
||||
}
|
||||
case "unicode" -> {
|
||||
return StringUtils.encodeUnicode(src);
|
||||
}
|
||||
case "url" -> {
|
||||
return StringUtils.encodeUrl(src);
|
||||
}
|
||||
case "xml" -> {
|
||||
return StringUtils.encodeXml(src);
|
||||
}
|
||||
default -> {
|
||||
return src;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes a {@code String} to JavaScript/ECMAScript.
|
||||
*
|
||||
* @param src the source {@code String}
|
||||
* @return the encoded {@code String}
|
||||
*/
|
||||
public static String encodeJs(String src) {
|
||||
if (src == null || src.isBlank()) {
|
||||
|
@ -147,7 +202,7 @@ public final class RenderUtils {
|
|||
/**
|
||||
* Fetches the content (body) of a URL.
|
||||
*
|
||||
* @param url the URL sSng.
|
||||
* @param url the URL {@code String}
|
||||
* @param defaultContent the default content to return if none fetched
|
||||
* @return the url content, or empty
|
||||
*/
|
||||
|
@ -168,8 +223,12 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the last 4 digits a credit card number. The number must satisfy the Luhn algorithm.
|
||||
* Non-digits are stripped from the number.
|
||||
* <p></p>Returns the last 4 digits a credit card number.</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li>The number must satisfy the Luhn algorithm</li>
|
||||
* <li>Non-digits are stripped from the number</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param src the credit card number
|
||||
* @return the last 4 digits of the credit card number or empty
|
||||
|
@ -189,10 +248,10 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Converts a text String to HTML decimal entities.
|
||||
* Converts a text {@code String} to HTML decimal entities.
|
||||
*
|
||||
* @param src the String to convert
|
||||
* @return the converted String
|
||||
* @param src the {@code String} to convert
|
||||
* @return the converted {@code String}
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
||||
public static String htmlEntities(String src) {
|
||||
|
@ -219,11 +278,11 @@ public final class RenderUtils {
|
|||
/**
|
||||
* Masks characters in a String.
|
||||
*
|
||||
* @param src the source String
|
||||
* @param mask the String to mask characters with
|
||||
* @param src the source {@code String}
|
||||
* @param mask the {@code String} to mask characters with
|
||||
* @param unmasked the number of characters to leave unmasked
|
||||
* @param fromStart to unmask characters from the start of the String
|
||||
* @return the masked String
|
||||
* @param fromStart to unmask characters from the start of the {@code String}
|
||||
* @return the masked {@code String}
|
||||
*/
|
||||
public static String mask(String src, String mask, int unmasked, boolean fromStart) {
|
||||
if (src == null || src.isEmpty()) {
|
||||
|
@ -247,10 +306,10 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Normalizes a String for inclusion in a URL path.
|
||||
* Normalizes a {@code String} for inclusion in a URL path.
|
||||
*
|
||||
* @param src the source String
|
||||
* @return the normalized String
|
||||
* @param src the source {@code String}
|
||||
* @return the normalized {@code String}
|
||||
*/
|
||||
public static String normalize(String src) {
|
||||
if (src == null || src.isBlank()) {
|
||||
|
@ -278,13 +337,31 @@ public final class RenderUtils {
|
|||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new {@code Properties} containing the properties specified in the given {$String}.
|
||||
*
|
||||
* @param src the {@code} String containing the properties
|
||||
* @return the new {@code Properties}
|
||||
*/
|
||||
public static Properties parsePropertiesString(String src) {
|
||||
var properties = new Properties();
|
||||
if (src != null && !src.isBlank()) {
|
||||
try {
|
||||
properties.load(new StringReader(src));
|
||||
} catch (IOException ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the plural form of a word, if count > 1.
|
||||
*
|
||||
* @param count the count
|
||||
* @param word the singular word
|
||||
* @param plural the plural word
|
||||
* @return the singular or plural String
|
||||
* @return the singular or plural {@code String}
|
||||
*/
|
||||
public static String plural(final long count, final String word, final String plural) {
|
||||
if (count > 1) {
|
||||
|
@ -295,9 +372,9 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Generates an SVG QR Code from the given String using <a href="https://goqr.me/">goQR.me</a>.
|
||||
* Generates an SVG QR Code from the given {@code String} using <a href="https://goqr.me/">goQR.me</a>.
|
||||
*
|
||||
* @param src the data String
|
||||
* @param src the data {@code String}
|
||||
* @param size the QR Code size. (e.g. {@code 150x150})
|
||||
* @return the QR code
|
||||
*/
|
||||
|
@ -312,10 +389,10 @@ public final class RenderUtils {
|
|||
}
|
||||
|
||||
/**
|
||||
* Translates a String to/from ROT13.
|
||||
* Translates a {@code String} to/from ROT13.
|
||||
*
|
||||
* @param src the source String
|
||||
* @return the translated String
|
||||
* @param src the source {@code String}
|
||||
* @return the translated {@code String}
|
||||
*/
|
||||
public static String rot13(String src) {
|
||||
if (src == null || src.isBlank()) {
|
||||
|
@ -353,7 +430,7 @@ public final class RenderUtils {
|
|||
/**
|
||||
* <p>Shortens a URL using <a href="https://is.gd/">is.gid</a>.</p>
|
||||
*
|
||||
* <p>The URL String must be a valid http or https URL.</p>
|
||||
* <p>The URL {@code String} must be a valid http or https URL.</p>
|
||||
*
|
||||
* <p>Based on <a href="https://github.com/ethauvin/isgd-shorten">isgd-shorten</a></p>
|
||||
*
|
||||
|
@ -371,8 +448,8 @@ public final class RenderUtils {
|
|||
/**
|
||||
* Swaps the case of a String.
|
||||
*
|
||||
* @param src the String to swap the case of
|
||||
* @return the modified String or null
|
||||
* @param src the {@code String} to swap the case of
|
||||
* @return the modified {@code String} or null
|
||||
*/
|
||||
@SuppressWarnings("PMD.AvoidReassigningLoopVariables")
|
||||
public static String swapCase(String src) {
|
||||
|
@ -507,4 +584,5 @@ public final class RenderUtils {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -40,6 +40,6 @@ public class Rot13 implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.rot13(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(RenderUtils.rot13(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,6 @@ public class ShortenUrl implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.shortenUrl(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(RenderUtils.shortenUrl(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,6 @@ public class SwapCase implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return RenderUtils.swapCase(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(RenderUtils.swapCase(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,6 +42,6 @@ public class TimeIso implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_TIME_FORMATTER);
|
||||
return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_TIME_FORMATTER));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,6 @@ public class Trim implements ValueRenderer {
|
|||
if (value == null || value.isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
return value.trim();
|
||||
return template.getEncoder().encode(value.trim());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,6 @@ public class Uncapitalize implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return StringUtils.uncapitalize(template.getValueOrAttribute(differentiator));
|
||||
return template.getEncoder().encode(StringUtils.uncapitalize(template.getValueOrAttribute(differentiator)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,6 @@ public class Uppercase implements ValueRenderer {
|
|||
if (value == null || value.isBlank()) {
|
||||
return value;
|
||||
}
|
||||
return value.toUpperCase(Localization.getLocale());
|
||||
return template.getEncoder().encode(value.toUpperCase(Localization.getLocale()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,10 +55,13 @@ public class Uptime implements ValueRenderer {
|
|||
}
|
||||
}
|
||||
|
||||
String uptime;
|
||||
if (template.hasAttribute(Uptime.class.getName())) {
|
||||
return RenderUtils.uptime((long) template.getAttribute(Uptime.class.getName()), properties);
|
||||
uptime = RenderUtils.uptime((long) template.getAttribute(Uptime.class.getName()), properties);
|
||||
} else {
|
||||
return RenderUtils.uptime(ManagementFactory.getRuntimeMXBean().getUptime(), properties);
|
||||
uptime = RenderUtils.uptime(ManagementFactory.getRuntimeMXBean().getUptime(), properties);
|
||||
}
|
||||
|
||||
return template.getEncoder().encode(uptime);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,6 +43,6 @@ public class Year implements ValueRenderer {
|
|||
*/
|
||||
@Override
|
||||
public String render(Template template, String valueId, String differentiator) {
|
||||
return ZonedDateTime.now().format(RenderUtils.ISO_8601_YEAR_FORMATTER);
|
||||
return template.getEncoder().encode(ZonedDateTime.now().format(RenderUtils.ISO_8601_YEAR_FORMATTER));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,10 @@ class TestEncode {
|
|||
var t = TemplateFactory.TXT.get("encodeBase64");
|
||||
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||
assertThat(t.getContent()).isEqualTo(t.getValue(TestCase.FOO) + ": VGhpcyBpcyBhIHRlc3Qu");
|
||||
|
||||
t = TemplateFactory.HTML.get("encodeBase64");
|
||||
t.setValue(TestCase.FOO, TestCase.SAMPLE_TEXT + " URL Encoded.");
|
||||
assertThat(t.getContent()).as("with URL encoding").contains("VGhpcyBpcyBhIHRlc3QuIFVSTCBFbmNvZGVkLg%3D%3D");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -50,6 +54,11 @@ class TestEncode {
|
|||
var t = TemplateFactory.TXT.get("encodeJs");
|
||||
t.setAttribute(TestCase.FOO, "'\"\\/");
|
||||
assertThat(t.getContent()).isEqualTo("\\'\\\"\\\\\\/");
|
||||
|
||||
t = TemplateFactory.HTML.get("encodeJs");
|
||||
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
||||
assertThat(t.getContent()).as("with unicode")
|
||||
.isEqualTo("\\u005C\\u0022\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E\\u005C\\u0022");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -57,6 +66,10 @@ class TestEncode {
|
|||
var t = TemplateFactory.JSON.get("encodeJson");
|
||||
t.setAttribute(TestCase.FOO, "This is a \"•test\"");
|
||||
assertThat(t.getContent()).isEqualTo("{\n \"foo\": \"This is a \\\"\\u2022test\\\"\"\n}");
|
||||
|
||||
t = TemplateFactory.HTML.get("encodeJson");
|
||||
t.setAttribute(TestCase.FOO, "\"<test>\"");
|
||||
assertThat(t.getContent()).as("with html").isEqualTo("\\"<test>\\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -80,6 +93,11 @@ class TestEncode {
|
|||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||
assertThat(t.getContent()).isEqualTo(
|
||||
"\\u0054\\u0068\\u0069\\u0073\\u0020\\u0069\\u0073\\u0020\\u0061\\u0020\\u0074\\u0065\\u0073\\u0074\\u002E");
|
||||
|
||||
t = TemplateFactory.HTML.get("encodeUnicode");
|
||||
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
|
||||
assertThat(t.getContent()).as("with js")
|
||||
.contains("'\\\\u0022\\\\u0054\\\\u0068\\\\u0069\\\\u0073\\\\u0020\\\\u0069\\\\u0073\\\\u0020\\\\u0061\\\\u0020\\\\u0074\\\\u0065\\\\u0073\\\\u0074\\\\u002E\\\\u0022'");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -87,6 +105,11 @@ class TestEncode {
|
|||
var t = TemplateFactory.HTML.get("encodeUrl");
|
||||
t.setAttribute(TestCase.FOO, "a test &");
|
||||
assertThat(t.getContent()).isEqualTo("<a href=\"https://example.com/a%20test%20%26\">a test &</a>");
|
||||
|
||||
t = TemplateFactory.HTML.get("encodeUrlwithUnicode");
|
||||
t.setAttribute(TestCase.FOO, "a=test");
|
||||
assertThat(t.getContent()).as("with unicode")
|
||||
.contains("https://foo.com/\\u0061\\u0025\\u0033\\u0044\\u0074\\u0065\\u0073\\u0074");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
@ -27,7 +27,7 @@ class TestFormat {
|
|||
void testAbbreviate() {
|
||||
var t = TemplateFactory.HTML.get("abbreviate");
|
||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||
assertThat(t.getContent()).as("activate.html").endsWith("…").hasSize(12);
|
||||
assertThat(t.getContent()).as("activate.html").endsWith("…").hasSize(19);
|
||||
|
||||
t = TemplateFactory.TXT.get("abbreviate");
|
||||
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
|
||||
|
@ -52,7 +52,8 @@ class TestFormat {
|
|||
var t = TemplateFactory.HTML.get("mask");
|
||||
var foo = "374380141731053";
|
||||
t.setAttribute(TestCase.FOO, foo);
|
||||
assertThat(t.getContent()).as("mask.html").isEqualTo("3743•••••••••••");
|
||||
assertThat(t.getContent()).as("mask.html")
|
||||
.isEqualTo("3743•••••••••••");
|
||||
|
||||
t = TemplateFactory.TXT.get("mask");
|
||||
t.setAttribute(TestCase.FOO, foo);
|
||||
|
@ -95,7 +96,7 @@ class TestFormat {
|
|||
t = TemplateFactory.HTML.get("uptime");
|
||||
t.setAttribute(Uptime.class.getName(), 547800300076L);
|
||||
assertThat(t.getContent()).as("uptime.html")
|
||||
.isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
||||
.isEqualTo("17 années, 4 mois, 2 semaines, 1 jour, 6 heures, 45 minutes");
|
||||
t.setAttribute(Uptime.class.getName(), 120000L);
|
||||
assertThat(t.getContent()).as("uptime.html: 2 min").isEqualTo("2 minutes");
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@ package rife.render;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class TestRenderUtils {
|
||||
|
@ -35,6 +37,21 @@ class TestRenderUtils {
|
|||
.isEqualTo(TestCase.SAMPLE_TEXT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testEncode() {
|
||||
var p = new Properties();
|
||||
p.put(RenderUtils.ENCODING_PROPERTY, "html");
|
||||
assertThat(RenderUtils.encode("<a test &>", p)).as("html").isEqualTo("<a test &>");
|
||||
p.put(RenderUtils.ENCODING_PROPERTY, "js");
|
||||
assertThat(RenderUtils.encode("\"test'", p)).as("js").isEqualTo("\\\"test\\'");
|
||||
p.put(RenderUtils.ENCODING_PROPERTY, "unicode");
|
||||
assertThat(RenderUtils.encode("test", p)).as("unicode").isEqualTo("\\u0074\\u0065\\u0073\\u0074");
|
||||
p.put(RenderUtils.ENCODING_PROPERTY, "url");
|
||||
assertThat(RenderUtils.encode("a = test", p)).as("url").isEqualTo("a%20%3D%20test");
|
||||
p.put(RenderUtils.ENCODING_PROPERTY, "xml");
|
||||
assertThat(RenderUtils.encode("Joe's Café & Bar", p)).as("xml").isEqualTo("Joe's Café & Bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testHtmlEntities() {
|
||||
assertThat(RenderUtils.htmlEntities(SAMPLE_GERMAN))
|
||||
|
|
1
lib/src/test/resources/templates/encodeBase64.html
Normal file
1
lib/src/test/resources/templates/encodeBase64.html
Normal file
|
@ -0,0 +1 @@
|
|||
<a href="https://foo.com/{{v render:rife.render.EncodeBase64:foo}}encoding=url{{/v}}"><!--v foo/--></a>
|
1
lib/src/test/resources/templates/encodeJs.html
Normal file
1
lib/src/test/resources/templates/encodeJs.html
Normal file
|
@ -0,0 +1 @@
|
|||
<!--v render:rife.render.EncodeJs:foo-->encoding=unicode<!--/v-->
|
1
lib/src/test/resources/templates/encodeJson.html
Normal file
1
lib/src/test/resources/templates/encodeJson.html
Normal file
|
@ -0,0 +1 @@
|
|||
<!--v render:rife.render.EncodeJson:foo-->encoding=html<!--/v-->
|
1
lib/src/test/resources/templates/encodeUnicode.html
Normal file
1
lib/src/test/resources/templates/encodeUnicode.html
Normal file
|
@ -0,0 +1 @@
|
|||
<script>alert('{{v render:rife.render.EncodeUnicode:foo}}encoding=js{{/v}}');</script>
|
|
@ -0,0 +1 @@
|
|||
<script>window.open("https://foo.com/{{v render:rife.render.EncodeUrl:foo}}encoding=unicode{{/v}}")</script>
|
Loading…
Add table
Add a link
Reference in a new issue