javascript - Why does decodeURI decode more characters than it should? -
i reading decodeuri
(mdn, es6 spec) , caught eye:
escape sequences not have been introduced encodeuri not replaced.
so, should decode characters encodeuri
encodes.
// none of these should escaped `encodeuri`. const unescaped = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-_.!~*'();/?:@&=+$,#"; const data = [...unescaped].map(char => ({ "char": char, "encodeuri(char)": encodeuri(char), "encodepercent(char)": encodepercent(char), "decodeuri(encodepercent(char))": decodeuri(encodepercent(char)) })); console.table( data ); console.log( "check browser's console." ); function encodepercent(string) { return string.replace(/./g, char => "%" + char.charcodeat(0).tostring(16)); }
why true ; / ? : @ & = + $ , #
?
the specification states following step:
- let unescapeduriset string containing 1 instance of each code unit valid in urireserved , uriunescaped plus "#"
let’s take @ urireserved, , voilĂ :
urireserved ::: 1 of
; / ? : @ & = + $ ,
the following step then:
- return encode(uristring, unescapeduriset).
encode in encodes string except characters in unescapeduriset, include ; / ? : @ & = + $ ,
.
this means encodeuri
can never introduce escape sequences in urireserved , uriunescaped.
interestingly enough, decodeuri
defined this:
let reserveduriset string containing 1 instance of each code unit valid in urireserved plus "#".
return decode(uristring, reserveduriset).
decode works encode , decodes except characters in reserveduriset. obviously, characters of urireserved excluded being decoded. , happen ; / ? : @ & = + $ ,
!
the questions remains why standard specifies this. if had included uriunescaped in reserveduriset behaviour introduction states. mistake?
Comments
Post a Comment