Escape content for use in HTML. This escaping is suitable for the following uses: Element content when the escaped data will be placed directly inside tags such as <p>, <td> etc. Attribute values when the attribute value is quoted with " or '. @par
(String content)
| 40 | * @return The escaped content or {@code null} if the content was {@code null} |
| 41 | */ |
| 42 | public static String htmlElementContent(String content) { |
| 43 | if (content == null) { |
| 44 | return null; |
| 45 | } |
| 46 | |
| 47 | StringBuilder sb = new StringBuilder(); |
| 48 | |
| 49 | for (int i = 0; i < content.length(); i++) { |
| 50 | char c = content.charAt(i); |
| 51 | if (c == '<') { |
| 52 | sb.append("<"); |
| 53 | } else if (c == '>') { |
| 54 | sb.append(">"); |
| 55 | } else if (c == '\'') { |
| 56 | sb.append("'"); |
| 57 | } else if (c == '&') { |
| 58 | sb.append("&"); |
| 59 | } else if (c == '"') { |
| 60 | sb.append("""); |
| 61 | } else if (c == '/') { |
| 62 | sb.append("/"); |
| 63 | } else { |
| 64 | sb.append(c); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return (sb.length() > content.length()) ? sb.toString() : content; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /** |