Browse Source

util: handle escaped forward slashes correctly

Fixes #6835
v0.11.11-release
Tom Gallacher 11 years ago
committed by Fedor Indutny
parent
commit
38a07a929b
  1. 3
      lib/util.js
  2. 15
      test/simple/test-util-inspect.js

3
lib/util.js

@ -413,7 +413,8 @@ function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
} else { } else {
name = name.replace(/'/g, "\\'") name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"') .replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'"); .replace(/(^"|"$)/g, "'")
.replace(/\\\\/g, '\\');
name = ctx.stylize(name, 'string'); name = ctx.stylize(name, 'string');
} }
} }

15
test/simple/test-util-inspect.js

@ -110,6 +110,21 @@ assert.doesNotThrow(function() {
var x = { inspect: util.inspect }; var x = { inspect: util.inspect };
assert.ok(util.inspect(x).indexOf('inspect') != -1); assert.ok(util.inspect(x).indexOf('inspect') != -1);
// util.inspect should not display the escaped value of a key.
var w = {
'\\': 1,
'\\\\': 2,
'\\\\\\': 3,
'\\\\\\\\': 4,
}
var y = ['a', 'b', 'c'];
y['\\\\\\'] = 'd';
assert.ok(util.inspect(w),
'{ \'\\\': 1, \'\\\\\': 2, \'\\\\\\\': 3, \'\\\\\\\\\': 4 }');
assert.ok(util.inspect(y), '[ \'a\', \'b\', \'c\', \'\\\\\\\': \'d\' ]');
// util.inspect.styles and util.inspect.colors // util.inspect.styles and util.inspect.colors
function test_color_style(style, input, implicit) { function test_color_style(style, input, implicit) {
var color_name = util.inspect.styles[style]; var color_name = util.inspect.styles[style];

Loading…
Cancel
Save