Skip to content

Commit

Permalink
fixing internals to accept String instances - #146
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Sep 10, 2014
1 parent 1bd6835 commit 9a5ccce
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m

## Changelog ##

### master (might become 1.14.1) ###

* fixing handling of String instances (not string primitives) - ([Issue #146](https://github.com/medialize/URI.js/issues/146))

### 1.14.0 (September 8th 2014) ###

* adding Hungarian second level domains - ([Issue #159](https://github.com/medialize/URI.js/issues/159))
Expand Down
16 changes: 8 additions & 8 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,8 @@
href = href.toString();
}

if (typeof href === 'string') {
this._parts = URI.parse(href, this._parts);
if (typeof href === 'string' || href instanceof String) {
this._parts = URI.parse(String(href), this._parts);
} else if (_URI || _object) {
var src = _URI ? href._parts : href;
for (key in src) {
Expand Down Expand Up @@ -1488,7 +1488,7 @@

segments.push(v[i]);
}
} else if (v || (typeof v === 'string')) {
} else if (v || typeof v === 'string') {
if (segments[segments.length -1] === '') {
// empty trailing elements have to be overwritten
// to prevent results such as /foo//bar
Expand All @@ -1498,7 +1498,7 @@
}
}
} else {
if (v || (typeof v === 'string' && v.length)) {
if (v) {
segments[segment] = v;
} else {
segments.splice(segment, 1);
Expand Down Expand Up @@ -1534,7 +1534,7 @@
}

if (!isArray(v)) {
v = typeof v === 'string' ? URI.encode(v) : v;
v = (typeof v === 'string' || v instanceof String) ? URI.encode(v) : v;
} else {
for (i = 0, l = v.length; i < l; i++) {
v[i] = URI.decode(v[i]);
Expand Down Expand Up @@ -1566,14 +1566,14 @@
p.setQuery = function(name, value, build) {
var data = URI.parseQuery(this._parts.query, this._parts.escapeQuerySpace);

if (typeof name === 'object') {
if (typeof name === 'string' || name instanceof String) {
data[name] = value !== undefined ? value : null;
} else if (typeof name === 'object') {
for (var key in name) {
if (hasOwn.call(name, key)) {
data[key] = name[key];
}
}
} else if (typeof name === 'string') {
data[name] = value !== undefined ? value : null;
} else {
throw new TypeError('URI.addQuery() accepts an object, string as the name parameter');
}
Expand Down
5 changes: 5 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@
equal(u.search(), '', 'href removed search');
equal(u.hash(), '', 'href removed hash');
equal(u.href(), '../path/index.html', 'href removed url');

/*jshint -W053 */
u.href(new String('/narf'));
/*jshint +W053 */
equal(u.pathname(), '/narf', 'href from String instance');
});
test('resource', function() {
var u = new URI('http://foo.bar/foo.html?hello#world');
Expand Down

0 comments on commit 9a5ccce

Please sign in to comment.