Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Handle all valid ST characters #58

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export default function ansiRegex({onlyFirst = false} = {}) {
// Valid string terminator sequences are BEL, ESC\, and 0x9c
const ST = '(?:\\u0007|\\u001B\\u005C|\\u009C)';
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\\/#&.:=?%@~_]*)*)?${ST})`,
'(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))',
].join('|');

Expand Down
22 changes: 12 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:[email protected]\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:[email protected]\u0007mail-me\u001B]8;;\u0007'.match(ansiRegex()), [
'\u001B]8;;mailto:[email protected]\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:[email protected]${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:[email protected]${ST}mail-me\u001B]8;;${ST}`.match(ansiRegex()), [
`\u001B]8;;mailto:[email protected]${ST}`,
`\u001B]8;;${ST}`,
]);
}
});

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