diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index a4035eb..b53d9dc 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -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" diff --git a/lib/src/main/java/rife/render/Abbreviate.java b/lib/src/main/java/rife/render/Abbreviate.java index cfe679a..6944aa4 100644 --- a/lib/src/main/java/rife/render/Abbreviate.java +++ b/lib/src/main/java/rife/render/Abbreviate.java @@ -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)); } -} +} \ No newline at end of file diff --git a/lib/src/main/java/rife/render/BeatTime.java b/lib/src/main/java/rife/render/BeatTime.java index e1eaf44..dd4cfad 100644 --- a/lib/src/main/java/rife/render/BeatTime.java +++ b/lib/src/main/java/rife/render/BeatTime.java @@ -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())); } } \ No newline at end of file diff --git a/lib/src/main/java/rife/render/Capitalize.java b/lib/src/main/java/rife/render/Capitalize.java index edce803..dc4c126 100644 --- a/lib/src/main/java/rife/render/Capitalize.java +++ b/lib/src/main/java/rife/render/Capitalize.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/DateIso.java b/lib/src/main/java/rife/render/DateIso.java index b261268..3fe35c6 100644 --- a/lib/src/main/java/rife/render/DateIso.java +++ b/lib/src/main/java/rife/render/DateIso.java @@ -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)); } } diff --git a/lib/src/main/java/rife/render/DateTimeIso.java b/lib/src/main/java/rife/render/DateTimeIso.java index 683abb7..50ac866 100644 --- a/lib/src/main/java/rife/render/DateTimeIso.java +++ b/lib/src/main/java/rife/render/DateTimeIso.java @@ -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)); } } diff --git a/lib/src/main/java/rife/render/DateTimeRfc2822.java b/lib/src/main/java/rife/render/DateTimeRfc2822.java index 5058b70..a420201 100644 --- a/lib/src/main/java/rife/render/DateTimeRfc2822.java +++ b/lib/src/main/java/rife/render/DateTimeRfc2822.java @@ -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)); } } diff --git a/lib/src/main/java/rife/render/EncodeBase64.java b/lib/src/main/java/rife/render/EncodeBase64.java index c5bd854..fd2e62a 100644 --- a/lib/src/main/java/rife/render/EncodeBase64.java +++ b/lib/src/main/java/rife/render/EncodeBase64.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/EncodeJs.java b/lib/src/main/java/rife/render/EncodeJs.java index caafb18..1122356 100644 --- a/lib/src/main/java/rife/render/EncodeJs.java +++ b/lib/src/main/java/rife/render/EncodeJs.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/EncodeJson.java b/lib/src/main/java/rife/render/EncodeJson.java index 60916f6..7356650 100644 --- a/lib/src/main/java/rife/render/EncodeJson.java +++ b/lib/src/main/java/rife/render/EncodeJson.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/EncodeUnicode.java b/lib/src/main/java/rife/render/EncodeUnicode.java index 1d67bf1..60166a1 100644 --- a/lib/src/main/java/rife/render/EncodeUnicode.java +++ b/lib/src/main/java/rife/render/EncodeUnicode.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/EncodeUrl.java b/lib/src/main/java/rife/render/EncodeUrl.java index f6201d5..6692636 100644 --- a/lib/src/main/java/rife/render/EncodeUrl.java +++ b/lib/src/main/java/rife/render/EncodeUrl.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/FormatCreditCard.java b/lib/src/main/java/rife/render/FormatCreditCard.java index 840d66e..f2025eb 100644 --- a/lib/src/main/java/rife/render/FormatCreditCard.java +++ b/lib/src/main/java/rife/render/FormatCreditCard.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/Lowercase.java b/lib/src/main/java/rife/render/Lowercase.java index d966142..aa7858c 100644 --- a/lib/src/main/java/rife/render/Lowercase.java +++ b/lib/src/main/java/rife/render/Lowercase.java @@ -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())); } } diff --git a/lib/src/main/java/rife/render/Mask.java b/lib/src/main/java/rife/render/Mask.java index 897b415..a9c1e22 100644 --- a/lib/src/main/java/rife/render/Mask.java +++ b/lib/src/main/java/rife/render/Mask.java @@ -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)); } } diff --git a/lib/src/main/java/rife/render/Normalize.java b/lib/src/main/java/rife/render/Normalize.java index 636c8cb..c3e66a2 100644 --- a/lib/src/main/java/rife/render/Normalize.java +++ b/lib/src/main/java/rife/render/Normalize.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/RenderUtils.java b/lib/src/main/java/rife/render/RenderUtils.java index f46f1a2..139d17f 100644 --- a/lib/src/main/java/rife/render/RenderUtils.java +++ b/lib/src/main/java/rife/render/RenderUtils.java @@ -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. + *

Encodes the source {@code String} to the specified encoding.

* - * @param src the source String - * @return the encoded String + *

The supported encodings are:

+ * + * + * + * @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. + *

Returns the last 4 digits a credit card number.

+ * + * * * @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 goQR.me. + * Generates an SVG QR Code from the given {@code String} using goQR.me. * - * @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 { /** *

Shortens a URL using is.gid.

* - *

The URL String must be a valid http or https URL.

+ *

The URL {@code String} must be a valid http or https URL.

* *

Based on isgd-shorten

* @@ -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; } + } \ No newline at end of file diff --git a/lib/src/main/java/rife/render/Rot13.java b/lib/src/main/java/rife/render/Rot13.java index 4bca6db..d3a33c4 100644 --- a/lib/src/main/java/rife/render/Rot13.java +++ b/lib/src/main/java/rife/render/Rot13.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/ShortenUrl.java b/lib/src/main/java/rife/render/ShortenUrl.java index b52c686..3ddd62e 100644 --- a/lib/src/main/java/rife/render/ShortenUrl.java +++ b/lib/src/main/java/rife/render/ShortenUrl.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/SwapCase.java b/lib/src/main/java/rife/render/SwapCase.java index 270f1f6..c059a6a 100644 --- a/lib/src/main/java/rife/render/SwapCase.java +++ b/lib/src/main/java/rife/render/SwapCase.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/TimeIso.java b/lib/src/main/java/rife/render/TimeIso.java index e1f0023..9f99269 100644 --- a/lib/src/main/java/rife/render/TimeIso.java +++ b/lib/src/main/java/rife/render/TimeIso.java @@ -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)); } } diff --git a/lib/src/main/java/rife/render/Trim.java b/lib/src/main/java/rife/render/Trim.java index 53a66e1..fb97e31 100644 --- a/lib/src/main/java/rife/render/Trim.java +++ b/lib/src/main/java/rife/render/Trim.java @@ -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()); } } diff --git a/lib/src/main/java/rife/render/Uncapitalize.java b/lib/src/main/java/rife/render/Uncapitalize.java index a4ecd24..bef0dfe 100644 --- a/lib/src/main/java/rife/render/Uncapitalize.java +++ b/lib/src/main/java/rife/render/Uncapitalize.java @@ -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))); } } diff --git a/lib/src/main/java/rife/render/Uppercase.java b/lib/src/main/java/rife/render/Uppercase.java index bb45bb7..47a7eb5 100644 --- a/lib/src/main/java/rife/render/Uppercase.java +++ b/lib/src/main/java/rife/render/Uppercase.java @@ -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())); } } diff --git a/lib/src/main/java/rife/render/Uptime.java b/lib/src/main/java/rife/render/Uptime.java index 06b1371..0353e6c 100644 --- a/lib/src/main/java/rife/render/Uptime.java +++ b/lib/src/main/java/rife/render/Uptime.java @@ -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); } } diff --git a/lib/src/main/java/rife/render/Year.java b/lib/src/main/java/rife/render/Year.java index d146738..c7fe2e3 100644 --- a/lib/src/main/java/rife/render/Year.java +++ b/lib/src/main/java/rife/render/Year.java @@ -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)); } } diff --git a/lib/src/test/java/rife/render/TestEncode.java b/lib/src/test/java/rife/render/TestEncode.java index c5bc8bd..251b046 100644 --- a/lib/src/test/java/rife/render/TestEncode.java +++ b/lib/src/test/java/rife/render/TestEncode.java @@ -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, "\"\""); + 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 test &"); + + 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 diff --git a/lib/src/test/java/rife/render/TestFormat.java b/lib/src/test/java/rife/render/TestFormat.java index cf7cdb9..945e33f 100644 --- a/lib/src/test/java/rife/render/TestFormat.java +++ b/lib/src/test/java/rife/render/TestFormat.java @@ -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"); diff --git a/lib/src/test/java/rife/render/TestRenderUtils.java b/lib/src/test/java/rife/render/TestRenderUtils.java index bed7b86..6854bf5 100644 --- a/lib/src/test/java/rife/render/TestRenderUtils.java +++ b/lib/src/test/java/rife/render/TestRenderUtils.java @@ -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("", 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)) diff --git a/lib/src/test/resources/templates/encodeBase64.html b/lib/src/test/resources/templates/encodeBase64.html new file mode 100644 index 0000000..804270c --- /dev/null +++ b/lib/src/test/resources/templates/encodeBase64.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/lib/src/test/resources/templates/encodeJs.html b/lib/src/test/resources/templates/encodeJs.html new file mode 100644 index 0000000..ed9dd6f --- /dev/null +++ b/lib/src/test/resources/templates/encodeJs.html @@ -0,0 +1 @@ +encoding=unicode \ No newline at end of file diff --git a/lib/src/test/resources/templates/encodeJson.html b/lib/src/test/resources/templates/encodeJson.html new file mode 100644 index 0000000..17b675b --- /dev/null +++ b/lib/src/test/resources/templates/encodeJson.html @@ -0,0 +1 @@ +encoding=html \ No newline at end of file diff --git a/lib/src/test/resources/templates/encodeUnicode.html b/lib/src/test/resources/templates/encodeUnicode.html new file mode 100644 index 0000000..f1ee916 --- /dev/null +++ b/lib/src/test/resources/templates/encodeUnicode.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/lib/src/test/resources/templates/encodeUrlwithUnicode.html b/lib/src/test/resources/templates/encodeUrlwithUnicode.html new file mode 100644 index 0000000..b59f98b --- /dev/null +++ b/lib/src/test/resources/templates/encodeUrlwithUnicode.html @@ -0,0 +1 @@ + \ No newline at end of file