1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 05:50:55 +01:00
phorge-phorge/externals/JsShrink/jsShrink.php
Merula Turdus 214988fafd T15774: Celerity: javascript minification issue with template literals (backticked strings)
Test Plan:
This archive contains a small demo: {F2142567}
When installed, an application named D25571 appears under "More Applications".
When you start it, you will see 2 messageboxes: one should be a single lined text, the other a multilined text.

Steps:
1) unpack archive in some directory
2) add ext-D25571/src/ path to load-libraries in local.json
3) bin/arc liberate
4) bin/cache purge --all
5) bin/celerity map
6) restart httpd
7) start D25571 application from More Applications

Also:

    arc unit ./src/applications/celerity/__tests__/CelerityResourceTransformerTestCase.php

Reviewers: O1 Blessed Committers, valerio.bozzolan

Reviewed By: O1 Blessed Committers, valerio.bozzolan

Subscribers: avivey, aklapper, tobiaswiese, valerio.bozzolan, Matthew, Cigaryno

Maniphest Tasks: T15774

Differential Revision: https://we.phorge.it/D25571
2024-06-11 17:09:47 +02:00

52 lines
1.7 KiB
PHP

<?php
/** Remove spaces and comments from JavaScript code
* @param string code with commands terminated by semicolon
* @return string shrinked code
* @link http://vrana.github.com/JsShrink/
* @author Jakub Vrana, http://www.vrana.cz/
* @copyright 2007 Jakub Vrana
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
function jsShrink($input) {
return preg_replace_callback('(
(?:
(`(?:\\\\.|[^`\\\\])*`) # template literal
|(^|[-+\([{}=,:;!%^&*|?~]|/(?![/*])|return|throw) # context before regexp
(?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
(/(?![/*])(?:
\\\\[^\n]
|[^[\n/\\\\]++
|\[(?:\\\\[^\n]|[^]])++
)+/) # regexp
|(^
|\'(?:\\\\.|[^\n\'\\\\])*\'
|"(?:\\\\.|[^\n"\\\\])*"
|([0-9A-Za-z_$]+)
|([-+]+)
|.
)
)(?:\s|//[^\n]*+\n|/\*(?:[^*]|\*(?!/))*+\*/)* # optional space
)sx', 'jsShrinkCallback', "$input\n");
}
function jsShrinkCallback($match) {
static $last = '';
$match += array_fill(1, 7, null); // avoid E_NOTICE
list(, $template, $context, $regexp, $result, $word, $operator) = $match;
if ($template) {
$result = $template;
} elseif ($word != '') {
$result = ($last == 'word' ? "\n" : ($last == 'return' ? " " : "")) . $result;
$last = ($word == 'return' || $word == 'throw' || $word == 'break' || $word == 'async' ? 'return' : 'word');
} elseif ($operator) {
$result = ($last == $operator[0] ? "\n" : "") . $result;
$last = $operator[0];
} else {
if ($regexp) {
$result = $context . ($context == '/' ? "\n" : "") . $regexp;
}
$last = '';
}
return $result;
}