Skip to content

Commit

Permalink
fix(withinString): include balanced parens - closes #247
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Dec 4, 2016
1 parent a1e8914 commit 77300b1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 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 capture balanced parentheses - [Issue #247](https://github.com/medialize/URI.js/issues/247)

### 1.18.3 (November 17th 2016) ###

* fixing UMD wrappers to properly detect CommonJS - [Issue #318](https://github.com/medialize/URI.js/issues/318), [PR #319](https://github.com/medialize/URI.js/pull/319)
Expand Down
25 changes: 23 additions & 2 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@
// everything up to the next whitespace
end: /[\s\r\n]|$/,
// trim trailing punctuation captured by end RegExp
trim: /[`!()\[\]{};:'".,<>?«»“”„‘’]+$/
trim: /[`!()\[\]{};:'".,<>?«»“”„‘’]+$/,
// balanced parens inclusion (), [], {}, <>
parens: /(\([^\)]*\)|\[[^\]]*\]|\{[^}]*\}|<[^>]*>)/g,
};
// http://www.iana.org/assignments/uri-schemes.html
// http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers#Well-known_ports
Expand Down Expand Up @@ -942,6 +944,7 @@
var _start = options.start || URI.findUri.start;
var _end = options.end || URI.findUri.end;
var _trim = options.trim || URI.findUri.trim;
var _parens = options.parens || URI.findUri.parens;
var _attributeOpen = /[a-z0-9-]=["']?$/i;

_start.lastIndex = 0;
Expand All @@ -961,7 +964,25 @@
}

var end = start + string.slice(start).search(_end);
var slice = string.slice(start, end).replace(_trim, '');
var slice = string.slice(start, end);
// make sure we include well balanced parens
var parensEnd = -1;
while (true) {
var parensMatch = _parens.exec(slice);
if (!parensMatch) {
break;
}

var parensMatchEnd = parensMatch.index + parensMatch[0].length;
parensEnd = Math.max(parensEnd, parensMatchEnd);
}

if (parensEnd > -1) {
slice = slice.slice(0, parensEnd) + slice.slice(parensEnd + 1).replace(_trim, '');
} else {
slice = slice.replace(_trim, '');
}

if (options.ignore && options.ignore.test(slice)) {
continue;
}
Expand Down
10 changes: 7 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1579,12 +1579,16 @@
+ '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«.';
+ 'links can also be in parens (http://example.org) or quotes »http://example.org«, '
+ 'yet https://example.com/with_(balanced_parentheses) contains the closing parens, but '
+ 'https://example.com/with_unbalanced_parentheses) does not.';
var expected = 'Hello <a>www.example.com</a>,\n'
+ '<a>http://google.com</a> is a search engine, like <a>http://www.bing.com</a>\n'
+ '<a>http://exämple.org/foo.html?baz=la#bumm</a> is an IDN URL,\n'
+ '<a>http://123.123.123.123/foo.html</a> is IPv4 and <a>http://fe80:0000:0000:0000:0204:61ff:fe9d:f156/foobar.html</a> is IPv6.\n'
+ 'links can also be in parens (<a>http://example.org</a>) or quotes »<a>http://example.org</a>«.';
+ 'links can also be in parens (<a>http://example.org</a>) or quotes »<a>http://example.org</a>«, '
+ 'yet <a>https://example.com/with_(balanced_parentheses)</a> contains the closing parens, but '
+ '<a>https://example.com/with_unbalanced_parentheses</a>) does not.';
/*jshint laxbreak: false */
var result = URI.withinString(source, function(url) {
return '<a>' + url + '</a>';
Expand Down Expand Up @@ -1685,7 +1689,7 @@
});
test('joinPaths', function() {
var result;

result = URI.joinPaths('/a/b', '/c', 'd', '/e').toString();
equal(result, '/a/b/c/d/e', 'absolute paths');

Expand Down

0 comments on commit 77300b1

Please sign in to comment.