Skip to content
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
3 changes: 2 additions & 1 deletion server/jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"moduleFileExtensions": [
"js",
"json",
"ts"
"ts",
"tsx"
],
"rootDir": "./",
"testRegex": ".*\\.spec\\.ts$",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SourceDialog from "../LinkToolBar/Dialog/SourceDialog";
import { VisualEditorContext } from "../../VisualEditorProvider";
import { useTranslation } from "next-i18next";
import AddIcon from '@mui/icons-material/Add';
import { URL_PATTERN } from "../../hooks/useFloatingLinkState";
import { URL_PATTERN } from "../../../../utils/ValidateFloatingLink";
import { HTTP_PROTOCOL_REGEX } from "../LinkToolBar/FloatingLinkToolbar";
import { useCommands } from "@remirror/react";
import { Node } from "@remirror/pm/model";
Expand Down
30 changes: 30 additions & 0 deletions src/components/Collaborative/hooks/useFloatingLinkState.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { validateFloatingLink } from "../../../utils/ValidateFloatingLink";

describe("URL Validation Tests", () => {
const validUrls = [
"https://example.com",
"https://example.br",
"https://example.org",
"https://example.net",
"https://example.edu",
"https://example.gov",
"https://example.mil",
"https://example.co",
"https://example.info",
"https://example.io",
"https://example.biz",
"https://example.us",
"https://example.uk",
"ftp://example.com",
"http://example.co",
"https://subdomain.example.org"
];

const mockT = jest.fn().mockReturnValue("URL inválida");

validUrls.forEach((url) => {
it(`should accept the URL: ${url}`, () => {
expect(() => validateFloatingLink(url, mockT)).not.toThrow();
});
});
});
13 changes: 1 addition & 12 deletions src/components/Collaborative/hooks/useFloatingLinkState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@ import {
useCurrentSelection,
useUpdateReason,
} from "@remirror/react";
import { useTranslation } from "next-i18next";
import { VisualEditorContext } from "../VisualEditorProvider";
import useLinkShortcut from "./useLinkShortcut";
import { uniqueId } from "remirror";

export const URL_PATTERN =
/^(ftp|http|https):\/\/[^ "]+\.(br|com|org|net|edu|gov|mil|co|info|io|biz|us|uk)(\/|\?|#|$)/;
import { validateFloatingLink } from "../../../utils/ValidateFloatingLink";

function useFloatingLinkState() {
const { t } = useTranslation();
const { editorSources, setEditorSources } = useContext(VisualEditorContext);

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

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

const validateFloatingLink = useCallback(() => {
// TODO: use a library or service that maintains a comprehensive list of valid TLDs
if (!URL_PATTERN.test(href)) {
throw new Error(t("sourceForm:errorMessageValidURL"));
}
}, [href, t]);

const updateFloatingLink = useCallback(
(id) => {
const range = linkShortcut ?? undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/FormField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { RegisterOptions } from "react-hook-form";
import { EditorParser } from "../../../lib/editor-parser";
import { ReviewTaskMachineContextReviewData } from "../../../server/review-task/dto/create-review-task.dto";
import { Roles } from "../../types/enums";
import { URL_PATTERN } from "../Collaborative/hooks/useFloatingLinkState";
import { URL_PATTERN } from "../../utils/ValidateFloatingLink";

export type FormField = {
fieldName: string;
Expand Down
9 changes: 9 additions & 0 deletions src/utils/ValidateFloatingLink.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export const URL_PATTERN =
/^(ftp|http|https):\/\/[^ "]+\.[a-zA-Z]{2,}(\/|\?|#|$)/;

export const validateFloatingLink = (href?: string, t?: (key: string) => string) => {
if (!URL_PATTERN.test(href)) {
throw new Error(t("sourceForm:errorMessageValidURL"));
}
};
Loading