* @param allowTags the allowable tags
*/
public static StringValue strip_tags(StringValue string,
@Optional Value allowTags)
{
StringValue result = string.createStringBuilder(string.length());
HashSet<StringValue> allowedTagMap = null;
if (! allowTags.isDefault())
allowedTagMap = getAllowedTags(allowTags.toStringValue());
int len = string.length();
for (int i = 0; i < len; i++) {
char ch = string.charAt(i);
if (i + 1 >= len || ch != '<') {
result.append(ch);
continue;
}
ch = string.charAt(i + 1);
if (Character.isWhitespace(ch)) {
i++;
result.append('<');
result.append(ch);
continue;
}
int tagNameStart = i + 1;
if (ch == '/')
tagNameStart++;
int j = tagNameStart;
while (j < len
&& (ch = string.charAt(j)) != '>'
// && ch != '/'
&& ! Character.isWhitespace(ch)) {
j++;
}
StringValue tagName = string.substring(tagNameStart, j);
int tagEnd = 0;
if (allowedTagMap != null && allowedTagMap.contains(tagName)) {
result.append(string, i, Math.min(j + 1, len));
}