Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding URI.iso8859() and URI.unicode() to switch base charsets (Issue #…

…10, mortenn)

adding .iso8859() and .unicode() to convert an URI's escape encoding
  • Loading branch information...
commit 44f0b9098d5b844fd2537cdf7af883264cdcd4a4 1 parent cdc5d7f
@rodneyrehm rodneyrehm authored
Showing with 92 additions and 18 deletions.
  1. +3 −0  README.md
  2. +27 −7 docs.html
  3. +35 −11 src/URI.js
  4. +27 −0 test/test.js
View
3  README.md
@@ -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
View
34 docs.html
@@ -85,6 +85,13 @@
<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
@@ -110,14 +117,12 @@
<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>
@@ -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>
@@ -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>
@@ -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");
View
46 src/URI.js
@@ -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;
@@ -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: {
@@ -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];
});
};
@@ -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
View
27 test/test.js
@@ -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');
+});
+
Please sign in to comment.
Something went wrong with that request. Please try again.