Re: [closure-compiler-discuss] Special character problems

123 views
Skip to first unread message

Alan Leung

unread,
Jan 9, 2013, 4:21:01 PM1/9/13
to closure-comp...@googlegroups.com
On Wed, Jan 9, 2013 at 12:49 PM, Korbinian Moser
<korbini...@gmail.com> wrote:
> Try this code in API or the simple POST form:
>
> console.log({'test':'ä'});
>
> It will be compressed to an empty string.

Can you tell me how are you POST'ing to the API?

I just use the compily.py from:
https://developers.google.com/closure/compiler/docs/api-tutorial1

python compile.py 'console.log({'test':'ä'});'

and got:

console.log({test:\u00e4});

Korbinian Moser

unread,
Jan 9, 2013, 6:34:32 PM1/9/13
to closure-comp...@googlegroups.com
public static function compressJS( $string)
{
    require_once( 'Zend/Http/Client.php');
    $client = new Zend_Http_Client( 'http://closure-compiler.appspot.com/compile', array('maxredirects'=>5,'timeout'=>60));    
    $client->setParameterPost( array
    (
        'js_code' => $string,
        'compilation_level' => 'WHITESPACE_ONLY',
        'output_info' => 'compiled_code',
        'output_format' => 'text',
    ));
    // $client->setHeaders( 'Content-type', 'application/x-www-form-urlencoded'); // didn't make a difference
    $response = $client->request('POST');
    // exit( $string.'<br /><br />- - - <br /><br />'.$response->getBody().'<br /><br />- - - <br /><br />'.print_r( $response, true));
    return $response->getBody();
}

Alan Leung

unread,
Jan 9, 2013, 7:17:23 PM1/9/13
to closure-comp...@googlegroups.com
Having no knowledge of Zend or PHP, my guess is you need to some how
tell the API you are posting UTF8 charsets.

As I demonstrated with the python example, I don't believe it is a bug
in the compilation service.

-Alan

Nick Santos

unread,
Jan 9, 2013, 7:21:55 PM1/9/13
to closure-comp...@googlegroups.com
AFAIR, PHP's string encoding is pretty bizarre. from
http://php.net/manual/en/language.types.string.php
"A string is series of characters, where a character is the same as a
byte. This means that PHP only supports a 256-character set, and hence
does not offer native Unicode support."

i suspect that your PHP is mangling your string somehow.

Korbinian Moser

unread,
Jan 10, 2013, 4:09:03 AM1/10/13
to closure-comp...@googlegroups.com
Seems, you are right... I modified the simple form example a little bit, and now it works:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
    <form action="http://closure-compiler.appspot.com/compile" method="POST">
    <p>Type JavaScript code to optimize here:</p>
    <textarea name="js_code" cols="50" rows="5">console.log({'test':'ä'});</textarea>
    <input type="hidden" name="compilation_level" value="SIMPLE_OPTIMIZATIONS">
    <input type="hidden" name="output_format" value="text">
    <input type="hidden" name="output_info" value="compiled_code">
    <br><br>
    <input type="submit" value="Optimize">
    </form>
  </body>
</html>


Has anyone an idea, how I can make PHP send the string correctly?

Korbinian Moser

unread,
Jan 10, 2013, 8:11:17 AM1/10/13
to closure-comp...@googlegroups.com
For anyone else with my problem, here's my working solution:

    public static function compressJS( $string)
    {
        $data = 'compilation_level=SIMPLE_OPTIMIZATIONS&';
        $data .= 'output_info=errors&';
        $data .= 'output_info=warnings&';
        $data .= 'output_info=statistics&';
        $data .= 'output_info=compiled_code&';
        $data .= 'output_format=json&';
        $data .= 'js_code='.urlencode($string);

       
        require_once( 'Zend/Http/Client.php');
        $client = new Zend_Http_Client( 'http://closure-compiler.appspot.com/compile', array('maxredirects'=>5,'timeout'=>60));   
        $client->setRawData( $data, 'application/x-www-form-urlencoded');

        $response = $client->request('POST');
       
        require_once( 'Zend/Json.php');
        $result = Zend_Json::decode( $response->getBody());
       
        if( isset($result['errors'])) trigger_error( Zend_Json::encode($result['errors']), E_USER_ERROR);
        elseif( isset($result['serverErrors'])) trigger_error( Zend_Json::encode($result['serverErrors']), E_USER_ERROR);
        elseif( isset($result['warnings'])) trigger_error( Zend_Json::encode($result['warnings']), E_USER_WARNING );
       
        return isset($result['compiledCode']) ? $result['compiledCode'] : '';
    }

Now, I'm even not sure, if the problem really had been caused by the specialchars, or just by trailing commas in arrays of my JS code... The encoding of specialcars in regex rules also doesn't affect function, as it seems. Thanks for the help!
Reply all
Reply to author
Forward
0 new messages