File tree Expand file tree Collapse file tree 6 files changed +44
-15
lines changed
Expand file tree Collapse file tree 6 files changed +44
-15
lines changed Original file line number Diff line number Diff line change 33 "moduleFileExtensions" : [
44 " js" ,
55 " json" ,
6- " ts"
6+ " ts" ,
7+ " tsx"
78 ],
89 "rootDir" : " ./" ,
910 "testRegex" : " .*\\ .spec\\ .ts$" ,
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import SourceDialog from "../LinkToolBar/Dialog/SourceDialog";
55import { VisualEditorContext } from "../../VisualEditorProvider" ;
66import { useTranslation } from "next-i18next" ;
77import AddIcon from '@mui/icons-material/Add' ;
8- import { URL_PATTERN } from "../../hooks/useFloatingLinkState " ;
8+ import { URL_PATTERN } from "../../../../utils/ValidateFloatingLink " ;
99import { HTTP_PROTOCOL_REGEX } from "../LinkToolBar/FloatingLinkToolbar" ;
1010import { useCommands } from "@remirror/react" ;
1111import { Node } from "@remirror/pm/model" ;
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change @@ -14,16 +14,12 @@ import {
1414 useCurrentSelection ,
1515 useUpdateReason ,
1616} from "@remirror/react" ;
17- import { useTranslation } from "next-i18next" ;
1817import { VisualEditorContext } from "../VisualEditorProvider" ;
1918import useLinkShortcut from "./useLinkShortcut" ;
2019import { uniqueId } from "remirror" ;
21-
22- export const URL_PATTERN =
23- / ^ ( f t p | h t t p | h t t p s ) : \/ \/ [ ^ " ] + \. ( b r | c o m | o r g | n e t | e d u | g o v | m i l | c o | i n f o | i o | b i z | u s | u k ) ( \/ | \? | # | $ ) / ;
20+ import { validateFloatingLink } from "../../../utils/ValidateFloatingLink" ;
2421
2522function 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 ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { RegisterOptions } from "react-hook-form";
33import { EditorParser } from "../../../lib/editor-parser" ;
44import { ReviewTaskMachineContextReviewData } from "../../../server/review-task/dto/create-review-task.dto" ;
55import { Roles } from "../../types/enums" ;
6- import { URL_PATTERN } from "../Collaborative/hooks/useFloatingLinkState " ;
6+ import { URL_PATTERN } from "../../utils/ValidateFloatingLink " ;
77
88export type FormField = {
99 fieldName : string ;
Original file line number Diff line number Diff line change 1+
2+ export const URL_PATTERN =
3+ / ^ ( f t p | h t t p | h t t p s ) : \/ \/ [ ^ " ] + \. [ a - z A - 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+ } ;
You can’t perform that action at this time.
0 commit comments