Fix #3278 - Handle UTF-8 in URI encoding/decoding

This commit is contained in:
w0rp 2020-08-10 02:03:41 +01:00
parent bf3c3e9438
commit 05210846e4
No known key found for this signature in database
GPG key ID: 0FC1ECAA8C81CD83
2 changed files with 21 additions and 3 deletions

View file

@ -1,9 +1,18 @@
" This probably doesn't handle Unicode characters well. function! s:EncodeChar(char) abort
let l:result = ''
for l:index in range(strlen(a:char))
let l:result .= printf('%%%02x', char2nr(a:char[l:index]))
endfor
return l:result
endfunction
function! ale#uri#Encode(value) abort function! ale#uri#Encode(value) abort
return substitute( return substitute(
\ a:value, \ a:value,
\ '\([^a-zA-Z0-9\\/$\-_.!*''(),]\)', \ '\([^a-zA-Z0-9\\/$\-_.!*''(),]\)',
\ '\=printf(''%%%02x'', char2nr(submatch(1)))', \ '\=s:EncodeChar(submatch(1))',
\ 'g' \ 'g'
\) \)
endfunction endfunction
@ -12,7 +21,7 @@ function! ale#uri#Decode(value) abort
return substitute( return substitute(
\ a:value, \ a:value,
\ '%\(\x\x\)', \ '%\(\x\x\)',
\ '\=nr2char(''0x'' . submatch(1))', \ '\=printf("%c", str2nr(submatch(1), 16))',
\ 'g' \ 'g'
\) \)
endfunction endfunction

View file

@ -1,3 +1,6 @@
Before:
scriptencoding utf-8
Execute(ale#path#ToURI should work for Windows paths): Execute(ale#path#ToURI should work for Windows paths):
AssertEqual 'file:///C:/foo/bar/baz.tst', ale#path#ToURI('C:\foo\bar\baz.tst') AssertEqual 'file:///C:/foo/bar/baz.tst', ale#path#ToURI('C:\foo\bar\baz.tst')
AssertEqual 'foo/bar/baz.tst', ale#path#ToURI('foo\bar\baz.tst') AssertEqual 'foo/bar/baz.tst', ale#path#ToURI('foo\bar\baz.tst')
@ -62,3 +65,9 @@ Execute(ale#path#ToURI should percent encode unsafe characters):
Execute(ale#path#FromURI should decode percent encodings): Execute(ale#path#FromURI should decode percent encodings):
AssertEqual ' +:?&=', ale#path#FromURI('%20%2b%3a%3f%26%3d') AssertEqual ' +:?&=', ale#path#FromURI('%20%2b%3a%3f%26%3d')
Execute(ale#path#ToURI should handle UTF-8):
AssertEqual 'file:///T%c3%a9l%c3%a9chargement', ale#path#ToURI('/Téléchargement')
Execute(ale#path#FromURI should handle UTF-8):
AssertEqual '/Téléchargement', ale#path#FromURI('file:///T%C3%A9l%C3%A9chargement')