Skip to content

Commit

Permalink
Added tests and functionality to trim strings with escaped new lines.
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Jan 26, 2014
1 parent 71b0217 commit f70f09e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
31 changes: 26 additions & 5 deletions src/JShrink/Minifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/jshrink/expect/preserve-strings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/Resources/jshrink/test/preserve-strings.js
Original file line number Diff line number Diff line change
@@ -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.