http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73193
Revision: 73193
Author: tstarling
Date: 2010-09-17 03:52:53 +0000 (Fri, 17 Sep 2010)
Log Message:
-----------
Tried to make it so that I can read this code without going insane, by
replacing the integer literals with constants, and re-adding some comments
which were left out during the port from jsmin.c
Modified Paths:
--------------
trunk/phase3/includes/libs/JSMin.php
Modified: trunk/phase3/includes/libs/JSMin.php
===================================================================
--- trunk/phase3/includes/libs/JSMin.php 2010-09-17 01:03:24 UTC (rev
73192)
+++ trunk/phase3/includes/libs/JSMin.php 2010-09-17 03:52:53 UTC (rev
73193)
@@ -49,8 +49,17 @@
const ORD_LF = 10;
const ORD_SPACE = 32;
+ // Action constants
+ const OUTPUT = 1;
+ const DELETE_A = 2;
+ const DELETE_B = 3;
+
+ /** Current character */
protected $a = '';
+
+ /** Next character */
protected $b = '';
+
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
@@ -74,12 +83,22 @@
// -- Protected Instance Methods
---------------------------------------------
+ /**
+ * Do something! What you do is determined by the argument:
+ * - self::OUTPUT Output A. Copy B to A. Get the next
B.
+ * - self::DELETE_A Copy B to A. Get the next B. (Delete
A).
+ * - self::DELETE_B Get the next B. (Delete B).
+ * action treats a string as a single character. Wow!
+ * action recognizes a regular expression if it is preceded by ( or ,
or =.
+ */
protected function action( $d ) {
switch( $d ) {
- case 1:
+ case self::OUTPUT:
+ // Output A. Copy B to A. Get the next B.
$this->output .= $this->a;
- case 2:
+ case self::DELETE_A:
+ // Copy B to A. Get the next B. (Delete A).
$this->a = $this->b;
if ( $this->a === "'" || $this->a === '"' ) {
@@ -102,7 +121,8 @@
}
}
- case 3:
+ case self::DELETE_B:
+ // Get the next B. (Delete B).
$this->b = $this->next();
if ( $this->b === '/' && (
@@ -133,6 +153,11 @@
}
}
+ /**
+ * Return the next character from the input. Watch out for lookahead. If
+ * the character is a control character, translate it to a space or
+ * linefeed.
+ */
protected function get() {
$c = $this->lookAhead;
$this->lookAhead = null;
@@ -157,21 +182,31 @@
return ' ';
}
+ /**
+ * Return true if the character is a letter, digit, underscore,
+ * dollar sign, or non-ASCII character.
+ */
protected function isAlphaNum( $c ) {
return ord( $c ) > 126 || $c === '\\' || preg_match(
'/^[\w\$]$/', $c ) === 1;
}
+ /**
+ * Copy the input to the output, deleting the characters which are
+ * insignificant to JavaScript. Comments will be removed. Tabs will be
+ * replaced with spaces. Carriage returns will be replaced with
linefeeds.
+ * Most spaces and linefeeds will be removed.
+ */
protected function min() {
$this->a = "\n";
- $this->action( 3 );
+ $this->action( self::DELETE_B );
while ( $this->a !== null ) {
switch ( $this->a ) {
case ' ':
if ( $this->isAlphaNum( $this->b ) ) {
- $this->action( 1 );
+ $this->action( self::OUTPUT );
} else {
- $this->action( 2 );
+ $this->action( self::DELETE_A );
}
break;
@@ -182,19 +217,19 @@
case '(':
case '+':
case '-':
- $this->action( 1 );
+ $this->action(
self::OUTPUT );
break;
case ' ':
- $this->action( 3 );
+ $this->action(
self::DELETE_B );
break;
default:
if ( $this->isAlphaNum(
$this->b ) ) {
- $this->action(
1 );
+ $this->action(
self::OUTPUT );
}
else {
- $this->action(
2 );
+ $this->action(
self::DELETE_A );
}
}
break;
@@ -203,11 +238,11 @@
switch ( $this->b ) {
case ' ':
if ( $this->isAlphaNum(
$this->a ) ) {
- $this->action(
1 );
+ $this->action(
self::OUTPUT );
break;
}
- $this->action( 3 );
+ $this->action(
self::DELETE_B );
break;
case "\n":
@@ -219,21 +254,21 @@
case '-':
case '"':
case "'":
-
$this->action( 1 );
+
$this->action( self::OUTPUT );
break;
default:
if (
$this->isAlphaNum( $this->a ) ) {
-
$this->action( 1 );
+
$this->action( self::OUTPUT );
}
else {
-
$this->action( 3 );
+
$this->action( self::DELETE_B );
}
}
break;
default:
- $this->action( 1 );
+ $this->action(
self::OUTPUT );
break;
}
}
@@ -242,6 +277,10 @@
return $this->output;
}
+ /**
+ * Get the next character, excluding comments. peek() is used to see
+ * if a '/' is followed by a '/' or '*'.
+ */
protected function next() {
$c = $this->get();
@@ -281,6 +320,9 @@
return $c;
}
+ /**
+ * Get the next character without getting it.
+ */
protected function peek() {
$this->lookAhead = $this->get();
return $this->lookAhead;
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs
