1- import type { UserSettings } from '../definitions' ;
1+ // @ts -check
22import { isIPV6 , compareIPV6 } from './ipv6' ;
33import { isThunderbird } from './platform' ;
44
5- let anchor : HTMLAnchorElement ;
5+ /** @typedef { import('../definitions').UserSettings } UserSettings */
66
7- export const parsedURLCache = new Map < string , URL > ( ) ;
7+ /** @type {HTMLAnchorElement } */
8+ let anchor ;
89
9- function fixBaseURL ( $url : string ) {
10+ /** @type {Map<string, URL> } */
11+ export const parsedURLCache = new Map ( ) ;
12+
13+ function fixBaseURL ( /** @type {string } */ $url ) {
1014 if ( ! anchor ) {
1115 anchor = document . createElement ( 'a' ) ;
1216 }
1317 anchor . href = $url ;
1418 return anchor . href ;
1519}
1620
17- export function parseURL ( $url : string , $base : string = null ) {
18- const key = `${ $url } ${ $base ? `;${ $base } ` : '' } ` ;
21+ /**
22+ * @param {string } $url
23+ * @param {string } [$base]
24+ * @returns {URL }
25+ */
26+ export function parseURL ( $url , $base = null ) {
27+ const key = `${ $url } ${ $base ? `;${ $base } ` : '' } ` ;
1928 if ( parsedURLCache . has ( key ) ) {
2029 return parsedURLCache . get ( key ) ;
2130 }
@@ -29,7 +38,12 @@ export function parseURL($url: string, $base: string = null) {
2938 return parsedURL ;
3039}
3140
32- export function getAbsoluteURL ( $base : string , $relative : string ) {
41+ /**
42+ * @param {string } $base
43+ * @param {string } $relative
44+ * @returns {string }
45+ */
46+ export function getAbsoluteURL ( $base , $relative ) {
3347 if ( $relative . match ( / ^ d a t a \\ ? \: / ) ) {
3448 return $relative ;
3549 }
@@ -48,7 +62,11 @@ export function getAbsoluteURL($base: string, $relative: string) {
4862// But https://duck.com/styles/ext.css would return false on https://duck.com/
4963// Visa versa https://duck.com/ext.css should return fasle on https://duck.com/search/
5064// We're checking if any relative value within ext.css could potentially not be on the same path.
51- export function isRelativeHrefOnAbsolutePath ( href : string ) : boolean {
65+ /**
66+ * @param {string } href
67+ * @returns {boolean }
68+ */
69+ export function isRelativeHrefOnAbsolutePath ( href ) {
5270 if ( href . startsWith ( 'data:' ) ) {
5371 return true ;
5472 }
@@ -68,7 +86,11 @@ export function isRelativeHrefOnAbsolutePath(href: string): boolean {
6886 return url . pathname === location . pathname ;
6987}
7088
71- export function getURLHostOrProtocol ( $url : string ) {
89+ /**
90+ * @param {string } $url
91+ * @returns {string }
92+ */
93+ export function getURLHostOrProtocol ( $url ) {
7294 const url = new URL ( $url ) ;
7395 if ( url . host ) {
7496 return url . host ;
@@ -78,16 +100,22 @@ export function getURLHostOrProtocol($url: string) {
78100 return url . protocol ;
79101}
80102
81- export function compareURLPatterns ( a : string , b : string ) {
103+ /**
104+ * @param {string } a
105+ * @param {string } b
106+ * @returns {number }
107+ */
108+ export function compareURLPatterns ( a , b ) {
82109 return a . localeCompare ( b ) ;
83110}
84111
85112/**
86113 * Determines whether URL has a match in URL template list.
87- * @param url Site URL.
88- * @paramlist List to search into.
114+ * @param {string } url Site URL.
115+ * @param {string[] } list List to search into.
116+ * @returns {boolean }
89117 */
90- export function isURLInList ( url : string , list : string [ ] ) {
118+ export function isURLInList ( url , list ) {
91119 for ( let i = 0 ; i < list . length ; i ++ ) {
92120 if ( isURLMatched ( url , list [ i ] ) ) {
93121 return true ;
@@ -98,10 +126,11 @@ export function isURLInList(url: string, list: string[]) {
98126
99127/**
100128 * Determines whether URL matches the template.
101- * @param url URL.
102- * @param urlTemplate URL template ("google.*", "youtube.com" etc).
129+ * @param {string } url URL.
130+ * @param {string } urlTemplate URL template ("google.*", "youtube.com" etc).
131+ * @returns {boolean }
103132 */
104- export function isURLMatched ( url : string , urlTemplate : string ) : boolean {
133+ export function isURLMatched ( url , urlTemplate ) {
105134 const isFirstIPV6 = isIPV6 ( url ) ;
106135 const isSecondIPV6 = isIPV6 ( urlTemplate ) ;
107136 if ( isFirstIPV6 && isSecondIPV6 ) {
@@ -113,7 +142,11 @@ export function isURLMatched(url: string, urlTemplate: string): boolean {
113142 return false ;
114143}
115144
116- function createUrlRegex ( urlTemplate : string ) : RegExp {
145+ /**
146+ * @param {string } urlTemplate
147+ * @returns {RegExp }
148+ */
149+ function createUrlRegex ( urlTemplate ) {
117150 urlTemplate = urlTemplate . trim ( ) ;
118151 const exactBeginning = ( urlTemplate [ 0 ] === '^' ) ;
119152 const exactEnding = ( urlTemplate [ urlTemplate . length - 1 ] === '$' ) ;
@@ -126,9 +159,12 @@ function createUrlRegex(urlTemplate: string): RegExp {
126159 . replace ( / \/ $ / , '' ) // Remove last slash
127160 ) ;
128161
129- let slashIndex : number ;
130- let beforeSlash : string ;
131- let afterSlash : string ;
162+ /** @type {number } */
163+ let slashIndex ;
164+ /** @type {string } */
165+ let beforeSlash ;
166+ /** @type {string } */
167+ let afterSlash ;
132168 if ( ( slashIndex = urlTemplate . indexOf ( '/' ) ) >= 0 ) {
133169 beforeSlash = urlTemplate . substring ( 0 , slashIndex ) ; // google.*
134170 afterSlash = urlTemplate . replace ( / \$ / g, '' ) . substring ( slashIndex ) ; // /login/abc
@@ -177,7 +213,11 @@ function createUrlRegex(urlTemplate: string): RegExp {
177213 return new RegExp ( result , 'i' ) ;
178214}
179215
180- export function isPDF ( url : string ) {
216+ /**
217+ * @param {string } url
218+ * @returns {boolean }
219+ */
220+ export function isPDF ( url ) {
181221 if ( url . includes ( '.pdf' ) ) {
182222 if ( url . includes ( '?' ) ) {
183223 url = url . substring ( 0 , url . lastIndexOf ( '?' ) ) ;
@@ -206,7 +246,13 @@ export function isPDF(url: string) {
206246 return false ;
207247}
208248
209- export function isURLEnabled ( url : string , userSettings : UserSettings , { isProtected, isInDarkList} : { isProtected : boolean ; isInDarkList : boolean } ) {
249+ /**
250+ * @param {string } url
251+ * @param {UserSettings } userSettings
252+ * @param {{isProtected: boolean; isInDarkList: boolean} } urlInfo
253+ * @returns {boolean }
254+ */
255+ export function isURLEnabled ( url , userSettings , { isProtected, isInDarkList} ) {
210256 if ( isProtected && ! userSettings . enableForProtectedPages ) {
211257 return false ;
212258 }
@@ -231,6 +277,10 @@ export function isURLEnabled(url: string, userSettings: UserSettings, {isProtect
231277 return ( ! isInDarkList && ! isURLInUserList ) ;
232278}
233279
234- export function isFullyQualifiedDomain ( candidate : string ) {
280+ /**
281+ * @param {string } candidate
282+ * @returns {boolean }
283+ */
284+ export function isFullyQualifiedDomain ( candidate ) {
235285 return / ^ [ a - z 0 - 9 . - ] + $ / . test ( candidate ) ;
236286}
0 commit comments