ZF-8011: addJavascript() uses preg_replace when trim would suffice


In ZendX/JQuery/View/Helper/JQuery/Container.php - addJavascript($js) function, the first line is:

$js = preg_replace('/^\s(.?)\s*$/s', '$1', $js);

I believe this is simply trimming whitespace from either end. trim function would be more efficient.


If $js is a very large string then addJavascript($js) fails to add $js as expected.

In my case (checking with preg_last_error) I had reached/exceeded the backtrack limit (PREG_BACKTRACK_LIMIT_ERROR).

Using trim() may be more efficient and would also remove this limit.

The preg_replace does not work with large string in deed. This is critical bug, please fix it.

My suggestion is to remove lines:

        $js = preg_replace('/^\s*(.*?)\s*$/s', '$1', $js);
        if (!in_array(substr($js, -1), array(';', '}'))) {
            $js .= ';';

Reasons: * Why should PHP class fix JavaScript code which a programmer wrote ? He could make much more mistakes then to forget ; in last line or to enter too many spaces. * This processing is taking unnecessary CPU on each request. * It is confusing to the JavaScript programmer that his code got changed.

If there is a plan to include some more sophisticated JavaScript minifier (I don't see a practical usage until it will not support caching) it should be optional.

Thank you for your contributed work to ZF.

On my critical list for bughunt day this week.

Fixed and merged in 1.9 release branch.