5

I am developing an application in titanium using Javascript. I need an open source implementation of encodeURIComponent in Javascript.

Can anybody guide me or show me some implementation?

Rahul
  • 2,658
  • 12
  • 28
Altaf
  • 5,150
  • 10
  • 39
  • 55
  • 2
    You can find Mozilla's implementation of `str_encodeURI_Component` in "/js/src/jsstr.cpp": [http://mxr.mozilla.org/mozilla-central/source/js/src/jsstr.cpp](http://mxr.mozilla.org/mozilla-central/source/js/src/jsstr.cpp) or [http://hg.mozilla.org/mozilla-central/file/](http://hg.mozilla.org/mozilla-central/file/). – XP1 Oct 17 '12 at 13:48
  • 1
    @XP1 Mozilla has retired MXR; that page is now at https://dxr.mozilla.org/mozilla-central/source/js/src/jsstr.cpp – Daniel Griscom Apr 26 '17 at 16:55

3 Answers3

6

The specification for this function is in 15.1.3.4.


Modern versions (2018) of V8 implement it in C++. See src/uri.h:

// ES6 section 18.2.6.5 encodeURIComponenet (uriComponent)
static MaybeHandle<String> EncodeUriComponent(Isolate* isolate,
                                              Handle<String> component) {

which calls into Encode defined in uri.cc.


Older versions of V8 implemented it in JavaScript and distributed under the BSD license. See line 359 of src/uri.js.

// ECMA-262 - 15.1.3.4
function URIEncodeComponent(component) {
  var unescapePredicate = function(cc) {
    if (isAlphaNumeric(cc)) return true;
    // !
    if (cc == 33) return true;
    // '()*
    if (39 <= cc && cc <= 42) return true;
    // -.
    if (45 <= cc && cc <= 46) return true;
    // _
    if (cc == 95) return true;
    // ~
    if (cc == 126) return true;

    return false;
  };

  var string = ToString(component);
  return Encode(string, unescapePredicate);
}

It's not called encodeURIComponent there, but this code in the same file, esablishes the mapping:

InstallFunctions(global, DONT_ENUM, $Array(
    "escape", URIEscape,
    "unescape", URIUnescape,
    "decodeURI", URIDecode,
    "decodeURIComponent", URIDecodeComponent,
    "encodeURI", URIEncode,
    "encodeURIComponent", URIEncodeComponent
  ));
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • but you are also calling Encode I just everything plain JavaScript – Altaf Mar 09 '12 at 14:57
  • Do you have an updated link to the source? I can't find the implementation myself. I'm going to port it to D (language like C++). – Jacob Birkett Aug 21 '18 at 01:07
  • @spikespaz, The code is now native. See [`uri.h`](https://chromium.googlesource.com/v8/v8/+/master/src/uri.h#34) and [`uri.cc`](https://chromium.googlesource.com/v8/v8/+/master/src/uri.cc#273) in the same directory. – Mike Samuel Aug 22 '18 at 13:46
1

Here is my implementation:

var encodeURIComponent = function( str ) {
    var hexDigits = '0123456789ABCDEF';
    var ret = '';
    for( var i=0; i<str.length; i++ ) {
        var c = str.charCodeAt(i);
        if( (c >= 48/*0*/ && c <= 57/*9*/) ||
            (c >= 97/*a*/ && c <= 122/*z*/) ||
            (c >= 65/*A*/ && c <= 90/*Z*/) ||
            c == 45/*-*/ || c == 95/*_*/ || c == 46/*.*/ || c == 33/*!*/ || c == 126/*~*/ ||
            c == 42/***/ || c == 92/*\\*/ || c == 40/*(*/ || c == 41/*)*/ ) {
                ret += str[i];
        }
        else {
            ret += '%';
            ret += hexDigits[ (c & 0xF0) >> 4 ];
            ret += hexDigits[ (c & 0x0F) ];
        }
    }
    return ret;
};
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
0

What for do you need encodeuricomponent? It is already present in JS.

Anyway, here's an example of implementation:

http://phpjs.org/functions/rawurlencode:501#comment_93984

kirilloid
  • 14,011
  • 6
  • 38
  • 52
  • Yeah it there in JS but I need open source it part of my project,by the way the you have provided is not working – Altaf Mar 09 '12 at 14:40