in wicket-core/src/main/java/org/apache/wicket/core/util/string/JavaScriptStripper.java [84:249]
public String stripCommentsAndWhitespace(String original)
{
// let's be optimistic
AppendingStringBuffer result = new AppendingStringBuffer(original.length() / 2);
int state = REGULAR_TEXT;
boolean wasNewLineInWhitespace = false;
for (int i = 0; i < original.length(); ++i)
{
char c = original.charAt(i);
char next = (i < original.length() - 1) ? original.charAt(i + 1) : 0;
char prev = (i > 0) ? original.charAt(i - 1) : 0;
if (state == WHITE_SPACE)
{
// WICKET 2060
if (c == '\n' && !wasNewLineInWhitespace)
{
result.append("\n");
wasNewLineInWhitespace = true;
}
if (Character.isWhitespace(next) == false)
{
state = REGULAR_TEXT;
}
continue;
}
if (state == REGULAR_TEXT)
{
if (c == '/' && next == '/' && prev != '\\')
{
state = LINE_COMMENT;
continue;
}
else if (c == '/' && next == '*')
{
state = MULTILINE_COMMENT;
++i;
continue;
}
else if (c == '/')
{
// This might be a divide operator, or it might be a regular expression.
// Work out if it's a regular expression by finding the previous non-whitespace
// char, which
// will be either '=' or '('. If it's not, it's just a divide operator.
int idx = result.length() - 1;
String trimmedResult = result.toString().trim();
while (idx > 0)
{
char tmp = result.charAt(idx);
if (Character.isWhitespace(tmp))
{
idx--;
continue;
}
if (tmp == '=' || tmp == '(' || tmp == '{' || tmp == ':' || tmp == ',' ||
tmp == '[' || tmp == ';' || tmp == '!' || trimmedResult.endsWith(RETURN_KEYWORD))
{
state = REG_EXP;
break;
}
break;
}
}
else if (Character.isWhitespace(c) && Character.isWhitespace(next))
{
// WICKET-2060
if (c == '\n' || next == '\n')
{
c = '\n';
wasNewLineInWhitespace = true;
}
else
{
c = ' ';
wasNewLineInWhitespace = false;
}
// ignore all whitespace characters after this one
state = WHITE_SPACE;
}
else if (c == '\'')
{
state = STRING_SINGLE_QUOTE;
}
else if (c == '"')
{
state = STRING_DOUBLE_QUOTES;
}
else if (c == '`')
{
state = TEMPLATE_LITERAL;
}
result.append(c);
continue;
}
if (state == LINE_COMMENT)
{
if (c == '\n' || c == '\r')
{
state = REGULAR_TEXT;
result.append(c);
continue;
}
}
if (state == MULTILINE_COMMENT)
{
if (c == '*' && next == '/')
{
state = REGULAR_TEXT;
++i;
continue;
}
}
if (state == STRING_SINGLE_QUOTE)
{
// to leave a string expression we need even (or zero) number of backslashes
int count = getPrevCount(original, i, '\\');
if (c == '\'' && count % 2 == 0)
{
state = REGULAR_TEXT;
}
result.append(c);
continue;
}
if (state == STRING_DOUBLE_QUOTES)
{
// to leave a string expression we need even (or zero) number of backslashes
int count = getPrevCount(original, i, '\\');
if (c == '"' && count % 2 == 0)
{
state = REGULAR_TEXT;
}
result.append(c);
continue;
}
if (state == REG_EXP)
{
// to leave regular expression we need even (or zero) number of backslashes
int count = getPrevCount(original, i, '\\');
if (c == '/' && count % 2 == 0)
{
state = REGULAR_TEXT;
}
result.append(c);
}
if (state == TEMPLATE_LITERAL) {
// to leave a template literal expression we need even (or zero) number of backslashes
int count = getPrevCount(original, i, '\\');
if (c == '`' && count % 2 == 0) {
state = REGULAR_TEXT;
}
result.append(c);
continue;
}
}
return result.toString();
}