TextContentFragment implements Fragment { static String escapeHTML(String text) { int length = text.length(); int index = findHTMLReservedChar(text); if (index == length) return text; StringBuilder builder = new StringBuilder(length * 2); for (int i = 0; i < index; i++) builder.append(text.charAt(i)); for (; index < length; index++) { char ch = text.charAt(index); switch (ch) { case '<' -> builder.append("&lt;"); case '>' -> builder.append("&gt;"); case '"' -> builder.append("&quot;"); case '&' -> builder.append("&amp;"); case '\'' -> builder.append("&#39;"); case '/' -> builder.append("&#47;"); default -> builder.append(ch); } } return builder.toString(); } TextContentFragment(String expression, TemplateMetaContext context, String location); @Override void process(StringBuilder builder, TemplateContext context);  }
@Test void escapeHTML() { assertEquals("text", TextContentFragment.escapeHTML("text")); assertEquals("before&lt;p&gt;text&lt;&#47;p&gt;after", TextContentFragment.escapeHTML("before<p>text</p>after")); assertEquals("&lt;html&gt;&lt;&#47;html&gt;", TextContentFragment.escapeHTML("<html></html>")); }