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

Partial fix for URLs with localhost:port #148

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
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: 2 additions & 2 deletions src/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
Expand Down Expand Up @@ -107,4 +107,4 @@ for (let i = 0; i < testers.length; i++) {
console.log(JSON.stringify(iidxes));
*/

export { iidxes };
export { iidxes };
59 changes: 58 additions & 1 deletion test/b_integration/issues.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference types="mocha" />
import anchorme from "../../dist/node/index";
import * as expect from "expect";
describe("Issues", () => {
Expand Down Expand Up @@ -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", () => {
Expand All @@ -68,4 +73,56 @@ describe("Issues", () => {
`What's the best way to clean your smartphone? 📱🚿<a href="https://t.co/cxjsA6j60J">https://t.co/cxjsA6j60J</a>`
);
});

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 = `
<a href="http://localhost:80"></a>
<a href="http://local:6666/"></a>
<a href="https://localhost:443/a/b?c=d"></a>

<a href='http://localhost:80'></a>
<a href='http://local:6666/'></a>
<a href='https://localhost:443/a/b?c=d'></a>

<a href=http://localhost:80></a>
<a href=http://local:6666/></a>
<a href=https://localhost:443/a/b?c=d></a>
`;

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',
]);
});
});
});
});