forked from readium/swift-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLHelper.swift
50 lines (40 loc) · 1.2 KB
/
URLHelper.swift
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
49
50
//
// Copyright 2024 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
class URLHelper {
/**
Check if an href destination is absolute or not.
- parameter href: The destination.
- returns: true only if href is absolute.
*/
static func isAbsolute(href: String) -> Bool {
if let url = URL(string: href) {
if url.scheme != nil, url.host != nil {
return true
}
}
return false
}
/**
Build an absolute href destination.
- parameter href: The relative destination.
- parameter base: The base URL.
- returns: The absolute href destination.
*/
static func getAbsolute(href: String?, base: URL?) -> String? {
var absolute: String?
if let href = href {
if URLHelper.isAbsolute(href: href) {
absolute = href
} else {
if let base = base {
absolute = URL(string: href, relativeTo: base)?.absoluteString
}
}
}
return absolute
}
}