Skip to content

Loading…

Helper methods for using URI.js against servers not understanding UTF-8 encoded URIs #10

Merged
merged 1 commit into from

2 participants

@mortenn

Hello,

This patch adds two new static functions - URI.iso8859 and URI.unicode - that replace the URI.encode and URI.decode refs with the old escape/unescape functions - and restores the default.
I have also modified the en/de-codeQuery methods to reference URI.en/de-code in order for the switching of modes to have the least possible impact (and avoiding redefining the methods).

I have also added the two new static methods to the documentation.

// Cheers, Morten

@rodneyrehm
Medialize member

wouldn't it also be nice to be able to URI('/%C3%A6.html').iso8859().toString() === '/%E6.html' and vice versa?

@mortenn

I suppose so, yes, but this patch was written with the intent of using URI.js to enhance older applications, where everything will be ISO and adding doing .iso8859() to each url on such a system would be counterproductive.

The main concern with enabling URI().iso8859().unicode()... is the age old problem of applying the wrong thing in the wrong order, or to the wrong thing.

I would certainly not mind that syntax, but I do not think I will implement that.

@mortenn

Another thing; new URI('...%E6...') makes IE8 error because %E6 is not valid unicode - this patch should fix that case as well, provided one has called URI.iso8859().

@rodneyrehm rodneyrehm merged commit cdc5d7f into medialize:gh-pages
@rodneyrehm rodneyrehm added a commit that referenced this pull request
@rodneyrehm rodneyrehm adding URI.iso8859() and URI.unicode() to switch base charsets (Issue #…
…10, mortenn)

adding .iso8859() and .unicode() to convert an URI's escape encoding
44f0b90
@rodneyrehm
Medialize member

included in URI.js v1.4.0

@mortenn

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 12, 2012
  1. @mortenn
Showing with 32 additions and 6 deletions.
  1. +17 −1 docs.html
  2. +15 −5 src/URI.js
View
18 docs.html
@@ -116,6 +116,8 @@
<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>
</ul>
</li>
</ul>
@@ -757,6 +759,20 @@ <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>
+ <pre class="prettyprint lang-js">URI.iso8859();
+
+var uri = new URI("http://example.org/foo/æ.html");
+// http://example.org/foo/%E6.html</pre>
+
+ <h3 id="static-unicode">URI.unicode()</h3>
+ <p>URI.unicode() restores the default unicode-encoded URLs.</p>
+ <pre class="prettyprint lang-js">URI.unicode();
+
+var uri = new URI("http://example.org/foo/æ.html");
+// http://example.org/foo/%C3%A6.html</pre>
+
</div>
</body>
-</html>
+</html>
View
20 src/URI.js
@@ -65,8 +65,18 @@ var URI = function(url, base) {
}
return this;
- },
- p = URI.prototype;
+ };
+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;
@@ -122,10 +132,10 @@ URI.characters = {
}
};
URI.encodeQuery = function(string) {
- return encodeURIComponent(string + "").replace(/%20/g, '+');
+ return URI.encode(string + "").replace(/%20/g, '+');
};
URI.decodeQuery = function(string) {
- return decodeURIComponent((string + "").replace(/\+/g, '%20'));
+ return URI.decode((string + "").replace(/\+/g, '%20'));
};
URI.recodePath = function(string) {
var segments = (string + "").split('/');
@@ -1260,4 +1270,4 @@ p.equals = function(uri) {
window.URI = URI;
-})();
+})();
Something went wrong with that request. Please try again.