@@ -13,6 +13,12 @@ var inspectCustom = require('./util.inspect').custom;
1313var inspectSymbol = ( inspectCustom && isSymbol ( inspectCustom ) ) ? inspectCustom : null ;
1414
1515module . exports = function inspect_ ( obj , opts , depth , seen ) {
16+ if ( ! opts ) opts = { } ;
17+
18+ if ( has ( opts , 'quoteStyle' ) && ( opts . quoteStyle !== 'single' && opts . quoteStyle !== 'double' ) ) {
19+ throw new TypeError ( 'option "quoteStyle" must be "single" or "double"' ) ;
20+ }
21+
1622 if ( typeof obj === 'undefined' ) {
1723 return 'undefined' ;
1824 }
@@ -22,8 +28,9 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
2228 if ( typeof obj === 'boolean' ) {
2329 return obj ? 'true' : 'false' ;
2430 }
31+
2532 if ( typeof obj === 'string' ) {
26- return inspectString ( obj ) ;
33+ return inspectString ( obj , opts ) ;
2734 }
2835 if ( typeof obj === 'number' ) {
2936 if ( obj === 0 ) {
@@ -32,8 +39,6 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
3239 return String ( obj ) ;
3340 }
3441
35- if ( ! opts ) opts = { } ;
36-
3742 var maxDepth = typeof opts . depth === 'undefined' ? 5 : opts . depth ;
3843 if ( typeof depth === 'undefined' ) depth = 0 ;
3944 if ( depth >= maxDepth && maxDepth > 0 && typeof obj === 'object' ) {
@@ -65,7 +70,7 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
6570 var s = '<' + String ( obj . nodeName ) . toLowerCase ( ) ;
6671 var attrs = obj . attributes || [ ] ;
6772 for ( var i = 0 ; i < attrs . length ; i ++ ) {
68- s += ' ' + attrs [ i ] . name + '=" ' + quote ( attrs [ i ] . value ) + '"' ;
73+ s += ' ' + attrs [ i ] . name + '=' + wrapQuotes ( quote ( attrs [ i ] . value ) , 'double' , opts ) ;
6974 }
7075 s += '>' ;
7176 if ( obj . childNodes && obj . childNodes . length ) s += '...' ;
@@ -119,6 +124,11 @@ module.exports = function inspect_ (obj, opts, depth, seen) {
119124 return String ( obj ) ;
120125} ;
121126
127+ function wrapQuotes ( s , defaultStyle , opts ) {
128+ var quoteChar = ( opts . quoteStyle || defaultStyle ) === 'double' ? '"' : "'" ;
129+ return quoteChar + s + quoteChar ;
130+ }
131+
122132function quote ( s ) {
123133 return String ( s ) . replace ( / " / g, '"' ) ;
124134}
@@ -197,9 +207,9 @@ function isElement (x) {
197207 ;
198208}
199209
200- function inspectString ( str ) {
210+ function inspectString ( str , opts ) {
201211 var s = str . replace ( / ( [ ' \\ ] ) / g, '\\$1' ) . replace ( / [ \x00 - \x1f ] / g, lowbyte ) ;
202- return "'" + s + "'" ;
212+ return wrapQuotes ( s , 'single' , opts ) ;
203213}
204214
205215function lowbyte ( c ) {
0 commit comments