Permalink
Browse files

Added tests and functionality to trim strings with escaped new lines.

  • Loading branch information...
1 parent 71b0217 commit f70f09eeceb754118551c1a500ecf41097933a58 @tedivm tedivm committed Jan 26, 2014
View
31 src/JShrink/Minifier.php
@@ -398,21 +398,42 @@ protected function saveString()
break 2;
- // New lines in strings without line delimiters are bad.
+
+ // New lines in strings without line delimiters are bad- actual
+ // new lines will be represented by the string \n and not the actual
+ // character, so those will be treated just fine using the switch
+ // block below.
case "\n":
throw new \RuntimeException('Unclosed string. ' . $this->index);
break;
- // This prevents escaped characters from being removed
- // This also catches multiline strings signified by a \
+ // Escaped characters get picked up here. If it's an escaped new line it's not really needed
case '\\':
+
+ // a is a slash. We want to keep it, and the next character,
+ // unless it's a new line. New lines as actual strings will be
+ // preserved, but escaped new lines should be reduced.
+ $this->b = $this->getChar();
+
+ // If b is a new line we discard a and b and restart the loop.
+ if($this->b == "\n") {
+ break;
+ }
+
+ // echo out the escaped character and restart the loop.
+ echo $this->a . $this->b;
+ break;
+
+
+ // Since we're not dealing with any special cases we simply
+ // output the character and continue our loop.
+ default:
echo $this->a;
- $this->a = $this->getChar();
}
// Echo a- it'll be set to the next char at the start of the loop
- echo $this->a;
+ // echo $this->a;
}
}
View
2 tests/Resources/jshrink/expect/preserve-strings.js
@@ -1 +1 @@
-var test="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+var test="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";var test="abcdefg\"hijklmnopqrst\"uvwxyzABCDEFGHIJKLMNO\"PQRSTUVWXYZ0123456789";var test="abcdefghijklmnopqrstuvwxyzABCD EFGHIJKLMNOPQRSTUVWXYZ0123456789";
View
8 tests/Resources/jshrink/test/preserve-strings.js
@@ -1 +1,9 @@
var test = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+var test = "abcdefg\"hijklmnopqrst\"uvwxyzABCDEFGHIJKLMNO\"PQRSTUVWXYZ0123456789";
+
+
+
+var test = "abcdefghij\
+klmnopqrstuvwxyzABCD \
+EFGHIJKLMNOPQRSTUVWXYZ0123456789";

0 comments on commit f70f09e

Please sign in to comment.