-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
HTMLAnchorElementUtility.ts
48 lines (43 loc) · 1.19 KB
/
HTMLAnchorElementUtility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import IDocument from '../document/IDocument';
import { URL } from 'url';
/**
* HTML Anchor Element utility.
*/
export default class HTMLAnchorElementUtility {
/**
* Returns "true" if it is a blob URL.
*
* According to spec, if element's url is non-null, its scheme is "blob", and it has an opaque path, then the process of updating properties on the URL should be terminated.
*
* @see https://html.spec.whatwg.org/multipage/links.html#reinitialise-url
* @param url
* @param url URL.
* @returns "true" if blob URL.
*/
public static isBlobURL(url: URL): boolean {
return (
url && url.protocol === 'blob:' && url.pathname.length > 1 && url.pathname.includes('://')
);
}
/**
* Returns URL.
*
* @see https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href
* @see https://html.spec.whatwg.org/multipage/links.html#hyperlink
* @param document Document.
* @param href Href.
* @returns URL.
*/
public static getUrl(document: IDocument, href: string | null): URL {
if (!href) {
return null;
}
const documentUrl = document.location.href;
try {
return new URL(href.trim(), documentUrl);
} catch (TypeError) {
// Ignore error
}
return null;
}
}