[ Index ]
 

Code source de SPIP 1.9.2c

Accédez au Source d'autres logiciels libres

title

Body

[fermer]

/ecrire/inc/ -> compacte_js.php (sommaire)

(pas de description)

Poids: 501 lignes (20 kb)
Inclus ou requis:0 fois
Référencé: 0 fois
Nécessite: 0 fichiers

Définit 3 classes

SourceMap:: (3 méthodes):
  getMap()
  __endCharNoSlash()
  __charNoSlash()

BaseConvert:: (3 méthodes):
  BaseConvert()
  toBase()
  fromBase()

JavaScriptCompressor:: (16 méthodes):
  JavaScriptCompressor()
  getClean()
  getPacked()
  __addCleanCode()
  __addClean()
  __clean()
  __commonInitMethods()
  __getHeader()
  __getScriptNames()
  __getSize()
  __getTime()
  __pack()
  __setStats()
  __sourceManager()
  __sourcePusher()
  __wordsParser()


Classe: SourceMap  - X-Ref

SourceMap class,
reads a generic language source code and returns its map.
______________________________________________________________
The SourceMap goals is to create a map of a generic script/program language.
The getMap method returns an array/list of arrays/dictionary/objects
of source map using delimeters variable to map correctly:
- multi line comments
- single line comments
- double quoted strings
- single quoted strings
- pure code
- everything else (for example regexp [/re/] with javascript), just adding a correct delimeter
--------------------------------------------------------------
What about the delimeter
It's an array/list of arrays/dictionary/obects with some properties to find what you're looking for.

parameters are:
- name, the name of the delimeter (i.e. "doublequote")
- start, one or mode chars to find as start delimeter (i.e. " for double quoted string)
- end, one or mode chars to find as end delimeter (i.e. " for double quoted string) [end should be an array/list too]

optional parameters are:
- noslash, if true find the end of the delimeter only if last char is not slashed (i.e. "string\"test" find " after test)
- match, if choosed language has regexp, verify if string from start to end matches used regexp (i.e. /^\/[^\n\r]+\/$/ for JavaScript regexp)

If end parameter is an array, match and noslash are not supported (i.e. ["\n", "\r"] for end delimeter of a single line comment)
--------------------------------------------------------------
What about SourceMap usage
It should be a good solution to create sintax highlighter, parser,
verifier or some other source code parsing procedure
--------------------------------------------------------------
What about SourceMap performance script/languages
I've created different version of this class to test each script/program language performance too.
Python with or without Psyco is actually the faster parser.
However with this PHP version this class has mapped "dojo.js.uncompressed.js" file (about 211Kb) in less than 0.5 second.
Test has been done with embed class and PHP as module, any accelerator was used for this PHP test.
--------------------------------------------------------------
getMap(&$source, &$delimeters)   X-Ref
public method
getMap(&$source:string, &$delimeters:array):array
Maps the source code using $delimeters rules and returns map as an array
NOTE: read comments to know more about map and delimeter

param: string        generic source code
param: array        array with nested array with code rules

__endCharNoSlash(&$source, $position, &$find, &$len)   X-Ref
Pas de description

__charNoSlash(&$source, &$position)   X-Ref
Pas de description

Classe: BaseConvert  - X-Ref

BaseConvert class,
converts an unsigned base 10 integer to a different base and vice versa.
______________________________________________________________
BaseConvert
|
|________ constructor(newBase:string)
|             uses newBase string var for convertion
|                 [i.e. "0123456789abcdef" for an hex convertion]
|
|________ toBase(unsignedInteger:uint):string
|             return base value of input
|
|________ fromBase(baseString:string):uint
return base 10 integer value of base input
--------------------------------------------------------------
REMEMBER: PHP < 6 doesn't work correctly with integer greater than 2147483647 (2^31 - 1)
--------------------------------------------------------------

BaseConvert($base)   X-Ref
Pas de description

toBase($num)   X-Ref
Pas de description

fromBase($str)   X-Ref
Pas de description

Classe: JavaScriptCompressor  - X-Ref

JavaScriptCompressor class,
removes comments or pack JavaScript source[s] code.
______________________________________________________________
JavaScriptCompressor (just 2 public methods)
|
|________ getClean(jsSource:mixed):string
|             returns one or more JavaScript code without comments,
|             by default removes some spaces too
|
|________ getPacked(jsSource:mixed):string
returns one or more JavaScript code packed,
using getClean and obfuscating output
--------------------------------------------------------------
Note about $jsSource input varible:
this var should be a string (i.e. $jsSource = file_get_contents("myFile.js");)
should be an array of strings (i.e. array(file_get_contents("1.js"), file_get_contents("2.js"), ... ))
should be an array with 1 or 2 keys:
(i.e. array('code'=>file_get_contents("mySource.js")))
(i.e. array('code'=>file_get_contents("mySource.js"), 'name'=>'mySource'))
... and should be an array of arrays created with theese rules
array(
file_get_contents("secret.js"),
array('code'=>$anotherJS),
array('code'=>$myJSapplication, 'name'=>'JSApplication V 1.0')
)

The name used on dedicated key, will be write on parsed source header
--------------------------------------------------------------
Note about returned strings:
Your browser should wrap very long strings, then don't use
cut and paste from your browser, save output into your database or directly
in a file or print them only inside <script> and </script> tags
--------------------------------------------------------------
Note about parser performance:
With pure PHP embed code this class should be slow and not really safe
for your server performance then don't parse JavaScript runtime for each
file you need and create some "parsed" caching system
(at least while i've not created a compiled version of theese class functions).
Here there's a caching system example: http://www.phpclasses.org/browse/package/3158.html
--------------------------------------------------------------
Note about JavaScript packed compatibility:
To be sure about compatibility include before every script JSL Library:
http://www.devpro.it/JSL/
JSL library add some features for old or buggy browsers, one of
those functions is String.replace with function as second argument,
used by JavaScript generated packed code to rebuild original code.

Remember that KDE 3.5, Safari and IE5 will not work correctly with packed version
if you'll not include JSL.
--------------------------------------------------------------
JavaScriptCompressor()   X-Ref
public constructor
creates a new BaseConvert class variable (base 36)


getClean($jsSource)   X-Ref
public method
getClean(mixed [, bool]):string
compress JavaScript removing comments and somespaces (on by default)

param: mixed        view example and notes on class comments

getPacked($jsSource)   X-Ref
public method
getPacked(mixed):string
compress JavaScript replaceing words and removing comments and some spaces

param: mixed        view example and notes on class comments

__addCleanCode($str)   X-Ref
Pas de description

__addClean(&$arr, &$str, &$start, &$end, $clean)   X-Ref
Pas de description

__clean(&$str)   X-Ref
Pas de description

__commonInitMethods(&$jsSource, $packed)   X-Ref
Pas de description

__getHeader()   X-Ref
Pas de description

__getScriptNames()   X-Ref
Pas de description

__getSize($size, $dec = 2)   X-Ref
Pas de description

__getTime($startTime = null)   X-Ref
Pas de description

__pack(&$str)   X-Ref
Pas de description

__setStats()   X-Ref
Pas de description

__sourceManager(&$jsSource)   X-Ref
Pas de description

__sourcePusher(&$code, $name)   X-Ref
Pas de description

__wordsParser($str, &$d)   X-Ref
Pas de description



Généré le : Wed Nov 21 10:20:27 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics