Skip to content

Commit

Permalink
[TASK] Update JavaScriptMinifier class
Browse files Browse the repository at this point in the history
  • Loading branch information
fnagel committed Jan 25, 2016
1 parent 9bbe5f8 commit 26a2f93
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions Resources/Private/Php/JavaScriptMinifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
// @codingStandardsIgnoreFile File external to MediaWiki. Ignore coding conventions checks.
/**
* JavaScript Minifier
*
Expand Down Expand Up @@ -59,7 +60,7 @@ class JavaScriptMinifier {
const TYPE_DO = 15; // keywords: case, var, finally, else, do, try
const TYPE_FUNC = 16; // keywords: function
const TYPE_LITERAL = 17; // all literals, identifiers and unrecognised tokens

// Sanity limit to avoid excessive memory usage
const STACK_LIMIT = 1000;

Expand All @@ -72,9 +73,9 @@ class JavaScriptMinifier {
* literals (e.g. quoted strings) longer than $maxLineLength are encountered
* or when required to guard against semicolon insertion.
*
* @param $s String JavaScript code to minify
* @param $statementsOnOwnLine Bool Whether to put each statement on its own line
* @param $maxLineLength Int Maximum length of a single line, or -1 for no maximum.
* @param string $s JavaScript code to minify
* @param bool $statementsOnOwnLine Whether to put each statement on its own line
* @param int $maxLineLength Maximum length of a single line, or -1 for no maximum.
* @return String Minified code
*/
public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength = 1000 ) {
Expand Down Expand Up @@ -385,7 +386,7 @@ public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength
self::TYPE_LITERAL => true
)
);

// Rules for when newlines should be inserted if
// $statementsOnOwnLine is enabled.
// $newlineBefore is checked before switching state,
Expand Down Expand Up @@ -514,7 +515,7 @@ public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength
return self::parseError($s, $end, 'Number with several E' );
}
$end++;

// + sign is optional; - sign is required.
$end += strspn( $s, '-+', $end );
$len = strspn( $s, '0123456789', $end );
Expand Down Expand Up @@ -564,13 +565,21 @@ public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength
$out .= ' ';
$lineLength++;
}

if (
$type === self::TYPE_LITERAL
&& ( $token === 'true' || $token === 'false' )
&& ( $state === self::EXPRESSION || $state === self::PROPERTY_EXPRESSION )
&& $last !== '.'
) {
$token = ( $token === 'true' ) ? '!0' : '!1';
}

$out .= $token;
$lineLength += $end - $pos; // += strlen( $token )
$last = $s[$end - 1];
$pos = $end;
$newlineFound = false;

// Output a newline after the token if required
// This is checked before AND after switching state
$newlineAdded = false;
Expand All @@ -589,7 +598,7 @@ public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength
} elseif( isset( $goto[$state][$type] ) ) {
$state = $goto[$state][$type];
}

// Check for newline insertion again
if ( $statementsOnOwnLine && !$newlineAdded && isset( $newlineAfter[$state][$type] ) ) {
$out .= "\n";
Expand All @@ -598,9 +607,9 @@ public static function minify( $s, $statementsOnOwnLine = false, $maxLineLength
}
return $out;
}

static function parseError($fullJavascript, $position, $errorMsg) {
// TODO: Handle the error: trigger_error, throw exception, return false...
return false;
}
}
}

0 comments on commit 26a2f93

Please sign in to comment.