Skip to content

Commit

Permalink
fix(withinString): allow callback to return undefined to signal no ch…
Browse files Browse the repository at this point in the history
…ange - #303
  • Loading branch information
rodneyrehm committed Jun 30, 2016
1 parent 6ed94b3 commit aab3f81
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

The release notes tracked in this document are also made available on the [releases page](https://github.com/medialize/URI.js/releases)

### master ###

* fixing [`URI.withinString()`](http://medialize.github.io/URI.js/docs.html#static-withinString) to allow callback to return `undefined` or `string` - [Issue #303](https://github.com/medialize/URI.js/issues/303)

### 1.18.1 (May 29th 2016) ###

* fixing UMD wrapper of `jquery.URI.js` - [Issue #295](https://github.com/medialize/URI.js/issues/295)
Expand Down
6 changes: 6 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,12 @@

end = start + slice.length;
var result = callback(slice, start, end, string);
if (result === undefined) {
_start.lastIndex = end;
continue;
}

result = String(result);
string = string.slice(0, start) + result + string.slice(end);
_start.lastIndex = start + result.length;
}
Expand Down
27 changes: 27 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1619,6 +1619,33 @@

equal(result, expected, 'filtered in string URI identification');
});
test('withinString - capture only', function() {
/*jshint laxbreak: true */
var source = 'Hello www.example.com,\n'
+ 'http://google.com is a search engine, like http://www.bing.com\n'
+ 'http://exämple.org/foo.html?baz=la#bumm is an IDN URL,\n'
+ 'http://123.123.123.123/foo.html is IPv4 and http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html is IPv6.\n'
+ 'links can also be in parens (http://example.org) or quotes »http://example.org«.';
var expected = [
'www.example.com',
'http://google.com',
'http://www.bing.com',
'http://exämple.org/foo.html?baz=la#bumm',
'http://123.123.123.123/foo.html',
'http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html',
'http://example.org',
'http://example.org'
];

/*jshint laxbreak: false */
var links = [];
var result = URI.withinString(source, function(url) {
links.push(url);
});

deepEqual(links, expected, 'urls extracted');
equal(result, source, 'source not modified');
});
test('noConflict', function() {
var actual_lib = URI; // actual library; after loading, before noConflict()
var unconflicted = URI.noConflict();
Expand Down

0 comments on commit aab3f81

Please sign in to comment.