Skip to content

Commit 44f0b90

Browse files
committed
adding URI.iso8859() and URI.unicode() to switch base charsets (Issue #10, mortenn)
adding .iso8859() and .unicode() to convert an URI's escape encoding
1 parent cdc5d7f commit 44f0b90

4 files changed

Lines changed: 92 additions & 18 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
165165

166166
## Changelog ##
167167

168+
* added URI.iso8859() and URI.unicode() to switch base charsets (Issue #10, mortenn)
169+
* added .iso8859() and .unicode() to convert an URI's escape encoding
170+
168171
### 1.3.1 ###
169172

170173
* Updated Punycode.js to version 0.3.0

docs.html

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
8585
<li><a href="#normalize-hash">normalizeHash(), normalizeFragment()</a></li>
8686
</ul>
8787
</li>
88+
<li>
89+
Charsets / Encodings
90+
<ul>
91+
<li><a href="#iso8859">iso8859()</a></li>
92+
<li><a href="#unicode">unicode()</a></li>
93+
</ul>
94+
</li>
8895
<li><a href="#readable">readable()</a></li>
8996
<li>
9097
Relative and Absolute URLs
@@ -110,14 +117,12 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
110117
<li><a href="#static-buildAuthority">URI.buildAuthority()</a></li>
111118
<li><a href="#static-buildHost">URI.buildHost()</a></li>
112119
<li><a href="#static-buildQuery">URI.buildQuery()</a></li>
113-
114120
<li><a href="#static-addQuery">URI.addQuery()</a></li>
115121
<li><a href="#static-removeQuery">URI.removeQuery()</a></li>
116-
117122
<li><a href="#static-commonPath">URI.commonPath()</a></li>
118123
<li><a href="#static-withinString">URI.withinString()</a></li>
119-
<li><a href="#static-iso8859">URI.iso8859()</a></li>
120-
<li><a href="#static-unicode">URI.unicode()</a></li>
124+
<li><a href="#static-iso8859">URI.iso8859()</a></li>
125+
<li><a href="#static-unicode">URI.unicode()</a></li>
121126
</ul>
122127
</li>
123128
</ul>
@@ -501,6 +506,22 @@ <h3 id="normalize-hash">normalizeHash(), normalizeFragment()</h3>
501506
uri.normalizeHash(); // returns the URI instance for chaining
502507
// uri == "http://example.org/bar/world.xml"</pre>
503508

509+
510+
<h2 id="charsets">Charsets / Encodings</h2>
511+
512+
<h3 id="#iso8859">iso8859()</h3>
513+
<p>.iso8859() converts unicode-encoded escape sequences to ISO8859-encoded escape sequences. It does this by calling <a href="#normalize">.normalize()</a> internally.</p>
514+
<pre class="prettyprint lang-js">var uri = new URI("/%C3%A4.html");
515+
uri.iso8859(); // returns the URI instance for chaining
516+
// uri == "/%E4.html"</pre>
517+
<p>NOTE: You can make URI work with ISO8859 encoding by default by calling <a href="#static-iso8859">URI.iso8859()</a>.
518+
519+
<h3 id="#unicode">unicode()</h3>
520+
<p>.unicode() converts ISO8859-encoded escape sequences to unicode-encoded escape sequences. It does this by calling <a href="#normalize">.normalize()</a> internally.</p>
521+
<pre class="prettyprint lang-js">var uri = new URI("/%E4.html");
522+
uri.unicode(); // returns the URI instance for chaining
523+
// uri == "/%C3%A4.html"</pre>
524+
504525

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

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

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

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

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

766786
var uri = new URI("http://example.org/foo/æ.html");

src/URI.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ var URI = function(url, base) {
6868
};
6969
var p = URI.prototype;
7070

71-
URI.iso8859 = function() {
72-
URI.encode = escape;
73-
URI.decode = unescape;
74-
}
75-
76-
URI.unicode = function() {
77-
URI.encode = encodeURIComponent;
78-
URI.decode = decodeURIComponent;
79-
}
80-
8171
// static properties
8272
URI.idn_expression = /[^a-z0-9\.-]/i;
8373
URI.punycode_expression = /(xn--)/i;
@@ -103,6 +93,14 @@ URI.invalid_hostname_characters = /[^a-zA-Z0-9\.-]/;
10393
// encoding / decoding according to RFC3986
10494
URI.encode = encodeURIComponent;
10595
URI.decode = decodeURIComponent;
96+
URI.iso8859 = function() {
97+
URI.encode = escape;
98+
URI.decode = unescape;
99+
};
100+
URI.unicode = function() {
101+
URI.encode = encodeURIComponent;
102+
URI.decode = decodeURIComponent;
103+
};
106104
URI.characters = {
107105
pathname: {
108106
encode: {
@@ -160,7 +158,7 @@ var _parts = {'encode':'encode', 'decode':'decode'},
160158
for (_part in _parts) {
161159
URI[_part + "PathSegment"] = (function(_part){
162160
return function(string) {
163-
return window[_part + 'URIComponent'](string + "").replace(URI.characters.pathname[_part].expression, function(c) {
161+
return URI[_part](string + "").replace(URI.characters.pathname[_part].expression, function(c) {
164162
return URI.characters.pathname[_part].map[c];
165163
});
166164
};
@@ -1079,6 +1077,32 @@ p.normalizeFragment = function(build) {
10791077
p.normalizeSearch = p.normalizeQuery;
10801078
p.normalizeHash = p.normalizeFragment;
10811079

1080+
p.iso8859 = function() {
1081+
// expect unicode input, iso8859 output
1082+
var e = URI.encode,
1083+
d = URI.decode;
1084+
1085+
URI.encode = escape;
1086+
URI.decode = decodeURIComponent;
1087+
this.normalize();
1088+
URI.encode = e;
1089+
URI.decode = d;
1090+
return this;
1091+
};
1092+
1093+
p.unicode = function() {
1094+
// expect iso8859 input, unicode output
1095+
var e = URI.encode,
1096+
d = URI.decode;
1097+
1098+
URI.encode = encodeURIComponent;
1099+
URI.decode = unescape;
1100+
this.normalize();
1101+
URI.encode = e;
1102+
URI.decode = d;
1103+
return this;
1104+
};
1105+
10821106
p.readable = function() {
10831107
var uri = new URI(this);
10841108
// removing username, password, because they shouldn't be displayed according to RFC 3986

test/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,30 @@ test("equals", function() {
737737
}
738738
});
739739

740+
module("Charset");
741+
test("iso8859", function() {
742+
var u = new URI("/ä.html");
743+
u.normalizePath();
744+
equal(u.path(), "/%C3%A4.html", 'Unicode');
745+
746+
URI.iso8859();
747+
u = new URI("/ä.html");
748+
u.normalizePath();
749+
equal(u.path(), "/%E4.html", 'ISO8859');
750+
u.path('/ö.html');
751+
equal(u.path(), "/%F6.html", 'ISO8859');
752+
753+
URI.unicode();
754+
u = new URI("/ä.html");
755+
u.normalizePath();
756+
equal(u.path(), "/%C3%A4.html", 'Unicode again');
757+
758+
u = new URI("/ä.html");
759+
u.normalizePath();
760+
equal(u.path(), "/%C3%A4.html", 'convert unicode start');
761+
u.iso8859();
762+
equal(u.path(), "/%E4.html", 'convert iso8859');
763+
u.unicode();
764+
equal(u.path(), "/%C3%A4.html", 'convert unicode');
765+
});
766+

0 commit comments

Comments
 (0)