@@ -613,113 +613,107 @@ function identity(value) {
613613 return value ;
614614}
615615
616- function processOptions ( options ) {
617- [ 'html5' , 'includeAutoGeneratedTags' ] . forEach ( function ( key ) {
618- if ( ! ( key in options ) ) {
619- options [ key ] = true ;
620- }
621- } ) ;
622-
623- if ( typeof options . log !== 'function' ) {
624- options . log = identity ;
625- }
626-
627- if ( ! options . canCollapseWhitespace ) {
628- options . canCollapseWhitespace = canCollapseWhitespace ;
629- }
630- if ( ! options . canTrimWhitespace ) {
631- options . canTrimWhitespace = canTrimWhitespace ;
632- }
633-
634- if ( ! ( 'ignoreCustomComments' in options ) ) {
635- options . ignoreCustomComments = [ / ^ ! / ] ;
636- }
637-
638- if ( ! ( 'ignoreCustomFragments' in options ) ) {
639- options . ignoreCustomFragments = [
616+ function processOptions ( values ) {
617+ var options = {
618+ canCollapseWhitespace : canCollapseWhitespace ,
619+ canTrimWhitespace : canTrimWhitespace ,
620+ html5 : true ,
621+ ignoreCustomComments : [ / ^ ! / ] ,
622+ ignoreCustomFragments : [
640623 / < % [ \s \S ] * ?% > / ,
641624 / < \? [ \s \S ] * ?\? > /
642- ] ;
643- }
644-
645- if ( ! options . minifyURLs ) {
646- options . minifyURLs = identity ;
647- }
648- if ( typeof options . minifyURLs !== 'function' ) {
649- var minifyURLs = options . minifyURLs ;
650- if ( typeof minifyURLs === 'string' ) {
651- minifyURLs = { site : minifyURLs } ;
652- }
653- else if ( typeof minifyURLs !== 'object' ) {
654- minifyURLs = { } ;
655- }
656- options . minifyURLs = function ( text ) {
657- try {
658- return RelateUrl . relate ( text , minifyURLs ) ;
659- }
660- catch ( err ) {
661- options . log ( err ) ;
662- return text ;
625+ ] ,
626+ includeAutoGeneratedTags : true ,
627+ log : identity ,
628+ minifyCSS : identity ,
629+ minifyJS : identity ,
630+ minifyURLs : identity
631+ } ;
632+ Object . keys ( values ) . forEach ( function ( key ) {
633+ var value = values [ key ] ;
634+ if ( key === 'log' ) {
635+ if ( typeof value === 'function' ) {
636+ options . log = value ;
663637 }
664- } ;
665- }
666-
667- if ( ! options . minifyJS ) {
668- options . minifyJS = identity ;
669- }
670- if ( typeof options . minifyJS !== 'function' ) {
671- var minifyJS = options . minifyJS ;
672- if ( typeof minifyJS !== 'object' ) {
673- minifyJS = { } ;
674638 }
675- ( minifyJS . parse || ( minifyJS . parse = { } ) ) . bare_returns = false ;
676- options . minifyJS = function ( text , inline ) {
677- var start = text . match ( / ^ \s * < ! - - .* / ) ;
678- var code = start ? text . slice ( start [ 0 ] . length ) . replace ( / \n \s * - - > \s * $ / , '' ) : text ;
679- minifyJS . parse . bare_returns = inline ;
680- var result = UglifyJS . minify ( code , minifyJS ) ;
681- if ( result . error ) {
682- options . log ( result . error ) ;
683- return text ;
639+ else if ( key === 'minifyCSS' && typeof value !== 'function' ) {
640+ if ( ! value ) {
641+ return ;
684642 }
685- return result . code . replace ( / ; $ / , '' ) ;
686- } ;
687- }
688-
689- if ( ! options . minifyCSS ) {
690- options . minifyCSS = identity ;
691- }
692- if ( typeof options . minifyCSS !== 'function' ) {
693- var minifyCSS = options . minifyCSS ;
694- if ( typeof minifyCSS !== 'object' ) {
695- minifyCSS = { } ;
696- }
697- options . minifyCSS = function ( text , type ) {
698- text = text . replace ( / ( u r l \s * \( \s * ) ( " | ' | ) ( . * ? ) \2 ( \s * \) ) / ig , function ( match , prefix , quote , url , suffix ) {
699- return prefix + quote + options . minifyURLs ( url ) + quote + suffix ;
700- } ) ;
701- try {
702- if ( type === 'inline' ) {
703- text = wrapInlineCSS ( text ) ;
704- }
705- else if ( type === 'media' ) {
706- text = wrapMediaQuery ( text ) ;
643+ if ( typeof value !== 'object' ) {
644+ value = { } ;
645+ }
646+ options . minifyCSS = function ( text , type ) {
647+ text = text . replace ( / ( u r l \s * \( \s * ) ( " | ' | ) ( . * ? ) \2 ( \s * \) ) / ig , function ( match , prefix , quote , url , suffix ) {
648+ return prefix + quote + options . minifyURLs ( url ) + quote + suffix ;
649+ } ) ;
650+ try {
651+ if ( type === 'inline' ) {
652+ text = wrapInlineCSS ( text ) ;
653+ }
654+ else if ( type === 'media' ) {
655+ text = wrapMediaQuery ( text ) ;
656+ }
657+ text = new CleanCSS ( value ) . minify ( text ) . styles ;
658+ if ( type === 'inline' ) {
659+ text = unwrapInlineCSS ( text ) ;
660+ }
661+ else if ( type === 'media' ) {
662+ text = unwrapMediaQuery ( text ) ;
663+ }
664+ return text ;
707665 }
708- text = new CleanCSS ( minifyCSS ) . minify ( text ) . styles ;
709- if ( type === 'inline' ) {
710- text = unwrapInlineCSS ( text ) ;
666+ catch ( err ) {
667+ options . log ( err ) ;
668+ return text ;
711669 }
712- else if ( type === 'media' ) {
713- text = unwrapMediaQuery ( text ) ;
670+ } ;
671+ }
672+ else if ( key === 'minifyJS' && typeof value !== 'function' ) {
673+ if ( ! value ) {
674+ return ;
675+ }
676+ if ( typeof value !== 'object' ) {
677+ value = { } ;
678+ }
679+ ( value . parse || ( value . parse = { } ) ) . bare_returns = false ;
680+ options . minifyJS = function ( text , inline ) {
681+ var start = text . match ( / ^ \s * < ! - - .* / ) ;
682+ var code = start ? text . slice ( start [ 0 ] . length ) . replace ( / \n \s * - - > \s * $ / , '' ) : text ;
683+ value . parse . bare_returns = inline ;
684+ var result = UglifyJS . minify ( code , value ) ;
685+ if ( result . error ) {
686+ options . log ( result . error ) ;
687+ return text ;
714688 }
715- return text ;
689+ return result . code . replace ( / ; $ / , '' ) ;
690+ } ;
691+ }
692+ else if ( key === 'minifyURLs' && typeof value !== 'function' ) {
693+ if ( ! value ) {
694+ return ;
716695 }
717- catch ( err ) {
718- options . log ( err ) ;
719- return text ;
696+ if ( typeof value === 'string' ) {
697+ value = { site : value } ;
720698 }
721- } ;
722- }
699+ else if ( typeof value !== 'object' ) {
700+ value = { } ;
701+ }
702+ options . minifyURLs = function ( text ) {
703+ try {
704+ return RelateUrl . relate ( text , value ) ;
705+ }
706+ catch ( err ) {
707+ options . log ( err ) ;
708+ return text ;
709+ }
710+ } ;
711+ }
712+ else {
713+ options [ key ] = value ;
714+ }
715+ } ) ;
716+ return options ;
723717}
724718
725719function uniqueId ( value ) {
@@ -784,7 +778,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) {
784778 }
785779
786780 var log = options . log ;
787- options . log = null ;
781+ options . log = identity ;
788782 options . sortAttributes = false ;
789783 options . sortClassName = false ;
790784 scan ( minify ( value , options ) ) ;
@@ -817,9 +811,6 @@ function createSortFns(value, options, uidIgnore, uidAttr) {
817811}
818812
819813function minify ( value , options , partialMarkup ) {
820- options = options || { } ;
821- var optionsStack = [ ] ;
822- processOptions ( options ) ;
823814 if ( options . collapseWhitespace ) {
824815 value = collapseWhitespace ( value , options , true , true ) ;
825816 }
@@ -834,7 +825,6 @@ function minify(value, options, partialMarkup) {
834825 stackNoCollapseWhitespace = [ ] ,
835826 optionalStartTag = '' ,
836827 optionalEndTag = '' ,
837- t = Date . now ( ) ,
838828 ignoredMarkupChunks = [ ] ,
839829 ignoredCustomMarkupChunks = [ ] ,
840830 uidIgnore ,
@@ -850,22 +840,25 @@ function minify(value, options, partialMarkup) {
850840 uidIgnore = uniqueId ( value ) ;
851841 var pattern = new RegExp ( '^' + uidIgnore + '([0-9]+)$' ) ;
852842 if ( options . ignoreCustomComments ) {
853- options . ignoreCustomComments . push ( pattern ) ;
843+ options . ignoreCustomComments = options . ignoreCustomComments . slice ( ) ;
854844 }
855845 else {
856- options . ignoreCustomComments = [ pattern ] ;
846+ options . ignoreCustomComments = [ ] ;
857847 }
848+ options . ignoreCustomComments . push ( pattern ) ;
858849 }
859850 var token = '<!--' + uidIgnore + ignoredMarkupChunks . length + '-->' ;
860851 ignoredMarkupChunks . push ( group1 ) ;
861852 return token ;
862853 } ) ;
863854
864- function escapeFragments ( text ) {
865- return text . replace ( uidPattern , function ( match , prefix , index ) {
866- var chunks = ignoredCustomMarkupChunks [ + index ] ;
867- return chunks [ 1 ] + uidAttr + index + chunks [ 2 ] ;
868- } ) ;
855+ function escapeFragments ( fn ) {
856+ return function ( text , type ) {
857+ return fn ( text . replace ( uidPattern , function ( match , prefix , index ) {
858+ var chunks = ignoredCustomMarkupChunks [ + index ] ;
859+ return chunks [ 1 ] + uidAttr + index + chunks [ 2 ] ;
860+ } ) , type ) ;
861+ } ;
869862 }
870863
871864 var customFragments = options . ignoreCustomFragments . map ( function ( re ) {
@@ -878,17 +871,11 @@ function minify(value, options, partialMarkup) {
878871 if ( ! uidAttr ) {
879872 uidAttr = uniqueId ( value ) ;
880873 uidPattern = new RegExp ( '(\\s*)' + uidAttr + '([0-9]+)(\\s*)' , 'g' ) ;
881- var minifyCSS = options . minifyCSS ;
882- if ( minifyCSS ) {
883- options . minifyCSS = function ( text , type ) {
884- return minifyCSS ( escapeFragments ( text ) , type ) ;
885- } ;
874+ if ( options . minifyCSS ) {
875+ options . minifyCSS = escapeFragments ( options . minifyCSS ) ;
886876 }
887- var minifyJS = options . minifyJS ;
888- if ( minifyJS ) {
889- options . minifyJS = function ( text , inline ) {
890- return minifyJS ( escapeFragments ( text ) , inline ) ;
891- } ;
877+ if ( options . minifyJS ) {
878+ options . minifyJS = escapeFragments ( options . minifyJS ) ;
892879 }
893880 }
894881 var token = uidAttr + ignoredCustomMarkupChunks . length ;
@@ -962,14 +949,9 @@ function minify(value, options, partialMarkup) {
962949 var lowerTag = tag . toLowerCase ( ) ;
963950
964951 if ( lowerTag === 'svg' ) {
965- optionsStack . push ( options ) ;
966- var nextOptions = { } ;
967- for ( var key in options ) {
968- nextOptions [ key ] = options [ key ] ;
969- }
970- nextOptions . keepClosingSlash = true ;
971- nextOptions . caseSensitive = true ;
972- options = nextOptions ;
952+ options = Object . create ( options ) ;
953+ options . keepClosingSlash = true ;
954+ options . caseSensitive = true ;
973955 }
974956
975957 tag = options . caseSensitive ? tag : lowerTag ;
@@ -1055,7 +1037,7 @@ function minify(value, options, partialMarkup) {
10551037 end : function ( tag , attrs , autoGenerated ) {
10561038 var lowerTag = tag . toLowerCase ( ) ;
10571039 if ( lowerTag === 'svg' ) {
1058- options = optionsStack . pop ( ) ;
1040+ options = Object . getPrototypeOf ( options ) ;
10591041 }
10601042 tag = options . caseSensitive ? tag : lowerTag ;
10611043
@@ -1282,7 +1264,6 @@ function minify(value, options, partialMarkup) {
12821264 } ) ;
12831265 }
12841266
1285- options . log ( 'minified in: ' + ( Date . now ( ) - t ) + 'ms' ) ;
12861267 return str ;
12871268}
12881269
@@ -1314,5 +1295,9 @@ function joinResultSegments(results, options) {
13141295}
13151296
13161297exports . minify = function ( value , options ) {
1317- return minify ( value , options ) ;
1298+ var start = Date . now ( ) ;
1299+ options = processOptions ( options || { } ) ;
1300+ var result = minify ( value , options ) ;
1301+ options . log ( 'minified in: ' + ( Date . now ( ) - start ) + 'ms' ) ;
1302+ return result ;
13181303} ;
0 commit comments