|
@@ -1,10 +1,6 @@
|
|
package com.ruoyi.common.utils.html;
|
|
package com.ruoyi.common.utils.html;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
-import java.util.Collections;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.List;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.ConcurrentMap;
|
|
import java.util.concurrent.ConcurrentMap;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
@@ -15,8 +11,7 @@ import java.util.regex.Pattern;
|
|
*
|
|
*
|
|
* @author ruoyi
|
|
* @author ruoyi
|
|
*/
|
|
*/
|
|
-public final class HTMLFilter
|
|
|
|
-{
|
|
|
|
|
|
+public final class HTMLFilter {
|
|
/**
|
|
/**
|
|
* regex flag union representing /si modifiers in php
|
|
* regex flag union representing /si modifiers in php
|
|
**/
|
|
**/
|
|
@@ -100,8 +95,7 @@ public final class HTMLFilter
|
|
/**
|
|
/**
|
|
* Default constructor.
|
|
* Default constructor.
|
|
*/
|
|
*/
|
|
- public HTMLFilter()
|
|
|
|
- {
|
|
|
|
|
|
+ public HTMLFilter() {
|
|
vAllowed = new HashMap<>();
|
|
vAllowed = new HashMap<>();
|
|
|
|
|
|
final ArrayList<String> a_atts = new ArrayList<>();
|
|
final ArrayList<String> a_atts = new ArrayList<>();
|
|
@@ -122,13 +116,13 @@ public final class HTMLFilter
|
|
vAllowed.put("i", no_atts);
|
|
vAllowed.put("i", no_atts);
|
|
vAllowed.put("em", no_atts);
|
|
vAllowed.put("em", no_atts);
|
|
|
|
|
|
- vSelfClosingTags = new String[] { "img" };
|
|
|
|
- vNeedClosingTags = new String[] { "a", "b", "strong", "i", "em" };
|
|
|
|
- vDisallowed = new String[] {};
|
|
|
|
- vAllowedProtocols = new String[] { "http", "mailto", "https" }; // no ftp.
|
|
|
|
- vProtocolAtts = new String[] { "src", "href" };
|
|
|
|
- vRemoveBlanks = new String[] { "a", "b", "strong", "i", "em" };
|
|
|
|
- vAllowedEntities = new String[] { "amp", "gt", "lt", "quot" };
|
|
|
|
|
|
+ vSelfClosingTags = new String[]{"img"};
|
|
|
|
+ vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"};
|
|
|
|
+ vDisallowed = new String[]{};
|
|
|
|
+ vAllowedProtocols = new String[]{"http", "mailto", "https"}; // no ftp.
|
|
|
|
+ vProtocolAtts = new String[]{"src", "href"};
|
|
|
|
+ vRemoveBlanks = new String[]{"a", "b", "strong", "i", "em"};
|
|
|
|
+ vAllowedEntities = new String[]{"amp", "gt", "lt", "quot"};
|
|
stripComment = true;
|
|
stripComment = true;
|
|
encodeQuotes = true;
|
|
encodeQuotes = true;
|
|
alwaysMakeTags = false;
|
|
alwaysMakeTags = false;
|
|
@@ -140,8 +134,7 @@ public final class HTMLFilter
|
|
* @param conf map containing configuration. keys match field names.
|
|
* @param conf map containing configuration. keys match field names.
|
|
*/
|
|
*/
|
|
@SuppressWarnings("unchecked")
|
|
@SuppressWarnings("unchecked")
|
|
- public HTMLFilter(final Map<String, Object> conf)
|
|
|
|
- {
|
|
|
|
|
|
+ public HTMLFilter(final Map<String, Object> conf) {
|
|
|
|
|
|
assert conf.containsKey("vAllowed") : "configuration requires vAllowed";
|
|
assert conf.containsKey("vAllowed") : "configuration requires vAllowed";
|
|
assert conf.containsKey("vSelfClosingTags") : "configuration requires vSelfClosingTags";
|
|
assert conf.containsKey("vSelfClosingTags") : "configuration requires vSelfClosingTags";
|
|
@@ -165,20 +158,17 @@ public final class HTMLFilter
|
|
alwaysMakeTags = conf.containsKey("alwaysMakeTags") ? (Boolean) conf.get("alwaysMakeTags") : true;
|
|
alwaysMakeTags = conf.containsKey("alwaysMakeTags") ? (Boolean) conf.get("alwaysMakeTags") : true;
|
|
}
|
|
}
|
|
|
|
|
|
- private void reset()
|
|
|
|
- {
|
|
|
|
|
|
+ private void reset() {
|
|
vTagCounts.clear();
|
|
vTagCounts.clear();
|
|
}
|
|
}
|
|
|
|
|
|
// ---------------------------------------------------------------
|
|
// ---------------------------------------------------------------
|
|
// my versions of some PHP library functions
|
|
// my versions of some PHP library functions
|
|
- public static String chr(final int decimal)
|
|
|
|
- {
|
|
|
|
|
|
+ public static String chr(final int decimal) {
|
|
return String.valueOf((char) decimal);
|
|
return String.valueOf((char) decimal);
|
|
}
|
|
}
|
|
|
|
|
|
- public static String htmlSpecialChars(final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ public static String htmlSpecialChars(final String s) {
|
|
String result = s;
|
|
String result = s;
|
|
result = regexReplace(P_AMP, "&", result);
|
|
result = regexReplace(P_AMP, "&", result);
|
|
result = regexReplace(P_QUOTE, """, result);
|
|
result = regexReplace(P_QUOTE, """, result);
|
|
@@ -195,8 +185,7 @@ public final class HTMLFilter
|
|
* @param input text (i.e. submitted by a user) than may contain html
|
|
* @param input text (i.e. submitted by a user) than may contain html
|
|
* @return "clean" version of input, with only valid, whitelisted html elements allowed
|
|
* @return "clean" version of input, with only valid, whitelisted html elements allowed
|
|
*/
|
|
*/
|
|
- public String filter(final String input)
|
|
|
|
- {
|
|
|
|
|
|
+ public String filter(final String input) {
|
|
reset();
|
|
reset();
|
|
String s = input;
|
|
String s = input;
|
|
|
|
|
|
@@ -213,22 +202,18 @@ public final class HTMLFilter
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean isAlwaysMakeTags()
|
|
|
|
- {
|
|
|
|
|
|
+ public boolean isAlwaysMakeTags() {
|
|
return alwaysMakeTags;
|
|
return alwaysMakeTags;
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean isStripComments()
|
|
|
|
- {
|
|
|
|
|
|
+ public boolean isStripComments() {
|
|
return stripComment;
|
|
return stripComment;
|
|
}
|
|
}
|
|
|
|
|
|
- private String escapeComments(final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String escapeComments(final String s) {
|
|
final Matcher m = P_COMMENTS.matcher(s);
|
|
final Matcher m = P_COMMENTS.matcher(s);
|
|
final StringBuffer buf = new StringBuffer();
|
|
final StringBuffer buf = new StringBuffer();
|
|
- if (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.find()) {
|
|
final String match = m.group(1); // (.*?)
|
|
final String match = m.group(1); // (.*?)
|
|
m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));
|
|
}
|
|
}
|
|
@@ -237,10 +222,8 @@ public final class HTMLFilter
|
|
return buf.toString();
|
|
return buf.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- private String balanceHTML(String s)
|
|
|
|
- {
|
|
|
|
- if (alwaysMakeTags)
|
|
|
|
- {
|
|
|
|
|
|
+ private String balanceHTML(String s) {
|
|
|
|
+ if (alwaysMakeTags) {
|
|
//
|
|
//
|
|
// try and form html
|
|
// try and form html
|
|
//
|
|
//
|
|
@@ -249,9 +232,7 @@ public final class HTMLFilter
|
|
s = regexReplace(P_BODY_TO_END, "<$1>", s);
|
|
s = regexReplace(P_BODY_TO_END, "<$1>", s);
|
|
s = regexReplace(P_XML_CONTENT, "$1<$2", s);
|
|
s = regexReplace(P_XML_CONTENT, "$1<$2", s);
|
|
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
//
|
|
//
|
|
// escape stray brackets
|
|
// escape stray brackets
|
|
//
|
|
//
|
|
@@ -269,13 +250,11 @@ public final class HTMLFilter
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
- private String checkTags(String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String checkTags(String s) {
|
|
Matcher m = P_TAGS.matcher(s);
|
|
Matcher m = P_TAGS.matcher(s);
|
|
|
|
|
|
final StringBuffer buf = new StringBuffer();
|
|
final StringBuffer buf = new StringBuffer();
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
String replaceStr = m.group(1);
|
|
String replaceStr = m.group(1);
|
|
replaceStr = processTag(replaceStr);
|
|
replaceStr = processTag(replaceStr);
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(replaceStr));
|
|
@@ -285,10 +264,8 @@ public final class HTMLFilter
|
|
// these get tallied in processTag
|
|
// these get tallied in processTag
|
|
// (remember to reset before subsequent calls to filter method)
|
|
// (remember to reset before subsequent calls to filter method)
|
|
final StringBuilder sBuilder = new StringBuilder(buf.toString());
|
|
final StringBuilder sBuilder = new StringBuilder(buf.toString());
|
|
- for (String key : vTagCounts.keySet())
|
|
|
|
- {
|
|
|
|
- for (int ii = 0; ii < vTagCounts.get(key); ii++)
|
|
|
|
- {
|
|
|
|
|
|
+ for (String key : vTagCounts.keySet()) {
|
|
|
|
+ for (int ii = 0; ii < vTagCounts.get(key); ii++) {
|
|
sBuilder.append("</").append(key).append(">");
|
|
sBuilder.append("</").append(key).append(">");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -297,18 +274,14 @@ public final class HTMLFilter
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
- private String processRemoveBlanks(final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String processRemoveBlanks(final String s) {
|
|
String result = s;
|
|
String result = s;
|
|
- for (String tag : vRemoveBlanks)
|
|
|
|
- {
|
|
|
|
- if (!P_REMOVE_PAIR_BLANKS.containsKey(tag))
|
|
|
|
- {
|
|
|
|
|
|
+ for (String tag : vRemoveBlanks) {
|
|
|
|
+ if (!P_REMOVE_PAIR_BLANKS.containsKey(tag)) {
|
|
P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">"));
|
|
P_REMOVE_PAIR_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?></" + tag + ">"));
|
|
}
|
|
}
|
|
result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result);
|
|
result = regexReplace(P_REMOVE_PAIR_BLANKS.get(tag), "", result);
|
|
- if (!P_REMOVE_SELF_BLANKS.containsKey(tag))
|
|
|
|
- {
|
|
|
|
|
|
+ if (!P_REMOVE_SELF_BLANKS.containsKey(tag)) {
|
|
P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));
|
|
P_REMOVE_SELF_BLANKS.putIfAbsent(tag, Pattern.compile("<" + tag + "(\\s[^>]*)?/>"));
|
|
}
|
|
}
|
|
result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result);
|
|
result = regexReplace(P_REMOVE_SELF_BLANKS.get(tag), "", result);
|
|
@@ -317,25 +290,19 @@ public final class HTMLFilter
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private static String regexReplace(final Pattern regex_pattern, final String replacement, final String s) {
|
|
Matcher m = regex_pattern.matcher(s);
|
|
Matcher m = regex_pattern.matcher(s);
|
|
return m.replaceAll(replacement);
|
|
return m.replaceAll(replacement);
|
|
}
|
|
}
|
|
|
|
|
|
- private String processTag(final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String processTag(final String s) {
|
|
// ending tags
|
|
// ending tags
|
|
Matcher m = P_END_TAG.matcher(s);
|
|
Matcher m = P_END_TAG.matcher(s);
|
|
- if (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.find()) {
|
|
final String name = m.group(1).toLowerCase();
|
|
final String name = m.group(1).toLowerCase();
|
|
- if (allowed(name))
|
|
|
|
- {
|
|
|
|
- if (!inArray(name, vSelfClosingTags))
|
|
|
|
- {
|
|
|
|
- if (vTagCounts.containsKey(name))
|
|
|
|
- {
|
|
|
|
|
|
+ if (allowed(name)) {
|
|
|
|
+ if (!inArray(name, vSelfClosingTags)) {
|
|
|
|
+ if (vTagCounts.containsKey(name)) {
|
|
vTagCounts.put(name, vTagCounts.get(name) - 1);
|
|
vTagCounts.put(name, vTagCounts.get(name) - 1);
|
|
return "</" + name + ">";
|
|
return "</" + name + ">";
|
|
}
|
|
}
|
|
@@ -345,35 +312,30 @@ public final class HTMLFilter
|
|
|
|
|
|
// starting tags
|
|
// starting tags
|
|
m = P_START_TAG.matcher(s);
|
|
m = P_START_TAG.matcher(s);
|
|
- if (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.find()) {
|
|
final String name = m.group(1).toLowerCase();
|
|
final String name = m.group(1).toLowerCase();
|
|
final String body = m.group(2);
|
|
final String body = m.group(2);
|
|
String ending = m.group(3);
|
|
String ending = m.group(3);
|
|
|
|
|
|
// debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" );
|
|
// debug( "in a starting tag, name='" + name + "'; body='" + body + "'; ending='" + ending + "'" );
|
|
- if (allowed(name))
|
|
|
|
- {
|
|
|
|
|
|
+ if (allowed(name)) {
|
|
final StringBuilder params = new StringBuilder();
|
|
final StringBuilder params = new StringBuilder();
|
|
|
|
|
|
final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);
|
|
final Matcher m2 = P_QUOTED_ATTRIBUTES.matcher(body);
|
|
final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);
|
|
final Matcher m3 = P_UNQUOTED_ATTRIBUTES.matcher(body);
|
|
final List<String> paramNames = new ArrayList<>();
|
|
final List<String> paramNames = new ArrayList<>();
|
|
final List<String> paramValues = new ArrayList<>();
|
|
final List<String> paramValues = new ArrayList<>();
|
|
- while (m2.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m2.find()) {
|
|
paramNames.add(m2.group(1)); // ([a-z0-9]+)
|
|
paramNames.add(m2.group(1)); // ([a-z0-9]+)
|
|
paramValues.add(m2.group(3)); // (.*?)
|
|
paramValues.add(m2.group(3)); // (.*?)
|
|
}
|
|
}
|
|
- while (m3.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m3.find()) {
|
|
paramNames.add(m3.group(1)); // ([a-z0-9]+)
|
|
paramNames.add(m3.group(1)); // ([a-z0-9]+)
|
|
paramValues.add(m3.group(3)); // ([^\"\\s']+)
|
|
paramValues.add(m3.group(3)); // ([^\"\\s']+)
|
|
}
|
|
}
|
|
|
|
|
|
String paramName, paramValue;
|
|
String paramName, paramValue;
|
|
- for (int ii = 0; ii < paramNames.size(); ii++)
|
|
|
|
- {
|
|
|
|
|
|
+ for (int ii = 0; ii < paramNames.size(); ii++) {
|
|
paramName = paramNames.get(ii).toLowerCase();
|
|
paramName = paramNames.get(ii).toLowerCase();
|
|
paramValue = paramValues.get(ii);
|
|
paramValue = paramValues.get(ii);
|
|
|
|
|
|
@@ -381,72 +343,55 @@ public final class HTMLFilter
|
|
// debug( "paramValue='" + paramValue + "'" );
|
|
// debug( "paramValue='" + paramValue + "'" );
|
|
// debug( "allowed? " + vAllowed.get( name ).contains( paramName ) );
|
|
// debug( "allowed? " + vAllowed.get( name ).contains( paramName ) );
|
|
|
|
|
|
- if (allowedAttribute(name, paramName))
|
|
|
|
- {
|
|
|
|
- if (inArray(paramName, vProtocolAtts))
|
|
|
|
- {
|
|
|
|
|
|
+ if (allowedAttribute(name, paramName)) {
|
|
|
|
+ if (inArray(paramName, vProtocolAtts)) {
|
|
paramValue = processParamProtocol(paramValue);
|
|
paramValue = processParamProtocol(paramValue);
|
|
}
|
|
}
|
|
params.append(' ').append(paramName).append("=\\\"").append(paramValue).append("\\\"");
|
|
params.append(' ').append(paramName).append("=\\\"").append(paramValue).append("\\\"");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- if (inArray(name, vSelfClosingTags))
|
|
|
|
- {
|
|
|
|
|
|
+ if (inArray(name, vSelfClosingTags)) {
|
|
ending = " /";
|
|
ending = " /";
|
|
}
|
|
}
|
|
|
|
|
|
- if (inArray(name, vNeedClosingTags))
|
|
|
|
- {
|
|
|
|
|
|
+ if (inArray(name, vNeedClosingTags)) {
|
|
ending = "";
|
|
ending = "";
|
|
}
|
|
}
|
|
|
|
|
|
- if (ending == null || ending.length() < 1)
|
|
|
|
- {
|
|
|
|
- if (vTagCounts.containsKey(name))
|
|
|
|
- {
|
|
|
|
|
|
+ if (ending == null || ending.length() < 1) {
|
|
|
|
+ if (vTagCounts.containsKey(name)) {
|
|
vTagCounts.put(name, vTagCounts.get(name) + 1);
|
|
vTagCounts.put(name, vTagCounts.get(name) + 1);
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
vTagCounts.put(name, 1);
|
|
vTagCounts.put(name, 1);
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
ending = " /";
|
|
ending = " /";
|
|
}
|
|
}
|
|
return "<" + name + params + ending + ">";
|
|
return "<" + name + params + ending + ">";
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// comments
|
|
// comments
|
|
m = P_COMMENT.matcher(s);
|
|
m = P_COMMENT.matcher(s);
|
|
- if (!stripComment && m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ if (!stripComment && m.find()) {
|
|
return "<" + m.group() + ">";
|
|
return "<" + m.group() + ">";
|
|
}
|
|
}
|
|
|
|
|
|
return "";
|
|
return "";
|
|
}
|
|
}
|
|
|
|
|
|
- private String processParamProtocol(String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String processParamProtocol(String s) {
|
|
s = decodeEntities(s);
|
|
s = decodeEntities(s);
|
|
final Matcher m = P_PROTOCOL.matcher(s);
|
|
final Matcher m = P_PROTOCOL.matcher(s);
|
|
- if (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ if (m.find()) {
|
|
final String protocol = m.group(1);
|
|
final String protocol = m.group(1);
|
|
- if (!inArray(protocol, vAllowedProtocols))
|
|
|
|
- {
|
|
|
|
|
|
+ if (!inArray(protocol, vAllowedProtocols)) {
|
|
// bad protocol, turn into local anchor link instead
|
|
// bad protocol, turn into local anchor link instead
|
|
s = "#" + s.substring(protocol.length() + 1);
|
|
s = "#" + s.substring(protocol.length() + 1);
|
|
- if (s.startsWith("#//"))
|
|
|
|
- {
|
|
|
|
|
|
+ if (s.startsWith("#//")) {
|
|
s = "#" + s.substring(3);
|
|
s = "#" + s.substring(3);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -455,13 +400,11 @@ public final class HTMLFilter
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
- private String decodeEntities(String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String decodeEntities(String s) {
|
|
StringBuffer buf = new StringBuffer();
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
|
|
Matcher m = P_ENTITY.matcher(s);
|
|
Matcher m = P_ENTITY.matcher(s);
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
final String match = m.group(1);
|
|
final String match = m.group(1);
|
|
final int decimal = Integer.decode(match).intValue();
|
|
final int decimal = Integer.decode(match).intValue();
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
@@ -471,8 +414,7 @@ public final class HTMLFilter
|
|
|
|
|
|
buf = new StringBuffer();
|
|
buf = new StringBuffer();
|
|
m = P_ENTITY_UNICODE.matcher(s);
|
|
m = P_ENTITY_UNICODE.matcher(s);
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
final String match = m.group(1);
|
|
final String match = m.group(1);
|
|
final int decimal = Integer.valueOf(match, 16).intValue();
|
|
final int decimal = Integer.valueOf(match, 16).intValue();
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
@@ -482,8 +424,7 @@ public final class HTMLFilter
|
|
|
|
|
|
buf = new StringBuffer();
|
|
buf = new StringBuffer();
|
|
m = P_ENCODE.matcher(s);
|
|
m = P_ENCODE.matcher(s);
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
final String match = m.group(1);
|
|
final String match = m.group(1);
|
|
final int decimal = Integer.valueOf(match, 16).intValue();
|
|
final int decimal = Integer.valueOf(match, 16).intValue();
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(chr(decimal)));
|
|
@@ -495,14 +436,12 @@ public final class HTMLFilter
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
|
|
|
|
- private String validateEntities(final String s)
|
|
|
|
- {
|
|
|
|
|
|
+ private String validateEntities(final String s) {
|
|
StringBuffer buf = new StringBuffer();
|
|
StringBuffer buf = new StringBuffer();
|
|
|
|
|
|
// validate entities throughout the string
|
|
// validate entities throughout the string
|
|
Matcher m = P_VALID_ENTITIES.matcher(s);
|
|
Matcher m = P_VALID_ENTITIES.matcher(s);
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
final String one = m.group(1); // ([^&;]*)
|
|
final String one = m.group(1); // ([^&;]*)
|
|
final String two = m.group(2); // (?=(;|&|$))
|
|
final String two = m.group(2); // (?=(;|&|$))
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
|
|
m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
|
|
@@ -512,14 +451,11 @@ public final class HTMLFilter
|
|
return encodeQuotes(buf.toString());
|
|
return encodeQuotes(buf.toString());
|
|
}
|
|
}
|
|
|
|
|
|
- private String encodeQuotes(final String s)
|
|
|
|
- {
|
|
|
|
- if (encodeQuotes)
|
|
|
|
- {
|
|
|
|
|
|
+ private String encodeQuotes(final String s) {
|
|
|
|
+ if (encodeQuotes) {
|
|
StringBuffer buf = new StringBuffer();
|
|
StringBuffer buf = new StringBuffer();
|
|
Matcher m = P_VALID_QUOTES.matcher(s);
|
|
Matcher m = P_VALID_QUOTES.matcher(s);
|
|
- while (m.find())
|
|
|
|
- {
|
|
|
|
|
|
+ while (m.find()) {
|
|
final String one = m.group(1); // (>|^)
|
|
final String one = m.group(1); // (>|^)
|
|
final String two = m.group(2); // ([^<]+?)
|
|
final String two = m.group(2); // ([^<]+?)
|
|
final String three = m.group(3); // (<|$)
|
|
final String three = m.group(3); // (<|$)
|
|
@@ -528,43 +464,34 @@ public final class HTMLFilter
|
|
}
|
|
}
|
|
m.appendTail(buf);
|
|
m.appendTail(buf);
|
|
return buf.toString();
|
|
return buf.toString();
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
|
|
+ } else {
|
|
return s;
|
|
return s;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private String checkEntity(final String preamble, final String term)
|
|
|
|
- {
|
|
|
|
|
|
+ private String checkEntity(final String preamble, final String term) {
|
|
|
|
|
|
return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble;
|
|
return ";".equals(term) && isValidEntity(preamble) ? '&' + preamble : "&" + preamble;
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean isValidEntity(final String entity)
|
|
|
|
- {
|
|
|
|
|
|
+ private boolean isValidEntity(final String entity) {
|
|
return inArray(entity, vAllowedEntities);
|
|
return inArray(entity, vAllowedEntities);
|
|
}
|
|
}
|
|
|
|
|
|
- private static boolean inArray(final String s, final String[] array)
|
|
|
|
- {
|
|
|
|
- for (String item : array)
|
|
|
|
- {
|
|
|
|
- if (item != null && item.equals(s))
|
|
|
|
- {
|
|
|
|
|
|
+ private static boolean inArray(final String s, final String[] array) {
|
|
|
|
+ for (String item : array) {
|
|
|
|
+ if (item != null && item.equals(s)) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean allowed(final String name)
|
|
|
|
- {
|
|
|
|
|
|
+ private boolean allowed(final String name) {
|
|
return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed);
|
|
return (vAllowed.isEmpty() || vAllowed.containsKey(name)) && !inArray(name, vDisallowed);
|
|
}
|
|
}
|
|
|
|
|
|
- private boolean allowedAttribute(final String name, final String paramName)
|
|
|
|
- {
|
|
|
|
|
|
+ private boolean allowedAttribute(final String name, final String paramName) {
|
|
return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));
|
|
return allowed(name) && (vAllowed.isEmpty() || vAllowed.get(name).contains(paramName));
|
|
}
|
|
}
|
|
}
|
|
}
|