Skip to content

Commit

Permalink
adding URI.iso8859() and URI.unicode() to switch base charsets (Issue #…
Browse files Browse the repository at this point in the history
…10, mortenn)

adding .iso8859() and .unicode() to convert an URI's escape encoding
  • Loading branch information
rodneyrehm committed Jan 12, 2012
1 parent cdc5d7f commit 44f0b90
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

* added URI.iso8859() and URI.unicode() to switch base charsets (Issue #10, mortenn)
* added .iso8859() and .unicode() to convert an URI's escape encoding

### 1.3.1 ###

* Updated Punycode.js to version 0.3.0
Expand Down
34 changes: 27 additions & 7 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
<li><a href="#normalize-hash">normalizeHash(), normalizeFragment()</a></li>
</ul>
</li>
<li>
Charsets / Encodings
<ul>
<li><a href="#iso8859">iso8859()</a></li>
<li><a href="#unicode">unicode()</a></li>
</ul>
</li>
<li><a href="#readable">readable()</a></li>
<li>
Relative and Absolute URLs
Expand All @@ -110,14 +117,12 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
<li><a href="#static-buildAuthority">URI.buildAuthority()</a></li>
<li><a href="#static-buildHost">URI.buildHost()</a></li>
<li><a href="#static-buildQuery">URI.buildQuery()</a></li>

<li><a href="#static-addQuery">URI.addQuery()</a></li>
<li><a href="#static-removeQuery">URI.removeQuery()</a></li>

<li><a href="#static-commonPath">URI.commonPath()</a></li>
<li><a href="#static-withinString">URI.withinString()</a></li>
<li><a href="#static-iso8859">URI.iso8859()</a></li>
<li><a href="#static-unicode">URI.unicode()</a></li>
<li><a href="#static-iso8859">URI.iso8859()</a></li>
<li><a href="#static-unicode">URI.unicode()</a></li>
</ul>
</li>
</ul>
Expand Down Expand Up @@ -501,6 +506,22 @@ <h3 id="normalize-hash">normalizeHash(), normalizeFragment()</h3>
uri.normalizeHash(); // returns the URI instance for chaining
// uri == "http://example.org/bar/world.xml"</pre>


<h2 id="charsets">Charsets / Encodings</h2>

<h3 id="#iso8859">iso8859()</h3>
<p>.iso8859() converts unicode-encoded escape sequences to ISO8859-encoded escape sequences. It does this by calling <a href="#normalize">.normalize()</a> internally.</p>
<pre class="prettyprint lang-js">var uri = new URI("/%C3%A4.html");
uri.iso8859(); // returns the URI instance for chaining
// uri == "/%E4.html"</pre>
<p>NOTE: You can make URI work with ISO8859 encoding by default by calling <a href="#static-iso8859">URI.iso8859()</a>.

<h3 id="#unicode">unicode()</h3>
<p>.unicode() converts ISO8859-encoded escape sequences to unicode-encoded escape sequences. It does this by calling <a href="#normalize">.normalize()</a> internally.</p>
<pre class="prettyprint lang-js">var uri = new URI("/%E4.html");
uri.unicode(); // returns the URI instance for chaining
// uri == "/%C3%A4.html"</pre>


<h2 id="formatting">Formatting URLs</h2>

Expand Down Expand Up @@ -530,8 +551,7 @@ <h3 id="absoluteto">absoluteTo()</h3>
var relUri = uri.absoluteTo("/relative/sub/foo/sub/file"); // returns a new URI instance
// relUri == "/relative/path"</pre>
<p>.relativeTo() and .absoluteTo() reverse each other.</p>




<h2 id="comparison">Comparing URLs</h2>

Expand Down Expand Up @@ -760,7 +780,7 @@ <h3 id="static-withinString">URI.withinString()</h3>
</pre>

<h3 id="static-iso8859">URI.iso8859()</h3>
<p>URI.iso8859() tells URI.js to use the older escape/unescape methods, for backwards compatibility with older platforms.</p>
<p>URI.iso8859() tells URI.js to use the older escape/unescape methods, for backwards compatibility with non-unicode platforms.</p>
<pre class="prettyprint lang-js">URI.iso8859();

var uri = new URI("http://example.org/foo/æ.html");
Expand Down
46 changes: 35 additions & 11 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ var URI = function(url, base) {
};
var p = URI.prototype;

URI.iso8859 = function() {
URI.encode = escape;
URI.decode = unescape;
}

URI.unicode = function() {
URI.encode = encodeURIComponent;
URI.decode = decodeURIComponent;
}

// static properties
URI.idn_expression = /[^a-z0-9\.-]/i;
URI.punycode_expression = /(xn--)/i;
Expand All @@ -103,6 +93,14 @@ URI.invalid_hostname_characters = /[^a-zA-Z0-9\.-]/;
// encoding / decoding according to RFC3986
URI.encode = encodeURIComponent;
URI.decode = decodeURIComponent;
URI.iso8859 = function() {
URI.encode = escape;
URI.decode = unescape;
};
URI.unicode = function() {
URI.encode = encodeURIComponent;
URI.decode = decodeURIComponent;
};
URI.characters = {
pathname: {
encode: {
Expand Down Expand Up @@ -160,7 +158,7 @@ var _parts = {'encode':'encode', 'decode':'decode'},
for (_part in _parts) {
URI[_part + "PathSegment"] = (function(_part){
return function(string) {
return window[_part + 'URIComponent'](string + "").replace(URI.characters.pathname[_part].expression, function(c) {
return URI[_part](string + "").replace(URI.characters.pathname[_part].expression, function(c) {
return URI.characters.pathname[_part].map[c];
});
};
Expand Down Expand Up @@ -1079,6 +1077,32 @@ p.normalizeFragment = function(build) {
p.normalizeSearch = p.normalizeQuery;
p.normalizeHash = p.normalizeFragment;

p.iso8859 = function() {
// expect unicode input, iso8859 output
var e = URI.encode,
d = URI.decode;

URI.encode = escape;
URI.decode = decodeURIComponent;
this.normalize();
URI.encode = e;
URI.decode = d;
return this;
};

p.unicode = function() {
// expect iso8859 input, unicode output
var e = URI.encode,
d = URI.decode;

URI.encode = encodeURIComponent;
URI.decode = unescape;
this.normalize();
URI.encode = e;
URI.decode = d;
return this;
};

p.readable = function() {
var uri = new URI(this);
// removing username, password, because they shouldn't be displayed according to RFC 3986
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,3 +737,30 @@ test("equals", function() {
}
});

module("Charset");
test("iso8859", function() {
var u = new URI("/ä.html");
u.normalizePath();
equal(u.path(), "/%C3%A4.html", 'Unicode');

URI.iso8859();
u = new URI("/ä.html");
u.normalizePath();
equal(u.path(), "/%E4.html", 'ISO8859');
u.path('/ö.html');
equal(u.path(), "/%F6.html", 'ISO8859');

URI.unicode();
u = new URI("/ä.html");
u.normalizePath();
equal(u.path(), "/%C3%A4.html", 'Unicode again');

u = new URI("/ä.html");
u.normalizePath();
equal(u.path(), "/%C3%A4.html", 'convert unicode start');
u.iso8859();
equal(u.path(), "/%E4.html", 'convert iso8859');
u.unicode();
equal(u.path(), "/%C3%A4.html", 'convert unicode');
});

0 comments on commit 44f0b90

Please sign in to comment.