Skip to content

Commit 9cba40d

Browse files
authoredSep 9, 2024··
Fix: Handle all valid ST characters (#58)
1 parent b630317 commit 9cba40d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed
 

‎index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
export default function ansiRegex({onlyFirst = false} = {}) {
2+
// Valid string terminator sequences are BEL, ESC\, and 0x9c
3+
const ST = '(?:\\u0007|\\u001B\\u005C|\\u009C)';
24
const pattern = [
3-
'[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)',
5+
`[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?${ST})`,
46
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
57
].join('|');
68

‎test.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ test('match only first', t => {
4343
});
4444

4545
test('match terminal link', t => {
46-
t.regex('\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007click\u001B]8;;\u0007', ansiRegex());
47-
t.regex('\u001B]8;;mailto:no-replay@mail.com\u0007mail\u001B]8;;\u0007', ansiRegex());
48-
t.deepEqual('\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007click\u001B]8;;\u0007'.match(ansiRegex()), [
49-
'\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le\u0007',
50-
'\u001B]8;;\u0007',
51-
]);
52-
t.deepEqual('\u001B]8;;mailto:no-reply@mail.com\u0007mail-me\u001B]8;;\u0007'.match(ansiRegex()), [
53-
'\u001B]8;;mailto:no-reply@mail.com\u0007',
54-
'\u001B]8;;\u0007',
55-
]);
46+
for (const ST of ['\u0007', '\u001B\u005C', '\u009C']) {
47+
t.regex(`\u000B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`, ansiRegex());
48+
t.regex(`\u001B]8;;mailto:no-replay@mail.com${ST}mail\u001B]8;;${ST}`, ansiRegex());
49+
t.deepEqual(`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}click\u001B]8;;${ST}`.match(ansiRegex()), [
50+
`\u001B]8;k=v;https://example-a.com/?a_b=1&c=2#tit%20le${ST}`,
51+
`\u001B]8;;${ST}`,
52+
]);
53+
t.deepEqual(`\u001B]8;;mailto:no-reply@mail.com${ST}mail-me\u001B]8;;${ST}`.match(ansiRegex()), [
54+
`\u001B]8;;mailto:no-reply@mail.com${ST}`,
55+
`\u001B]8;;${ST}`,
56+
]);
57+
}
5658
});
5759

5860
test('match "change icon name and window title" in string', t => {

0 commit comments

Comments
 (0)
Please sign in to comment.