@@ -21,26 +21,25 @@ extension Href {
2121 public init ( _ url: URL ) {
2222 self = . init( url. absoluteString)
2323 }
24-
24+
2525 public static func url( _ url: URL ) -> Href {
2626 . init( url)
2727 }
28-
28+
2929 /// Creates a link with a fragment identifier (#section)
3030 public static func fragment( _ base: String , fragment: String ) -> Href {
3131 let baseWithoutFragment = base. split ( separator: " # " ) [ 0 ]
3232 let fragmentWithoutHash = fragment. hasPrefix ( " # " ) ? String ( fragment. dropFirst ( ) ) : fragment
3333 return Href ( " \( baseWithoutFragment) # \( fragmentWithoutHash) " )
3434 }
35-
35+
3636 /// Creates a link to a specific fragment on the current page
3737 public static func anchor( _ fragmentId: String ) -> Href {
3838 let fragmentWithoutHash = fragmentId. hasPrefix ( " # " ) ? fragmentId : " # \( fragmentId) "
3939 return Href ( fragmentWithoutHash)
4040 }
4141}
4242
43-
4443#if canImport(Foundation)
4544import Foundation
4645
@@ -50,46 +49,46 @@ extension Href {
5049 // Define a custom allowed character set that excludes ?, &, =, and other special chars
5150 var allowedCharacters = CharacterSet . urlQueryAllowed
5251 allowedCharacters. remove ( charactersIn: " ?&=+% " )
53-
52+
5453 var url = " mailto: \( address) "
55-
54+
5655 if subject != nil || body != nil {
5756 url += " ? "
5857 var queryParts : [ String ] = [ ]
59-
58+
6059 if let subject = subject {
6160 // Properly encode the subject
6261 let encodedSubject = subject. addingPercentEncoding ( withAllowedCharacters: allowedCharacters) ?? subject
6362 queryParts. append ( " subject= \( encodedSubject) " )
6463 }
65-
64+
6665 if let body = body {
6766 // Properly encode the body
6867 let encodedBody = body. addingPercentEncoding ( withAllowedCharacters: allowedCharacters) ?? body
6968 queryParts. append ( " body= \( encodedBody) " )
7069 }
71-
70+
7271 url += queryParts. joined ( separator: " & " )
7372 }
74-
73+
7574 return Href ( url)
7675 }
77-
76+
7877 /// Creates a telephone link (tel:) - Foundation only
7978 public static func tel( _ phoneNumber: String ) -> Href {
8079 let formattedNumber = phoneNumber. replacingOccurrences ( of: #"[^\d+]"# , with: " " , options: . regularExpression)
8180 return Href ( " tel: \( formattedNumber) " )
8281 }
83-
82+
8483 /// Creates an SMS link (sms:) - Foundation only
8584 public static func sms( _ phoneNumber: String , body: String ? = nil ) -> Href {
8685 let formattedNumber = phoneNumber. replacingOccurrences ( of: #"[^\d+]"# , with: " " , options: . regularExpression)
87-
86+
8887 if let body = body {
8988 // Define a custom allowed character set that excludes ?, &, =, and other special chars
9089 var allowedCharacters = CharacterSet . urlQueryAllowed
9190 allowedCharacters. remove ( charactersIn: " ?&=+% " )
92-
91+
9392 // Properly encode the body
9493 let encodedBody = body. addingPercentEncoding ( withAllowedCharacters: allowedCharacters) ?? body
9594 return Href ( " sms: \( formattedNumber) ?body= \( encodedBody) " )
0 commit comments