@@ -9,10 +9,7 @@ var UglifyJS = require('uglify-js');
9
9
var utils = require ( './utils' ) ;
10
10
11
11
function trimWhitespace ( str ) {
12
- if ( typeof str !== 'string' ) {
13
- return str ;
14
- }
15
- return str . replace ( / ^ [ \n \r \t \f ] + / , '' ) . replace ( / [ \n \r \t \f ] + $ / , '' ) ;
12
+ return str && str . replace ( / ^ [ \n \r \t \f ] + / , '' ) . replace ( / [ \n \r \t \f ] + $ / , '' ) ;
16
13
}
17
14
18
15
function collapseWhitespaceAll ( str ) {
@@ -258,7 +255,7 @@ function isSrcset(attrName, tag) {
258
255
}
259
256
260
257
function cleanAttributeValue ( tag , attrName , attrValue , options , attrs ) {
261
- if ( attrValue && isEventAttribute ( attrName , options ) ) {
258
+ if ( isEventAttribute ( attrName , options ) ) {
262
259
attrValue = trimWhitespace ( attrValue ) . replace ( / ^ j a v a s c r i p t : \s * / i, '' ) ;
263
260
return options . minifyJS ( attrValue , true ) ;
264
261
}
@@ -314,7 +311,7 @@ function cleanAttributeValue(tag, attrName, attrValue, options, attrs) {
314
311
return ( + numString ) . toString ( ) ;
315
312
} ) ;
316
313
}
317
- else if ( attrValue && options . customAttrCollapse && options . customAttrCollapse . test ( attrName ) ) {
314
+ else if ( options . customAttrCollapse && options . customAttrCollapse . test ( attrName ) ) {
318
315
attrValue = attrValue . replace ( / \n + | \r + | \s { 2 , } / g, '' ) ;
319
316
}
320
317
else if ( tag === 'script' && attrName === 'type' ) {
@@ -522,7 +519,7 @@ function canTrimWhitespace(tag) {
522
519
}
523
520
524
521
function normalizeAttr ( attr , attrs , tag , options ) {
525
- var attrName = options . caseSensitive ? attr . name : attr . name . toLowerCase ( ) ,
522
+ var attrName = options . name ( attr . name ) ,
526
523
attrValue = attr . value ;
527
524
528
525
if ( options . decodeEntities && attrValue ) {
@@ -538,7 +535,9 @@ function normalizeAttr(attr, attrs, tag, options) {
538
535
return ;
539
536
}
540
537
541
- attrValue = cleanAttributeValue ( tag , attrName , attrValue , options , attrs ) ;
538
+ if ( attrValue ) {
539
+ attrValue = cleanAttributeValue ( tag , attrName , attrValue , options , attrs ) ;
540
+ }
542
541
543
542
if ( options . removeEmptyAttributes &&
544
543
canDeleteEmptyAttribute ( tag , attrName , attrValue , options ) ) {
@@ -615,6 +614,9 @@ function identity(value) {
615
614
616
615
function processOptions ( values ) {
617
616
var options = {
617
+ name : function ( name ) {
618
+ return name . toLowerCase ( ) ;
619
+ } ,
618
620
canCollapseWhitespace : canCollapseWhitespace ,
619
621
canTrimWhitespace : canTrimWhitespace ,
620
622
html5 : true ,
@@ -631,7 +633,12 @@ function processOptions(values) {
631
633
} ;
632
634
Object . keys ( values ) . forEach ( function ( key ) {
633
635
var value = values [ key ] ;
634
- if ( key === 'log' ) {
636
+ if ( key === 'caseSensitive' ) {
637
+ if ( value ) {
638
+ options . name = identity ;
639
+ }
640
+ }
641
+ else if ( key === 'log' ) {
635
642
if ( typeof value === 'function' ) {
636
643
options . log = value ;
637
644
}
@@ -732,7 +739,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) {
732
739
733
740
function attrNames ( attrs ) {
734
741
return attrs . map ( function ( attr ) {
735
- return options . caseSensitive ? attr . name : attr . name . toLowerCase ( ) ;
742
+ return options . name ( attr . name ) ;
736
743
} ) ;
737
744
}
738
745
@@ -756,7 +763,7 @@ function createSortFns(value, options, uidIgnore, uidAttr) {
756
763
}
757
764
for ( var i = 0 , len = attrs . length ; i < len ; i ++ ) {
758
765
var attr = attrs [ i ] ;
759
- if ( classChain && ( options . caseSensitive ? attr . name : attr . name . toLowerCase ( ) ) === 'class' ) {
766
+ if ( classChain && attr . value && options . name ( attr . name ) === 'class' ) {
760
767
classChain . add ( trimWhitespace ( attr . value ) . split ( / [ \t \n \f \r ] + / ) . filter ( shouldSkipUIDs ) ) ;
761
768
}
762
769
else if ( options . processScripts && attr . name . toLowerCase ( ) === 'type' ) {
@@ -946,16 +953,13 @@ function minify(value, options, partialMarkup) {
946
953
html5 : options . html5 ,
947
954
948
955
start : function ( tag , attrs , unary , unarySlash , autoGenerated ) {
949
- var lowerTag = tag . toLowerCase ( ) ;
950
-
951
- if ( lowerTag === 'svg' ) {
956
+ if ( tag . toLowerCase ( ) === 'svg' ) {
952
957
options = Object . create ( options ) ;
953
- options . keepClosingSlash = true ;
954
958
options . caseSensitive = true ;
959
+ options . keepClosingSlash = true ;
960
+ options . name = identity ;
955
961
}
956
-
957
- tag = options . caseSensitive ? tag : lowerTag ;
958
-
962
+ tag = options . name ( tag ) ;
959
963
currentTag = tag ;
960
964
charsPrevTag = tag ;
961
965
if ( ! inlineTextTags ( tag ) ) {
@@ -1035,11 +1039,10 @@ function minify(value, options, partialMarkup) {
1035
1039
}
1036
1040
} ,
1037
1041
end : function ( tag , attrs , autoGenerated ) {
1038
- var lowerTag = tag . toLowerCase ( ) ;
1039
- if ( lowerTag === 'svg' ) {
1042
+ if ( tag . toLowerCase ( ) === 'svg' ) {
1040
1043
options = Object . getPrototypeOf ( options ) ;
1041
1044
}
1042
- tag = options . caseSensitive ? tag : lowerTag ;
1045
+ tag = options . name ( tag ) ;
1043
1046
1044
1047
// check if current tag is in a whitespace stack
1045
1048
if ( options . collapseWhitespace ) {
0 commit comments