uritemplate-js icon indicating copy to clipboard operation
uritemplate-js copied to clipboard

Percent encoding needs 0-padding if less than 2 digits

Open pfrazee opened this issue 12 years ago • 3 comments

Discovered on Chrome 26.0.1410.65, OSX 10.8.

Some character codes (eg "\n") produce hex values which are less than "F" (eg "%A") which creates an error when decoded by the browser (it expects "%0A").

To reproduce this issue:

decodeURIComponent(UriTemplate.parse('http://whatever.com{?q}').expand({q:"foo\nbar"}));

A simple fix:

    function pad0(v) {
      if (v.length > 1) return v;
        return '0'+v;
    }

    /**
     * encodes a character, if needed or not.
     * @param chr
     * @return pct-encoded character
     */
    function encodeCharacter (chr) {
        var
            result = '',
            octets = utf8.encode(chr),
            octet,
            index;
        for (index = 0; index < octets.length; index += 1) {
            octet = octets.charCodeAt(index);
            result += '%' + pad0(octet.toString(16).toUpperCase());
        }
        return result;
    }

pfrazee avatar May 21 '13 20:05 pfrazee

of course you are right. I will add a test case and your fix sorry, as a freelancer I normally can only fix things on weekend

fxa avatar May 26 '13 15:05 fxa

No problem! This has been a very useful library for me - I appreciate it.

pfrazee avatar May 26 '13 15:05 pfrazee

Thank you!

I just fixed your issue.

And yes, the “extract” function will come.

I have a 0.0.1 version and will check in, but the bug fixes have higher priority

Franz

Von: Paul Frazee [mailto:[email protected]] Gesendet: Sonntag, 26. Mai 2013 17:40 An: fxa/uritemplate-js Cc: Franz X Antesberger Betreff: Re: [uritemplate-js] Percent encoding needs 0-padding if less than 2 digits (#12)

No problem! This has been a very useful library for me - I appreciate it.

— Reply to this email directly or view it on GitHub https://github.com/fxa/uritemplate-js/issues/12#issuecomment-18464869 . https://github.com/notifications/beacon/z007K-oJ-ZnzAnsfdzRoGT9s4OxJfp3jOhB_xi0I6lw1MEyQReoufU5WMr73pUaN.gif

fxa avatar May 26 '13 22:05 fxa