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

Change url validation in links #152

Merged
merged 1 commit into from
May 24, 2022
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
26 changes: 26 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
roots: [
'<rootDir>',
],
collectCoverageFrom: [
'**/*.{js,ts,jsx,tsx}',
'!**/node_modules/**',
],
transform: {
'^.+\\.(js|ts|jsx|tsx)?$': 'babel-jest',
},
moduleNameMapper: {
'd3-shape': '<rootDir>/node_modules/d3-shape/dist/d3-shape.js',
},
transformIgnorePatterns: [
'.*/node_modules/(?!d3|d3-shape)',
],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(ts|js|tsx|jsx)?$',
moduleFileExtensions: [
'ts',
'js',
'tsx',
'jsx',
'json',
],
};
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"build": "rollup -c && tsc --project tsconfig-typings.json",
"prepack": "yarn build",
"watch": "rollup -c -w",
"test": "jest",
"test:coverage": "jest --coverage",
"typecheck": "tsc",
"lint": "eslint 'src/**/*.[tj]s?(x)'",
"lint:stories": "eslint 'storybook/stories/**/*.[tj]s?(x)'",
Expand All @@ -41,9 +43,12 @@
"dependencies": {
"@tanem/svg-injector": "^10.1.14",
"@togglecorp/fujs": "^1.9.3",
"@types/jest": "^27.5.1",
"@types/react-color": "^3.0.6",
"@types/requestidlecallback": "^0.3.4",
"babel-jest": "^28.1.0",
"d3-shape": "^3.0.1",
"jest": "^28.1.0",
"react-color": "^2.19.3",
"react-icons": "^4.3.1",
"react-zoom-pan-pinch": "^2.1.3",
Expand Down
6 changes: 3 additions & 3 deletions src/components/ButtonLikeLink/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import { isValidUrl, _cs } from '@togglecorp/fujs';
import { _cs } from '@togglecorp/fujs';
import {
Link as RouterLink,
LinkProps as RouterLinkProps,
} from 'react-router-dom';

import { genericMemo } from '../../utils';
import { genericMemo, isValidUrl } from '../../utils';
import { useButtonFeatures } from '../Button';
import styles from './styles.css';

Expand Down Expand Up @@ -51,7 +51,7 @@ function ButtonLikeLink(props: Props) {
// FIXME: the typing is problematic
const isExternalLink = React.useMemo(
() => (
typeof to === 'string' && (isValidUrl(to) || to.startsWith('mailto:'))
typeof to === 'string' && isValidUrl(to)
),
[to],
);
Expand Down
5 changes: 2 additions & 3 deletions src/components/Link/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import {
_cs,
isValidUrl,
} from '@togglecorp/fujs';
import {
Link as RouterLink,
Expand All @@ -11,7 +10,7 @@ import { IoChevronForward } from 'react-icons/io5';

import {
genericMemo,
isLocalUrl,
isValidUrl,
} from '../../utils';
import { SpacingTypes } from '../../types';
import Actions from '../Actions';
Expand Down Expand Up @@ -55,7 +54,7 @@ function Link(props: Props) {

const isExternalLink = React.useMemo(
() => (
typeof to === 'string' && (isValidUrl(to) || to.startsWith('mailto:') || isLocalUrl(to))
typeof to === 'string' && isValidUrl(to)
),
[to],
);
Expand Down
14 changes: 11 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ export function dateStringToDate(value: string) {
return new Date(`${value}T00:00`);
}

const localhostRegex = /(?<=\/\/)localhost(?=[:/]|$)/;
export function isLocalUrl(url: string) {
return localhostRegex.test(url);
export function isValidUrl(url?: string) {
if (!url) {
return false;
}
try {
// eslint-disable-next-line no-new
new URL(url);
return true;
} catch {
return false;
}
}
11 changes: 11 additions & 0 deletions src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {
isValidUrl,
} from './index';

test('Is valid URL', () => {
expect(isValidUrl('https://wikipedia.org')).toBe(true);
expect(isValidUrl(undefined)).toBe(false);
expect(isValidUrl('/asd/a1jkn/')).toBe(false);
expect(isValidUrl('mailto:[email protected]')).toBe(true);
expect(isValidUrl('http://localhost:3900')).toBe(true);
});
Loading