@@ -27,6 +27,11 @@ var path = require("path"),
2727
2828var ruleSets = null ;
2929
30+ /**
31+ * For a given directory, recursively edit all files in it that match a filename
32+ * pattern representing source code. Replace URLs in those files with rewritten
33+ * ones if possible.
34+ */
3035function processDir ( dir ) {
3136 var stream = readdirp ( {
3237 root : dir ,
@@ -39,7 +44,7 @@ function processDir(dir) {
3944 stream
4045 . on ( 'warn' , function ( err ) {
4146 console . error ( 'non-fatal error' , err ) ;
42- // optionally call stream.destroy() here in order to abort and cause 'close' to be emitted
47+ // Optionally call stream.destroy() here in order to abort and cause 'close' to be emitted
4348 } )
4449 . on ( 'error' , function ( err ) { console . error ( 'fatal error' , err ) ; } )
4550 . pipe ( es . mapSync ( function ( entry ) {
@@ -53,13 +58,15 @@ function processDir(dir) {
5358 } ) ) ;
5459}
5560
56- // Overwrite the default URI find_uri_expression with a modified one that
57- // mitigates a catastrophic backtracking issue common in CSS.
58- // The workaround was to insist that URLs start with http, since those are the
59- // only ones we want to rewrite anyhow. Note that this may still go exponential
60- // on certain inputs. http://www.regular-expressions.info/catastrophic.html
61- // Example string that blows up URI.withinString:
62- // image:url(http://img.youtube.com/vi/x7f
61+ /**
62+ * Overwrite the default URI find_uri_expression with a modified one that
63+ * mitigates a catastrophic backtracking issue common in CSS.
64+ * The workaround was to insist that URLs start with http, since those are the
65+ * only ones we want to rewrite anyhow. Note that this may still go exponential
66+ * on certain inputs. http://www.regular-expressions.info/catastrophic.html
67+ * Example string that blows up URI.withinString:
68+ * image:url(http://img.youtube.com/vi/x7f
69+ */
6370URI . find_uri_expression = / \b ( (?: h t t p : (?: \/ { 1 , 3 } | [ a - z 0 - 9 % ] ) | w w w \d { 0 , 3 } [ . ] | [ a - z 0 - 9 . \- ] + [ . ] [ a - z ] { 2 , 4 } \/ ) (?: [ ^ \s ( ) < > ] + ) + (?: \( ( [ ^ \s ( ) < > ] + | ( \( [ ^ \s ( ) < > ] + \) ) ) * \) | [ ^ \s ` ! ( ) \[ \] { } ; : ' " . , < > ? « » “ ” ‘ ’ ] ) ) / ig;
6471
6572function processFile ( filename ) {
@@ -89,6 +96,9 @@ function processFile(filename) {
8996 fs . renameSync ( filename + ".new" , filename ) ;
9097}
9198
99+ /**
100+ * Load all rulesets for rewriting.
101+ */
92102function loadRuleSets ( ) {
93103 console . log ( "Loading rules..." ) ;
94104 var fileContents = fs . readFileSync ( path . join ( __dirname , '../pkg/crx/rules/default.rulesets' ) , 'utf8' ) ;
@@ -108,8 +118,9 @@ if (process.argv.length <= 2) {
108118
109119for ( var i = 2 ; i < process . argv . length ; i ++ ) {
110120 var rewritePath = process . argv [ i ] ;
111- if ( rewritePath . indexOf ( '-' ) == 0 )
121+ if ( rewritePath . indexOf ( '-' ) == 0 ) {
112122 usage ( ) ;
123+ }
113124 if ( ! fs . existsSync ( rewritePath ) ) {
114125 console . log ( "Path doesn't exist: " + rewritePath ) ;
115126 process . exit ( 1 ) ;
0 commit comments