From 2ae99de61c351846d13df0ca8c959205f318ed1f Mon Sep 17 00:00:00 2001 From: Bill Mill Date: Wed, 4 Sep 2024 11:58:25 -0400 Subject: [PATCH] fix: handle all valid ST characters According to wiki, all of [0x1b5c, 0x07, 0x9C] are valid ST (string terminator) signals, so support them all. --- index.js | 2 +- test.js | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 0dfae22..94cc931 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ export default function ansiRegex({onlyFirst = false} = {}) { const pattern = [ - '[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)', + '[\\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=><~]))' ].join('|'); diff --git a/test.js b/test.js index 7cfb248..07b77f4 100644 --- a/test.js +++ b/test.js @@ -43,16 +43,18 @@ test('match only first', t => { }); test('match terminal link', t => { - t.regex('\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007click\u001B]8;;\u0007', ansiRegex()); - t.regex('\u001B]8;;mailto:no-replay@mail.com\u0007mail\u001B]8;;\u0007', ansiRegex()); - t.deepEqual('\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007click\u001B]8;;\u0007'.match(ansiRegex()), [ - '\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007', - '\u001B]8;;\u0007' - ]); - t.deepEqual('\u001B]8;;mailto:no-reply@mail.com\u0007mail-me\u001B]8;;\u0007'.match(ansiRegex()), [ - '\u001B]8;;mailto:no-reply@mail.com\u0007', - '\u001B]8;;\u0007' - ]); + for (const ST of ['\u0007', '\u001B\u005C', '\u009C']) { + t.regex(`\u000B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`, ansiRegex()); + t.regex(`\u001B]8;;mailto:no-replay@mail.com${ST}mail\u001B]8;;${ST}`, ansiRegex()); + t.deepEqual(`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`.match(ansiRegex()), [ + `\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}`, + `\u001B]8;;${ST}` + ]); + t.deepEqual(`\u001B]8;;mailto:no-reply@mail.com${ST}mail-me\u001B]8;;${ST}`.match(ansiRegex()), [ + `\u001B]8;;mailto:no-reply@mail.com${ST}`, + `\u001B]8;;${ST}` + ]); + } }); test('match "change icon name and window title" in string', t => {