@@ -812,7 +812,14 @@ End Function
812812
813813''
814814' Encode string for URLs
815- ' Reference: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
815+ ' Reference:
816+ ' - http://www.blooberry.com/indexdot/html/topics/urlencoding.htm
817+ ' - https://www.ietf.org/rfc/rfc1738.txt
818+ '
819+ ' From RFC 1738:
820+ ' > Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
821+ ' reserved characters used for their reserved purposes may be used
822+ ' unencoded within a URL.
816823'
817824' @method UrlEncode
818825' @param {Variant} Text Text to encode
@@ -850,19 +857,34 @@ Public Function UrlEncode(Text As Variant, Optional SpaceAsPlus As Boolean = Fal
850857 web_CharCode = VBA.Asc(web_Char)
851858
852859 Select Case web_CharCode
853- Case 97 To 122 , 65 To 90 , 48 To 57 , 45 , 46 , 95 , 126
860+ Case 33 , 36 , 39 , 40 , 41 , 42 , 44 , 45 , 46 , 48 To 57 , 65 To 90 , 95 , 97 To 122
861+ ' Unencoded:
862+ ' alphanumeric - 48-57, 65-90, 97-122
863+ ' $-_.!*'(), - 33, 36, 39, 40, 41, 42, 43, 44, 45, 46, 95
854864 web_Result(web_i) = web_Char
855- Case 32
856- web_Result(web_i) = web_Space
857- Case 0 To 15
858- web_Result(web_i) = "%0" & VBA.Hex(web_CharCode)
859865 Case 34 , 35 , 37 , 60 , 62 , 91 To 94 , 96 , 123 To 126
860- ' Unsafe characters
866+ ' Unsafe characters: <>"#%{}|\^~[]`
867+ If EncodeUnsafe Then
868+ web_Result(web_i) = "%" & VBA.Hex(web_CharCode)
869+ Else
870+ web_Result(web_i) = web_Char
871+ End If
872+ Case 32
861873 If EncodeUnsafe Then
874+ web_Result(web_i) = web_Space
875+ Else
876+ web_Result(web_i) = web_Char
877+ End If
878+ Case 43
879+ ' + is considered safe special character
880+ ' but in space-as-plus cases, it's encoded to differentiate with space
881+ If EncodeUnsafe And SpaceAsPlus Then
862882 web_Result(web_i) = "%" & VBA.Hex(web_CharCode)
863883 Else
864884 web_Result(web_i) = web_Char
865885 End If
886+ Case 0 To 15
887+ web_Result(web_i) = "%0" & VBA.Hex(web_CharCode)
866888 Case Else
867889 web_Result(web_i) = "%" & VBA.Hex(web_CharCode)
868890 End Select
0 commit comments