diff --git a/src/regex.ts b/src/regex.ts index 17e86fe..6e76db4 100644 --- a/src/regex.ts +++ b/src/regex.ts @@ -8,7 +8,7 @@ const ipv4 = `((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(?:25[0-5]|2[0-4]\\d| const ipv6 = `\\[(?:(?:[a-f\\d:]+:+)+[a-f\\d]+)\\]`; const port = `(:(\\d{1,5}))?`; const protocol = `(ht{2}ps?:|ftps?:)\\/\\/`; -const confirmedByProtocol = `(${protocol})\\S+\\b`; +const confirmedByProtocol = `(${protocol})[^'"<>&\\s]+\\b`; const fqdn = `(((${protocol})?(${domain}|${ipv4})(?=\\b|_)${port})|(?:${confirmedByProtocol}))`; export const email = `\\b(mailto:)?${emailAddress}@(${domain}|${ipv4})`; @@ -107,4 +107,4 @@ for (let i = 0; i < testers.length; i++) { console.log(JSON.stringify(iidxes)); */ -export { iidxes }; \ No newline at end of file +export { iidxes }; diff --git a/test/b_integration/issues.test.ts b/test/b_integration/issues.test.ts index a28fd8a..c991e85 100644 --- a/test/b_integration/issues.test.ts +++ b/test/b_integration/issues.test.ts @@ -1,3 +1,4 @@ +/// import anchorme from "../../dist/node/index"; import * as expect from "expect"; describe("Issues", () => { @@ -57,7 +58,11 @@ describe("Issues", () => { const res = anchorme.list( `http://localhost localhost http://local http://machine` ); - expect(res.length).toBe(3); + expect(res.map((x) => x.string)).toStrictEqual([ + 'http://localhost', + 'http://local', + 'http://machine', + ]); }); it("Link after emoji", () => { @@ -68,4 +73,56 @@ describe("Issues", () => { `What's the best way to clean your smartphone? 📱🚿https://t.co/cxjsA6j60J` ); }); + + describe("localhost with port - https://github.com/alexcorvi/anchorme.js/issues/119", () => { + it("localhost with port", () => { + const res = anchorme.list(` + http://localhost:80 + http://local:6666/ + https://localhost:443/a/b?c=d" + `); + + expect(res.map((x) => x.string)).toStrictEqual([ + 'http://localhost:80', + 'http://local:6666/', + 'https://localhost:443/a/b?c=d', + ]); + }); + + describe("within HTML attributes", () => { + const input = ` + + + + + + + + + + + + `; + + it('no results with skipHTML=true', () => { + const res = anchorme.list(input, true); + expect(res.map((x) => x.string)).toStrictEqual([]); + }); + + it('only the attribute values with skipHTML=false', () => { + const res = anchorme.list(input, false); + expect(res.map((x) => x.string)).toStrictEqual([ + 'http://localhost:80', + 'http://local:6666/', + 'https://localhost:443/a/b?c=d', + 'http://localhost:80', + 'http://local:6666/', + 'https://localhost:443/a/b?c=d', + 'http://localhost:80', + 'http://local:6666/', + 'https://localhost:443/a/b?c=d', + ]); + }); + }); + }); });