Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encodeURIComponent algorithm source code

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?

like image 362
Altaf Avatar asked Mar 09 '12 14:03

Altaf


People also ask

How do I use encodeURIComponent in JavaScript?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.

Why do we encode URI?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

How do I encode a URL in react?

Depending on what you need to do, there are 2 JavaScript functions that will help you. The first is encodeURI(), and the second is encodeURIComponent(). Note: you might read about escape(), but that is deprecated and should not be used. Those 2 methods differ in which characters they do encode.


1 Answers

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
  ));
like image 169
Mike Samuel Avatar answered Oct 21 '22 21:10

Mike Samuel