Make mw.uri.encode 'WIKI' mode match core {{urlencode:}}

The core {{urlencode:}} parser function doesn't encode various
characters in WIKI mode that it does in other modes. mw.uri.encode
should match that.

Bug: T174239
Change-Id: I2be0811cf39c02c5c0ad3433e4b0ef9030350e24
This commit is contained in:
Brad Jorsch 2017-08-28 10:32:49 -04:00
parent ca81c50000
commit ca85f20099
2 changed files with 18 additions and 7 deletions

View file

@ -35,6 +35,17 @@ local function rawencode( s, space )
return ret
end
local function wikiencode( s )
local ret = string.gsub( s, '([^a-zA-Z0-9!$()*,./:;@~_-])', function ( c )
if c == ' ' then
return '_'
else
return string.format( '%%%02X', string.byte( c, 1, 1 ) )
end
end );
return ret
end
local function rawdecode( s )
local ret = string.gsub( s, '%%(%x%x)', function ( hex )
return string.char( tonumber( hex, 16 ) )
@ -51,7 +62,7 @@ function uri.encode( s, enctype )
elseif enctype == 'PATH' then
return rawencode( s, '%20' )
elseif enctype == 'WIKI' then
return rawencode( s, '_' )
return wikiencode( s )
else
error( "bad argument #2 to 'encode' (expected QUERY, PATH, or WIKI)", 2 )
end

View file

@ -18,16 +18,16 @@ local tests = {
expect = { '__foo+b%C3%A1r+%2B+baz__' }
},
{ name = 'uri.encode QUERY', func = mw.uri.encode,
args = { '__foo b\195\161r + baz__', 'QUERY' },
expect = { '__foo+b%C3%A1r+%2B+baz__' }
args = { '__foo b\195\161r + /baz/__', 'QUERY' },
expect = { '__foo+b%C3%A1r+%2B+%2Fbaz%2F__' }
},
{ name = 'uri.encode PATH', func = mw.uri.encode,
args = { '__foo b\195\161r + baz__', 'PATH' },
expect = { '__foo%20b%C3%A1r%20%2B%20baz__' }
args = { '__foo b\195\161r + /baz/__', 'PATH' },
expect = { '__foo%20b%C3%A1r%20%2B%20%2Fbaz%2F__' }
},
{ name = 'uri.encode WIKI', func = mw.uri.encode,
args = { '__foo b\195\161r + baz__', 'WIKI' },
expect = { '__foo_b%C3%A1r_%2B_baz__' }
args = { '__foo b\195\161r + /baz/__', 'WIKI' },
expect = { '__foo_b%C3%A1r_%2B_/baz/__' }
},
{ name = 'uri.decode', func = mw.uri.decode,