EscapeUtils { public static String escapeSingleLine(String line) { StringBuilder escaped = new StringBuilder(line.length() + 16); String currentLine = line; if (currentLine.startsWith(QUOTE_START)) { escaped.append(currentLine.substring(0, 1)); currentLine = currentLine.substring(1); } int url = currentLine.indexOf(URL_START); while (url > -1) { String before = currentLine.substring(0, url); before = escapeBackticks(escaped, before); escaped.append(StringEscapeUtils.escapeHtml4(before)); int urlEnd = currentLine.indexOf(">", url) + 1; escaped.append(currentLine.substring(url, urlEnd)); currentLine = currentLine.substring(urlEnd); url = currentLine.indexOf(URL_START); } currentLine = escapeBackticks(escaped, currentLine); escaped.append(StringEscapeUtils.escapeHtml4(currentLine)); return escaped.toString(); } private  EscapeUtils(); static String escapeMarkdown(String unescaped); static String escapeSingleLine(String line); static List<String> escapeLines(List<String> lines); static String preserveRuleTagQuotes(String text);  }
@Test public void testEscapeHtmlWithinMarkdownSingleLine() { assertEquals("a &lt;script&gt; tag outside of `<script>` backticks should be escaped", EscapeUtils.escapeSingleLine("a <script> tag outside of `<script>` backticks should be escaped")); assertEquals("a &lt;script&gt; &quot;tag&quot; outside of `<script>` backticks should be escaped &lt;multiple&gt; times `<strong>`.", EscapeUtils.escapeSingleLine("a <script> \"tag\" outside of `<script>` backticks should be escaped <multiple> times `<strong>`.")); assertEquals("URLS: a https: EscapeUtils.escapeSingleLine("URLS: a https: assertEquals("multiple URLS: <https: EscapeUtils.escapeSingleLine("multiple URLS: <https: assertEquals("URL: <http: EscapeUtils.escapeSingleLine("URL: <http: assertEquals("> this is a quote line", EscapeUtils.escapeSingleLine("> this is a quote line")); assertEquals("combination of URLs and backticks: <https: EscapeUtils.escapeSingleLine("combination of URLs and backticks: <https: assertEquals("combination of URLs and backticks: `<script>` &lt;strong&gt;escaped&lt;/strong&gt; but <https: EscapeUtils.escapeSingleLine("combination of URLs and backticks: `<script>` <strong>escaped</strong> but <https: }