Added JS escaping for newline, linefeed, return, tab and backspace. Closes #1

This commit is contained in:
Erik C. Thauvin 2024-02-28 03:29:29 -08:00
parent a0ebfe2566
commit 0d14c27032
2 changed files with 10 additions and 0 deletions

View file

@ -195,6 +195,11 @@ public final class RenderUtils {
case '"' -> sb.append("\\\""); case '"' -> sb.append("\\\"");
case '\\' -> sb.append("\\\\"); case '\\' -> sb.append("\\\\");
case '/' -> sb.append("\\/"); case '/' -> sb.append("\\/");
case '\b' -> sb.append("\\b");
case '\n' -> sb.append(("\\n"));
case '\t' -> sb.append("\\t");
case '\f' -> sb.append("\\f");
case '\r' -> sb.append("\\r");
default -> sb.append(c); default -> sb.append(c);
} }
} }
@ -597,4 +602,5 @@ public final class RenderUtils {
} }
return false; return false;
} }
} }

View file

@ -55,6 +55,10 @@ class TestEncode {
t.setAttribute(TestCase.FOO, "'\"\\/"); t.setAttribute(TestCase.FOO, "'\"\\/");
assertThat(t.getContent()).isEqualTo("\\'\\\"\\\\\\/"); assertThat(t.getContent()).isEqualTo("\\'\\\"\\\\\\/");
t = TemplateFactory.TXT.get("encodeJs");
t.setAttribute(TestCase.FOO, "This is\f\b a\r\n\ttest");
assertThat(t.getContent()).isEqualTo("This is\\f\\b a\\r\\n\\ttest");
t = TemplateFactory.HTML.get("encodeJs"); t = TemplateFactory.HTML.get("encodeJs");
t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"'); t.setAttribute(TestCase.FOO, '"' + TestCase.SAMPLE_TEXT + '"');
assertThat(t.getContent()).as("with unicode") assertThat(t.getContent()).as("with unicode")