@@ -8,65 +8,89 @@ See the accompanying LICENSE file for terms.
88/*
99This utility prints out sanitized html content
1010*/
11- var Debug = require ( "debug" ) ,
12- progname = 'HTML-Purifier' ;
13- var Purifier = require ( '../src/html-purify' ) ;
14-
15- Debug . enable ( progname ) ;
11+ var Purifier = require ( '../src/html-purify' ) ,
12+ Benchmark = require ( 'benchmark' ) ;
1613
1714( function ( ) {
1815 var fs = require ( 'fs' ) ,
19- file ,
20- lineByLine = 0 ,
21- noofargs = 0 ;
16+ file ,
17+ // lineByLine = false,
18+ benchmark = false ,
19+ noofargs = process . argv . length ;
2220
2321 process . argv . forEach ( function ( val , index ) {
24- ++ noofargs ;
2522 if ( index === 2 ) {
2623 file = val ;
24+ } else if ( index === 3 ) {
25+ if ( val === "--benchmark" ) {
26+ benchmark = true ;
27+ }
28+ // else if (val === "-l") {
29+ // lineByLine = true;
30+ // }
2731 }
28- if ( index === 3 ) {
29- if ( val === "-l" ) {
30- lineByLine = 1 ;
31- }
32- }
3332 } ) ;
3433
35- if ( noofargs >= 3 ) {
36- if ( fs . existsSync ( file ) ) {
37- var data = fs . readFileSync ( file , 'utf-8' ) ;
38- var i ;
39- var output = '' ;
40- if ( lineByLine ) {
41- // reading and processing line by line
42- var data2 = data . split ( / \n / ) ;
43- for ( i = 0 ; i < data2 . length ; i ++ ) {
44- if ( data2 [ i ] . length !== 0 ) {
45- output = ( new Purifier ( ) ) . purify ( data2 [ i ] ) ;
46- console . log ( "*****" ) ;
47- console . log ( "input ==> " + data2 [ i ] ) ;
48- console . log ( "output ==> " + output ) ;
49- }
50- }
5134
52- } else {
53- var start = + new Date ( ) ;
54- output = ( new Purifier ( ) ) . purify ( data ) ;
55- /*for (i = 0; i < 100; i++) {
56- output = (new Purifier()).purify(data);
57- }*/
58- var end = + new Date ( ) ;
59- console . log ( output ) ;
60- //console.log("html-purify runs at a speed of " + 10/((end - start)/1000) + " MB per seconds [" + (end-start)/10/1000 + " second per MB].");
61- }
62- process . exit ( 0 ) ;
63- } else {
64- console . log ( "[ERROR] " + file + " not exist" ) ;
65- process . exit ( 1 ) ;
66- }
67- } else {
68- console . log ( "Usage: html-purify <any html file>" ) ;
69- process . exit ( 1 ) ;
35+ if ( noofargs < 3 ) {
36+ console . log ( "Usage: html-purifier <html_filepath> [--benchmark]" ) ;
37+ process . exit ( 1 ) ;
38+ }
39+
40+ if ( ! fs . existsSync ( file ) ) {
41+ console . log ( "[ERROR] " + file + " not exist" ) ;
42+ process . exit ( 1 ) ;
43+ }
44+
45+ var data = fs . readFileSync ( file , 'utf-8' ) , i , output = '' ;
46+
47+ // The following is disabled as it might generate insecure html,
48+ // as it could violate the assumption that purify() must start with the data state
49+ // if (lineByLine) {
50+ // // reading and processing line by line
51+ // var data2 = data.split(/\n/);
52+ // for (i = 0; i < data2.length; i++) {
53+ // if (data2[i].length !== 0) {
54+ // output = (new Purifier()).purify(data2[i]);
55+ // console.log("*****");
56+ // console.log("input ==> " + data2[i]);
57+ // console.log("output ==> " + output);
58+ // }
59+ // }
60+ // process.exit(0);
61+ // }
62+
63+ if ( ! benchmark ) {
64+ console . log ( ( new Purifier ( ) ) . purify ( data ) ) ;
65+ process . exit ( 0 ) ;
66+ }
67+ else if ( benchmark ) {
68+
69+ var suite = new Benchmark . Suite ;
70+ var purifier1a = new Purifier ( ) ;
71+ var purifier1b = new Purifier ( { enableTagBalancing :false } ) ;
72+
73+ suite . add ( 'default' , function ( ) {
74+ purifier1a . purify ( data ) ;
75+ } )
76+ . add ( 'disabled tag balancing' , function ( ) {
77+ purifier1b . purify ( data ) ;
78+ } )
79+ // add listeners
80+ . on ( 'cycle' , function ( event ) {
81+ console . log ( String ( event . target ) ) ;
82+ } )
83+ . on ( 'complete' , function ( ) {
84+ console . log ( 'Fastest is ' , this . filter ( 'fastest' ) . pluck ( 'name' ) ) ;
85+
86+ var t = this . filter ( 'fastest' ) [ 0 ] . stats . mean ;
87+ console . log ( 'Speed/Time is ' , data . length / 1000000 / t + 'MB/s' , t + 's' ) ;
88+ } )
89+ // run async
90+ . run ( {
91+ // 'minSamples': 10,
92+ 'async' : true
93+ } ) ;
7094 }
7195
7296} ) . call ( this ) ;
0 commit comments