mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
util: enforce shouldColorize in styleText array arg
PR-URL: https://github.com/nodejs/node/pull/56722 Fixes: https://github.com/nodejs/node/issues/56717 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Deokjin Kim <deokjin81.kim@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
47
lib/util.js
47
lib/util.js
@@ -119,6 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
|
||||
validateString(text, 'text');
|
||||
validateBoolean(validateStream, 'options.validateStream');
|
||||
|
||||
let skipColorize;
|
||||
if (validateStream) {
|
||||
if (
|
||||
!isReadableStream(stream) &&
|
||||
@@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
|
||||
) {
|
||||
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
|
||||
}
|
||||
|
||||
// If the stream is falsy or should not be colorized, set skipColorize to true
|
||||
skipColorize = !lazyUtilColors().shouldColorize(stream);
|
||||
}
|
||||
|
||||
if (ArrayIsArray(format)) {
|
||||
let left = '';
|
||||
let right = '';
|
||||
for (const key of format) {
|
||||
const formatCodes = inspect.colors[key];
|
||||
if (formatCodes == null) {
|
||||
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
|
||||
}
|
||||
left += escapeStyleCode(formatCodes[0]);
|
||||
right = `${escapeStyleCode(formatCodes[1])}${right}`;
|
||||
// If the format is not an array, convert it to an array
|
||||
const formatArray = ArrayIsArray(format) ? format : [format];
|
||||
|
||||
let left = '';
|
||||
let right = '';
|
||||
for (const key of formatArray) {
|
||||
const formatCodes = inspect.colors[key];
|
||||
// If the format is not a valid style, throw an error
|
||||
if (formatCodes == null) {
|
||||
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
|
||||
}
|
||||
|
||||
return `${left}${text}${right}`;
|
||||
if (skipColorize) continue;
|
||||
left += escapeStyleCode(formatCodes[0]);
|
||||
right = `${escapeStyleCode(formatCodes[1])}${right}`;
|
||||
}
|
||||
|
||||
const formatCodes = inspect.colors[format];
|
||||
if (formatCodes == null) {
|
||||
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
|
||||
}
|
||||
|
||||
// Check colorize only after validating arg type and value
|
||||
if (
|
||||
validateStream &&
|
||||
(
|
||||
!stream ||
|
||||
!lazyUtilColors().shouldColorize(stream)
|
||||
)
|
||||
) {
|
||||
return text;
|
||||
}
|
||||
|
||||
return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
|
||||
return skipColorize ? text : `${left}${text}${right}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -95,8 +95,15 @@ if (fd !== -1) {
|
||||
...process.env,
|
||||
...testCase.env
|
||||
};
|
||||
const output = util.styleText('red', 'test', { stream: writeStream });
|
||||
assert.strictEqual(output, testCase.expected);
|
||||
{
|
||||
const output = util.styleText('red', 'test', { stream: writeStream });
|
||||
assert.strictEqual(output, testCase.expected);
|
||||
}
|
||||
{
|
||||
// Check that when passing an array of styles, the output behaves the same
|
||||
const output = util.styleText(['red'], 'test', { stream: writeStream });
|
||||
assert.strictEqual(output, testCase.expected);
|
||||
}
|
||||
process.env = originalEnv;
|
||||
});
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user