From 38f053cd35355e88044abbd036c1e1cf80728688 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Mon, 9 Sep 2024 11:49:18 -0400 Subject: [PATCH 1/2] util: update ansi regex --- lib/internal/util/inspect.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 674cb43ab1f01f..ab2c9278a83739 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -241,14 +241,17 @@ const meta = [ ]; // Regex used for ansi escape code splitting -// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js -// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore +// Ref: https://github.com/chalk/ansi-regex/blob/f338e1814144efb950276aac84135ff86b72dc8e/index.js +// License: MIT by Sindre Sorhus // Matches all ansi escape code sequences in a string -const ansiPattern = '[\\u001B\\u009B][[\\]()#;?]*' + - '(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*' + - '|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' + - '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))'; -const ansi = new RegExp(ansiPattern, 'g'); +const ansi = new RegExp( + '[\\u001B\\u009B][[\\]()#;?]*' + + '(?:(?:(?:(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]+)*' + + '|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/\\#&.:=?%@~_]*)*)?' + + '(?:\\u0007|\\u001B\\u005C|\\u009C))' + + '|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?' + + '[\\dA-PR-TZcf-nq-uy=><~]))', 'g', +); let getStringWidth; From 3c2491662510844c8c66a11b22e70a06df7fd4c2 Mon Sep 17 00:00:00 2001 From: RedYetiDev <38299977+RedYetiDev@users.noreply.github.com> Date: Mon, 9 Sep 2024 17:48:04 -0400 Subject: [PATCH 2/2] test: add `util.stripVTControlCharacters` test --- .../test-util-stripvtcontrolcharacters.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/parallel/test-util-stripvtcontrolcharacters.js diff --git a/test/parallel/test-util-stripvtcontrolcharacters.js b/test/parallel/test-util-stripvtcontrolcharacters.js new file mode 100644 index 00000000000000..a33d18d26dbc51 --- /dev/null +++ b/test/parallel/test-util-stripvtcontrolcharacters.js @@ -0,0 +1,28 @@ +'use strict'; + +require('../common'); +const util = require('util'); +const { test } = require('node:test'); + +// Ref: https://github.com/chalk/ansi-regex/blob/main/test.js +const tests = [ + // [before, expected] + ['\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m', 'foofoo'], // Basic ANSI + ['\u001B[0;33;49;3;9;4mbar\u001B[0m', 'bar'], // Advanced colors + ['foo\u001B[0gbar', 'foobar'], // Clear tabs + ['foo\u001B[Kbar', 'foobar'], // Clear line + ['foo\u001B[2Jbar', 'foobar'], // Clear screen +]; + +for (const ST of ['\u0007', '\u001B\u005C', '\u009C']) { + tests.push( + [`\u001B]8;;mailto:no-replay@mail.com${ST}mail\u001B]8;;${ST}`, 'mail'], + [`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`, 'click'], + ); +} + +test('util.stripVTControlCharacters', (t) => { + for (const [before, expected] of tests) { + t.assert.strictEqual(util.stripVTControlCharacters(before), expected); + } +});