Skip to content

Commit

Permalink
console: rework console.dirxml to call console.dir on each argument r…
Browse files Browse the repository at this point in the history
…eceived.

Minimal implementation following the Living Standard specs, following
reviews.

Fixes: #17128
  • Loading branch information
Tiriel committed Nov 24, 2017
1 parent 52fefd4 commit 6319499
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
28 changes: 16 additions & 12 deletions doc/api/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ Defaults to `2`. To make it recurse indefinitely, pass `null`.
Defaults to `false`. Colors are customizable; see
[customizing `util.inspect()` colors][].

### console.dirxml(...data)
<!-- YAML
added: v8.0.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/17128
description: "`console.dirxml` now calls `console.dir` for each argument."
-->
* `...data` {any}

This method calls `console.dir()` with default options for each argument it
receives. See [`console.dir()`][] for more details about said defaults.
Please note that this method doesn't produce any xml formatting and uses the
default `console.dir()` formatting resolution instead.

### console.error([data][, ...args])
<!-- YAML
added: v0.1.100
Expand Down Expand Up @@ -435,18 +450,6 @@ The following methods are exposed by the V8 engine in the general API but do
not display anything unless used in conjunction with the [inspector][]
(`--inspect` flag).

### console.dirxml(object)
<!-- YAML
added: v8.0.0
-->
* `object` {string}

This method does not display anything unless used in the inspector. The
`console.dirxml()` method displays in `stdout` an XML interactive tree
representation of the descendants of the specified `object` if possible, or the
JavaScript representation if not. Calling `console.dirxml()` on an HTML or XML
element is equivalent to calling `console.log()`.

### console.markTimeline(label)
<!-- YAML
added: v8.0.0
Expand Down Expand Up @@ -521,6 +524,7 @@ added: v8.0.0
This method does not display anything unless used in the inspector. The
`console.timelineEnd()` method is the deprecated form of [`console.timeEnd()`][].

[`console.dir()`]: #console_console_dir_obj_options
[`console.error()`]: #console_console_error_data_args
[`console.group()`]: #console_console_group_label
[`console.log()`]: #console_console_log_data_args
Expand Down
14 changes: 1 addition & 13 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,8 @@ Console.prototype.dir = function dir(object, options) {


Console.prototype.dirxml = function dirxml(...data) {
const optionProps = ['showHidden', 'depth', 'colors'],
maybeOptions = Object.getOwnPropertyNames(data.slice(-1)),
isOption = maybeOptions.some((p) => optionProps.indexOf(p) !== -1);
let options = { customInspect: false };

if (isOption) {
options = Object.assign(data.splice(-1), options);
}
for (const item of data) {
write(this._ignoreErrors,
this._stdout,
util.inspect(item, options),
this._stdoutErrorHandler,
this[kGroupIndent]);
Console.prototype.dir.call(this, item);
}
};

Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ console.dir(custom_inspect, { showHidden: false });
console.dir({ foo: { bar: { baz: true } } }, { depth: 0 });
console.dir({ foo: { bar: { baz: true } } }, { depth: 1 });

// test console.dirxml()
console.dirxml(custom_inspect, custom_inspect);
console.dirxml(
{ foo: { bar: { baz: true } } },
{ foo: { bar: { quux: false } } },
{ foo: { bar: { quux: true } } }
);

// test console.trace()
console.trace('This is a %j %d', { formatted: 'trace' }, 10, 'foo');

Expand Down Expand Up @@ -171,6 +179,14 @@ assert.strictEqual(strings.shift(),
"{ foo: 'bar', inspect: [Function: inspect] }\n");
assert.ok(strings.shift().includes('foo: [Object]'));
assert.strictEqual(strings.shift().includes('baz'), false);
assert.strictEqual(strings.shift(),
"{ foo: 'bar', inspect: [Function: inspect] }\n");
assert.strictEqual(strings.shift(),
"{ foo: 'bar', inspect: [Function: inspect] }\n");
assert.strictEqual(strings.shift().includes('foo: { bar: { baz:'), true);
assert.strictEqual(strings.shift().includes('quux'), true);
assert.strictEqual(strings.shift().includes('quux: true'), true);

assert.ok(/^label: \d+\.\d{3}ms$/.test(strings.shift().trim()));
assert.ok(/^__proto__: \d+\.\d{3}ms$/.test(strings.shift().trim()));
assert.ok(/^constructor: \d+\.\d{3}ms$/.test(strings.shift().trim()));
Expand Down

0 comments on commit 6319499

Please sign in to comment.