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

lookbehind not supported on safari < 17 #143

Open
rhysburnie opened this issue Jan 17, 2024 · 3 comments · May be fixed by #146
Open

lookbehind not supported on safari < 17 #143

rhysburnie opened this issue Jan 17, 2024 · 3 comments · May be fixed by #146

Comments

@rhysburnie
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/anchorme/dist/node/regex.js b/node_modules/anchorme/dist/node/regex.js
index 817f906..573a757 100644
--- a/node_modules/anchorme/dist/node/regex.js
+++ b/node_modules/anchorme/dist/node/regex.js
@@ -15,7 +15,14 @@ var fqdn = "(((".concat(protocol, ")?(").concat(domain, "|").concat(ipv4, ")(?=\
 exports.email = "\\b(mailto:)?".concat(emailAddress, "@(").concat(domain, "|").concat(ipv4, ")");
 exports.url = "(".concat(fqdn, ")").concat(path, "?");
 exports.file = "(file:\\/\\/\\/)(?:[a-z]+:(?:\\/|\\\\)+)?([\\w.]+(?:[\\/\\\\]?)+)+";
-exports.final = "(?<=\\b|_)((".concat(exports.email, ")|(").concat(exports.file, ")|(").concat(exports.url, "))(\\b)?");
+
+try {
+  new RegExp('(?<=\\b|_)'); // safari < 17 doesnt like this
+  exports.final = "(?<=\\b|_)((".concat(exports.email, ")|(").concat(exports.file, ")|(").concat(exports.url, "))(\\b)?");
+} catch(err) {
+  exports.final = "(?:\\b|_)((".concat(exports.email, ")|(").concat(exports.file, ")|(").concat(exports.url, "))(\\b)?");
+}
+
 exports.finalRegex = new RegExp(exports.final, "gi");
 // for validation purposes
 exports.ipRegex = new RegExp("^(".concat(ipv4, "|").concat(ipv6, ")$"), "i");

This issue body was partially generated by patch-package.

@rhysburnie
Copy link
Author

I'm no regExp expert I was just going by this for rough equiv of look behind https://stackoverflow.com/a/74994273/738957
This was crashing our entire app, if this fix has a side effect on some anchorme links its preferable over an entire crash.
However if there is a better fix would be good to know.

@alexcorvi
Copy link
Owner

Please check the latest version 3.0.7

@lionel-rowe
Copy link

Current fix gives wrong capture group results, as there are more capture groups in the lookaround-supporting regex than there are in the compat-mode regex:

const OriginalRegExp = globalThis.RegExp;

class SafariRegExp extends OriginalRegExp {
    constructor(pattern, flags) {
        super(pattern, flags)
        if (String(pattern).includes('(?<=')) {
            throw new SyntaxError('idk what (?<= even means')
        }
    }
}

globalThis.RegExp = SafariRegExp

const anchorme = (await import('https://esm.sh/v135/[email protected]')).default
console.info(
    anchorme.list(
        `https://example.xyz example.com [email protected] file:https:///filename.txt 192.168.1.1`,
        false,
    )
)

// expected:
[
	{ start: 0, end: 19, string: 'https://example.xyz', isURL: true, protocol: 'https://', /* ... */ },
	{ start: 20, end: 31, string: 'example.com', isURL: true, protocol: undefined, /* ... */ },
	{ start: 32, end: 46, string: '[email protected]', isEmail: true, local: 'user', /* ... */ },
	{ start: 47, end: 67, string: 'file:https:///filename.txt', isFile: true, protocol: 'file:https:///', /* ... */ },
	{ start: 68, end: 79, string: '192.168.1.1', isURL: true, protocol: undefined, /* ... */ },	
]
// actual:
[
	{ start: 0, end: 19, string: 'https://example.xyz', reason: 'unknown' },
	{ start: 20, end: 31, string: 'example.com', reason: 'unknown' },
	{ start: 32, end: 46, string: '[email protected]', reason: 'unknown' },
	{ start: 47, end: 67, string: 'file:https:///filename.txt', isURL: true, protocol: undefined, /* ... */ },
	{ start: 68, end: 79, string: '192.168.1.1', reason: 'unknown' },	
]

I'll have a PR up with a fix shortly — I think with the fix, reason: "unknown" can also be removed entirely as it should never occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants