Skip to content

Commit 6319499

Browse files
committed
console: rework console.dirxml to call console.dir on each argument received.
Minimal implementation following the Living Standard specs, following reviews. Fixes: #17128
1 parent 52fefd4 commit 6319499

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

doc/api/console.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,21 @@ Defaults to `2`. To make it recurse indefinitely, pass `null`.
277277
Defaults to `false`. Colors are customizable; see
278278
[customizing `util.inspect()` colors][].
279279

280+
### console.dirxml(...data)
281+
<!-- YAML
282+
added: v8.0.0
283+
changes:
284+
- version: REPLACEME
285+
pr-url: https://github.com/nodejs/node/pull/17128
286+
description: "`console.dirxml` now calls `console.dir` for each argument."
287+
-->
288+
* `...data` {any}
289+
290+
This method calls `console.dir()` with default options for each argument it
291+
receives. See [`console.dir()`][] for more details about said defaults.
292+
Please note that this method doesn't produce any xml formatting and uses the
293+
default `console.dir()` formatting resolution instead.
294+
280295
### console.error([data][, ...args])
281296
<!-- YAML
282297
added: v0.1.100
@@ -435,18 +450,6 @@ The following methods are exposed by the V8 engine in the general API but do
435450
not display anything unless used in conjunction with the [inspector][]
436451
(`--inspect` flag).
437452

438-
### console.dirxml(object)
439-
<!-- YAML
440-
added: v8.0.0
441-
-->
442-
* `object` {string}
443-
444-
This method does not display anything unless used in the inspector. The
445-
`console.dirxml()` method displays in `stdout` an XML interactive tree
446-
representation of the descendants of the specified `object` if possible, or the
447-
JavaScript representation if not. Calling `console.dirxml()` on an HTML or XML
448-
element is equivalent to calling `console.log()`.
449-
450453
### console.markTimeline(label)
451454
<!-- YAML
452455
added: v8.0.0
@@ -521,6 +524,7 @@ added: v8.0.0
521524
This method does not display anything unless used in the inspector. The
522525
`console.timelineEnd()` method is the deprecated form of [`console.timeEnd()`][].
523526

527+
[`console.dir()`]: #console_console_dir_obj_options
524528
[`console.error()`]: #console_console_error_data_args
525529
[`console.group()`]: #console_console_group_label
526530
[`console.log()`]: #console_console_log_data_args

lib/console.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,8 @@ Console.prototype.dir = function dir(object, options) {
163163

164164

165165
Console.prototype.dirxml = function dirxml(...data) {
166-
const optionProps = ['showHidden', 'depth', 'colors'],
167-
maybeOptions = Object.getOwnPropertyNames(data.slice(-1)),
168-
isOption = maybeOptions.some((p) => optionProps.indexOf(p) !== -1);
169-
let options = { customInspect: false };
170-
171-
if (isOption) {
172-
options = Object.assign(data.splice(-1), options);
173-
}
174166
for (const item of data) {
175-
write(this._ignoreErrors,
176-
this._stdout,
177-
util.inspect(item, options),
178-
this._stdoutErrorHandler,
179-
this[kGroupIndent]);
167+
Console.prototype.dir.call(this, item);
180168
}
181169
};
182170

test/parallel/test-console.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ console.dir(custom_inspect, { showHidden: false });
103103
console.dir({ foo: { bar: { baz: true } } }, { depth: 0 });
104104
console.dir({ foo: { bar: { baz: true } } }, { depth: 1 });
105105

106+
// test console.dirxml()
107+
console.dirxml(custom_inspect, custom_inspect);
108+
console.dirxml(
109+
{ foo: { bar: { baz: true } } },
110+
{ foo: { bar: { quux: false } } },
111+
{ foo: { bar: { quux: true } } }
112+
);
113+
106114
// test console.trace()
107115
console.trace('This is a %j %d', { formatted: 'trace' }, 10, 'foo');
108116

@@ -171,6 +179,14 @@ assert.strictEqual(strings.shift(),
171179
"{ foo: 'bar', inspect: [Function: inspect] }\n");
172180
assert.ok(strings.shift().includes('foo: [Object]'));
173181
assert.strictEqual(strings.shift().includes('baz'), false);
182+
assert.strictEqual(strings.shift(),
183+
"{ foo: 'bar', inspect: [Function: inspect] }\n");
184+
assert.strictEqual(strings.shift(),
185+
"{ foo: 'bar', inspect: [Function: inspect] }\n");
186+
assert.strictEqual(strings.shift().includes('foo: { bar: { baz:'), true);
187+
assert.strictEqual(strings.shift().includes('quux'), true);
188+
assert.strictEqual(strings.shift().includes('quux: true'), true);
189+
174190
assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
175191
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
176192
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));

0 commit comments

Comments
 (0)