diff options
author | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-04-12 11:03:14 +0200 |
---|---|---|
committer | Jean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com> | 2013-04-12 11:03:14 +0200 |
commit | 9f42fb95a0d3ff069f72bdd94de1ab1c2e9d771f (patch) | |
tree | 4be56324f7fbe936d0204064a8d4067988b14c08 | |
parent | 83ef5eeadef022ca6c7d7bdc69733c68d3949fc5 (diff) | |
download | sonarqube-9f42fb95a0d3ff069f72bdd94de1ab1c2e9d771f.tar.gz sonarqube-9f42fb95a0d3ff069f72bdd94de1ab1c2e9d771f.zip |
SONAR-3893 Added ampersand encoding
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java | 13 | ||||
-rw-r--r-- | sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java | 34 |
2 files changed, 43 insertions, 4 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java index 81bbd8e1b16..ec5c17df42f 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java +++ b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java @@ -42,8 +42,10 @@ public class HtmlTextWrapper { public static final char LF_END_OF_LINE = '\n'; public static final char HTML_OPENING = '<'; public static final char HTML_CLOSING = '>'; + public static final char AMPERSAND = '&'; public static final String ENCODED_HTML_OPENING = "<"; public static final String ENCODED_HTML_CLOSING = ">"; + public static final String ENCODED_AMPERSAND = "&"; public List<String> wrapTextWithHtml(String text, HighlightingContext context) { @@ -100,12 +102,17 @@ public class HtmlTextWrapper { } private char[] normalize(char currentChar) { + char[] normalizedChars; if(currentChar == HTML_OPENING) { - return ENCODED_HTML_OPENING.toCharArray(); + normalizedChars = ENCODED_HTML_OPENING.toCharArray(); } else if(currentChar == HTML_CLOSING) { - return ENCODED_HTML_CLOSING.toCharArray(); + normalizedChars = ENCODED_HTML_CLOSING.toCharArray(); + } else if(currentChar == AMPERSAND) { + normalizedChars = ENCODED_AMPERSAND.toCharArray(); + } else { + normalizedChars = new char[]{currentChar}; } - return new char[]{currentChar}; + return normalizedChars; } private boolean shouldAppendCharToHtmlOutput(CharactersReader charsReader) { diff --git a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java index 0182722689a..e90680ef6c1 100644 --- a/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java +++ b/sonar-core/src/test/java/org/sonar/core/source/HtmlTextWrapperTest.java @@ -165,7 +165,7 @@ public class HtmlTextWrapperTest { } @Test - public void should_encode_markup_chars() throws Exception { + public void should_escape_markup_chars() throws Exception { String javadocWithHtml = "/**\n" + @@ -199,4 +199,36 @@ public class HtmlTextWrapperTest { "<span class=\"cppd\"> * </ul></span>", "<span class=\"cppd\"> */</span>"); } + + @Test + public void should_escape_ampersand_char() throws Exception { + + String javadocWithAmpersandChar = + "/**\n" + + " * Definition of a dashboard.\n" + + " * <p/>\n" + + " * Its name and description can be retrieved using the i18n mechanism, using the keys \"dashboard.<id>.name\" and\n" + + " * \"dashboard.<id>.description\".\n" + + " *\n" + + " * @since 2.13\n" + + " */\n"; + + HighlightingContext context = HighlightingContext.buildFrom("0,220,cppd;"); + + HtmlTextWrapper htmlTextWrapper = new HtmlTextWrapper(); + List<String> htmlOutput = htmlTextWrapper.wrapTextWithHtml(javadocWithAmpersandChar, context); + + assertThat(htmlOutput).containsExactly( + "<span class=\"cppd\">/**</span>", + "<span class=\"cppd\"> * Definition of a dashboard.</span>", + "<span class=\"cppd\"> * <p/></span>", + "<span class=\"cppd\"> * Its name and description can be retrieved using the i18n mechanism, using the keys \"dashboard.&lt;id&gt;.name\" and</span>", + "<span class=\"cppd\"> * \"dashboard.&lt;id&gt;.description\".</span>", + "<span class=\"cppd\"> *</span>", + "<span class=\"cppd\"> * @since 2.13</span>", + "<span class=\"cppd\"> */</span>"); + + + + } } |