Fix issue with prepro.js where double-slashes inside block comments were filtered out (e.g. as part of URLs).

This commit is contained in:
Jürg Lehni 2011-11-10 18:29:50 +01:00
parent eb34e058c0
commit 8a048e8448

View file

@ -129,50 +129,46 @@ function stripComments(str) {
var singleQuote = false, var singleQuote = false,
doubleQuote = false, doubleQuote = false,
blockComment = false, blockComment = false,
lineComment = false; lineComment = false,
var removed = []; preserveComment = false;
for (var i = 0, l = str.length; i < l; i++) { for (var i = 0, l = str.length; i < l; i++) {
if (singleQuote) { if (singleQuote) {
if (str[i] == "'" && str[i-1] !== '\\') if (str[i] == "'" && str[i - 1] !== '\\')
singleQuote = false; singleQuote = false;
continue; } else if (doubleQuote) {
} if (str[i] == '"' && str[i - 1] !== '\\')
if (doubleQuote) {
if (str[i] == '"' && str[i-1] !== '\\')
doubleQuote = false; doubleQuote = false;
continue; } else if (blockComment) {
} // Is the block comment closing?
if (blockComment) { if (str[i] == '*' && str[i + 1] == '/') {
if (str[i] == '*' && str[i+1] == '/') { if (!preserveComment)
str[i+1] = ''; str[i] = str[i + 1] = '';
blockComment = false; blockComment = preserveComment = false;
} else if (!preserveComment) {
str[i] = '';
} }
removed.push(str[i]); } else if (lineComment) {
str[i] = ''; // One-line comments end with the line-break
continue; if (str[i + 1] == '\n' || str[i + 1] == '\r')
}
if (lineComment) {
if (str[i+1] == '\n' || str[i+1] == '\r')
lineComment = false; lineComment = false;
str[i] = ''; str[i] = '';
continue; } else {
doubleQuote = str[i] == '"';
singleQuote = str[i] == "'";
if (!blockComment && str[i] == '/') {
if (str[i + 1] == '*') {
// Do not filter out conditional comments and comments marked
// as protected (/*! */)
preserveComment = str[i + 2] == '@' || str[i + 2] == '!';
if (!preserveComment)
str[i] = '';
blockComment = true;
} else if (str[i + 1] == '/') {
str[i] = '';
lineComment = true;
}
}
} }
doubleQuote = str[i] == '"';
singleQuote = str[i] == "'";
if (str[i] == '/') {
// Do not filter out conditional comments and comments marked
// as protected (/*! */)
if (str[i+1] == '*' && str[i+2] != '@' && str[i+2] != '!') {
str[i] = '';
blockComment = true;
continue;
}
if (str[i+1] == '/') {
str[i] = '';
lineComment = true;
continue;
}
}
} }
return str.join('').slice(2, -2); return str.join('').slice(2, -2);
} }