Skip to content
Snippets Groups Projects
Commit 45be45c5 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

small improvements on JS compressor (maybe fixes #807)

darcs-hash:20060527140157-7ad00-225a9ba66f658cc279cb44ebb04d483291d567d2.gz
parent 44409c3d
No related branches found
No related tags found
No related merge requests found
......@@ -40,15 +40,25 @@ class js_js_compress_test extends UnitTestCase {
}
function test_dquot1(){
$text = 'var foo="Now what \'do we//get /*here*/ ?";';
$text = 'var foo="Now what \\" \'do we//get /*here*/ ?";';
$this->assertEqual(js_compress($text), $text);
}
function test_dquotrunaway(){
$text = 'var foo="Now where does it end';
$this->assertEqual(js_compress($text), "$text\n"); //\n is added by compressor
}
function test_squot1(){
$text = "var foo='Now what \"do we//get /*here*/ ?';";
$text = "var foo='Now what \\' \"do we//get /*here*/ ?';";
$this->assertEqual(js_compress($text), $text);
}
function test_squotrunaway(){
$text = "var foo='Now where does it end";
$this->assertEqual(js_compress($text), "$text\n"); //\n is added by compressor
}
function test_nl1(){
$text = "var foo=6;\nvar baz=7;";
$this->assertEqual(js_compress($text), 'var foo=6;var baz=7;');
......@@ -63,6 +73,12 @@ class js_js_compress_test extends UnitTestCase {
$text = "var foo=6; \t ";
$this->assertEqual(js_compress($text), 'var foo=6;');
}
function test_shortcond(){
$text = "var foo = (baz) ? 'bar' : 'bla';";
$this->assertEqual(js_compress($text), "var foo=(baz)?'bar':'bla';");
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
......@@ -238,7 +238,7 @@ function js_compress($s){
$len = strlen($s);
// items that don't need spaces next to them
$chars = '^&|!+\-*\/%=:;,{}()<>% \t\n\r';
$chars = '^&|!+\-*\/%=\?:;,{}()<>% \t\n\r';
ob_start();
while($i < $len){
......@@ -286,11 +286,12 @@ function js_compress($s){
// double quote strings
if($ch == '"'){
$j = 1;
while( $s{$i+$j} != '"' ){
while( ($s{$i+$j} != '\\') && ($s{$i+$j} != '"') ){
$j = $j + 1;
while( $s{$i+$j} != '"' && ($i+$j < $len)){
if( $s{$i+$j} == '\\' && $s{$i+$j+1} == '"' ){
$j += 2;
}else{
$j += 1;
}
if($s{$i+$j} == '\\') $j = $j + 2;
}
echo substr($s,$i,$j+1);
$i = $i + $j + 1;
......@@ -300,11 +301,12 @@ function js_compress($s){
// single quote strings
if($ch == "'"){
$j = 1;
while( $s{$i+$j} != "'" ){
while( ($s{$i+$j} != '\\') && ($s{$i+$j} != "'") ){
$j = $j + 1;
while( $s{$i+$j} != "'" && ($i+$j < $len)){
if( $s{$i+$j} == '\\' && $s{$i+$j+1} == "'" ){
$j += 2;
}else{
$j += 1;
}
if ($s{$i+$j} == '\\') $j = $j + 2;
}
echo substr($s,$i,$j+1);
$i = $i + $j + 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment