Skip to content

Commit 5f26943

Browse files
authored
Merge pull request #1799 from AletheiaFact/Improving-url-validation-regex
2 parents 7655f84 + 6e781c8 commit 5f26943

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

server/jest.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"moduleFileExtensions": [
44
"js",
55
"json",
6-
"ts"
6+
"ts",
7+
"tsx"
78
],
89
"rootDir": "./",
910
"testRegex": ".*\\.spec\\.ts$",

src/components/Collaborative/Components/Source/EditorAddSources.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import SourceDialog from "../LinkToolBar/Dialog/SourceDialog";
55
import { VisualEditorContext } from "../../VisualEditorProvider";
66
import { useTranslation } from "next-i18next";
77
import AddIcon from '@mui/icons-material/Add';
8-
import { URL_PATTERN } from "../../hooks/useFloatingLinkState";
8+
import { URL_PATTERN } from "../../../../utils/ValidateFloatingLink";
99
import { HTTP_PROTOCOL_REGEX } from "../LinkToolBar/FloatingLinkToolbar";
1010
import { useCommands } from "@remirror/react";
1111
import { Node } from "@remirror/pm/model";
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { validateFloatingLink } from "../../../utils/ValidateFloatingLink";
2+
3+
describe("URL Validation Tests", () => {
4+
const validUrls = [
5+
"https://example.com",
6+
"https://example.br",
7+
"https://example.org",
8+
"https://example.net",
9+
"https://example.edu",
10+
"https://example.gov",
11+
"https://example.mil",
12+
"https://example.co",
13+
"https://example.info",
14+
"https://example.io",
15+
"https://example.biz",
16+
"https://example.us",
17+
"https://example.uk",
18+
"ftp://example.com",
19+
"http://example.co",
20+
"https://subdomain.example.org"
21+
];
22+
23+
const mockT = jest.fn().mockReturnValue("URL inválida");
24+
25+
validUrls.forEach((url) => {
26+
it(`should accept the URL: ${url}`, () => {
27+
expect(() => validateFloatingLink(url, mockT)).not.toThrow();
28+
});
29+
});
30+
});

src/components/Collaborative/hooks/useFloatingLinkState.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,12 @@ import {
1414
useCurrentSelection,
1515
useUpdateReason,
1616
} from "@remirror/react";
17-
import { useTranslation } from "next-i18next";
1817
import { VisualEditorContext } from "../VisualEditorProvider";
1918
import useLinkShortcut from "./useLinkShortcut";
2019
import { uniqueId } from "remirror";
21-
22-
export const URL_PATTERN =
23-
/^(ftp|http|https):\/\/[^ "]+\.(br|com|org|net|edu|gov|mil|co|info|io|biz|us|uk)(\/|\?|#|$)/;
20+
import { validateFloatingLink } from "../../../utils/ValidateFloatingLink";
2421

2522
function useFloatingLinkState() {
26-
const { t } = useTranslation();
2723
const { editorSources, setEditorSources } = useContext(VisualEditorContext);
2824

2925
const [error, setError] = useState(null);
@@ -54,13 +50,6 @@ function useFloatingLinkState() {
5450

5551
useEffect(() => setHref(url), [url]);
5652

57-
const validateFloatingLink = useCallback(() => {
58-
// TODO: use a library or service that maintains a comprehensive list of valid TLDs
59-
if (!URL_PATTERN.test(href)) {
60-
throw new Error(t("sourceForm:errorMessageValidURL"));
61-
}
62-
}, [href, t]);
63-
6453
const updateFloatingLink = useCallback(
6554
(id) => {
6655
const range = linkShortcut ?? undefined;

src/components/Form/FormField.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RegisterOptions } from "react-hook-form";
33
import { EditorParser } from "../../../lib/editor-parser";
44
import { ReviewTaskMachineContextReviewData } from "../../../server/review-task/dto/create-review-task.dto";
55
import { Roles } from "../../types/enums";
6-
import { URL_PATTERN } from "../Collaborative/hooks/useFloatingLinkState";
6+
import { URL_PATTERN } from "../../utils/ValidateFloatingLink";
77

88
export type FormField = {
99
fieldName: string;

src/utils/ValidateFloatingLink.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
export const URL_PATTERN =
3+
/^(ftp|http|https):\/\/[^ "]+\.[a-zA-Z]{2,}(\/|\?|#|$)/;
4+
5+
export const validateFloatingLink = (href?: string, t?: (key: string) => string) => {
6+
if (!URL_PATTERN.test(href)) {
7+
throw new Error(t("sourceForm:errorMessageValidURL"));
8+
}
9+
};

0 commit comments

Comments
 (0)