Skip to content

Commit c1da024

Browse files
committed
making filter more robust and flexible
1 parent 5e31f67 commit c1da024

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

docs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ <h3 id="readable">readable()</h3>
627627
<h2 id="relative-and-absolute">Relative and Absolute URLs</h2>
628628

629629
<h3 id="relativeto">relativeTo()</h3>
630-
<p>.relativeTo() compares to paths an makes one relative to the other</p>
630+
<p>.relativeTo() compares two paths an makes one relative to the other</p>
631631
<pre class="prettyprint lang-js">var uri = new URI("/relative/path");
632632
// make path relative
633633
var relUri = uri.relativeTo("/relative/sub/foo/sub/file"); // returns a new URI instance

jquery-uri-plugin.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ <h1><a href="https://github.com/medialize/URI.js">URI.js</a></h1>
4949

5050
// selectors
5151
$('a:uri(fragment^=accessor)')
52+
$('a:uri(equals:http://example.org/hello/foo/../world.html)
53+
// ) may not be used in match-expression!
5254

5355
// access to (transparently updating) URI
5456
var uri = $('a').uri();

src/jquery.URI.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function getUriProperty(elem) {
4444
return property;
4545
}
4646

47-
var pseudo = /^([a-zA-Z]+)\s*([\^\$*]?=)\s*(['"]?)(.+)\3|^\s*([a-zA-Z0-9]+)\s*$/,
47+
var pseudo = /^([a-zA-Z]+)\s*([\^\$*]?=|:)\s*(['"]?)(.+)\3|^\s*([a-zA-Z0-9]+)\s*$/,
4848
comparable = {},
4949
// https://developer.mozilla.org/en/CSS/Attribute_selectors
5050
compare = {
@@ -65,6 +65,12 @@ var pseudo = /^([a-zA-Z]+)\s*([\^\$*]?=)\s*(['"]?)(.+)\3|^\s*([a-zA-Z0-9]+)\s*$/
6565
// contains
6666
'*=': function(value, target) {
6767
return !!(value + "").match(new RegExp(escapeRegEx(target), 'i'));
68+
},
69+
'equals:': function(uri, target) {
70+
return uri.equals(target);
71+
},
72+
'is:': function(uri, target) {
73+
return uri.is(target);
6874
}
6975
};
7076

@@ -145,16 +151,16 @@ $.expr.filters.uri = function(elem, index, matches) {
145151
// - https://github.com/jquery/sizzle/wiki/Sizzle-Home
146152
// - https://github.com/jquery/sizzle/blob/master/sizzle.js#L626
147153

148-
// skip anything without src|href|action
149-
if (!getUriProperty(elem)) {
154+
// skip anything without src|href|action and bad :uri() syntax
155+
if (!getUriProperty(elem) || !matches[3]) {
150156
return false;
151157
}
152158

153159
var t = matches[3].match(pseudo),
154160
property,
155161
uri;
156162

157-
if (!t || (!t[5] && !compare[t[2]])) {
163+
if (!t || (!t[5] && t[2] !== ':' && !compare[t[2]])) {
158164
// abort because the given selector cannot be executed
159165
// filers seem to fail silently
160166
return false;
@@ -164,6 +170,14 @@ $.expr.filters.uri = function(elem, index, matches) {
164170

165171
if (t[5]) {
166172
return uri.is(t[5]);
173+
} else if (t[2] === ':') {
174+
property = t[1].toLowerCase() + ':';
175+
if (!compare[property]) {
176+
// filers seem to fail silently
177+
return false;
178+
}
179+
180+
return compare[property](uri, t[4]);
167181
} else {
168182
property = t[1].toLowerCase();
169183
if (!comparable[property]) {

test/test_jquery.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module("jQuery.URI", {
33
var links = [
44
'<a href="http://example.org/">an HTTP link</a>',
55
'<a href="https://example.org/">an HTTPS link</a>',
6-
'<a href="http://example.org/some.pdf">some pdf</a>',
6+
'<a href="http://example.org/so)me.pdf">some pdf</a>',
77
'<a href="http://example.org/hello/world.html">hello world</a>',
88
'<a href="mailto:mail@example.org?subject=Hello+World">Mail me</a>',
99
'<a href="javascript:alert(\'ugly!\');">some javascript</a>',
@@ -48,7 +48,13 @@ test("filtering with :uri()", function() {
4848

4949
// find using URI.is()
5050
equal($links.find(':uri(relative)').length, 5, ":uri(relative)");
51-
51+
equal($links.find(':uri(is:relative)').length, 5, ":uri(is:relative)");
52+
53+
// find using URI.equal()
54+
equal($links.find(':uri(equals:http://example.org/hello/foo/../world.html)').length, 1, ":uri(equals:$url$)");
55+
equal($links.find(':uri(equals:"http://example.org/hello/foo/../world.html")').length, 1, ":uri(equals:$url$)");
56+
equal($links.find(':uri(equals: "http://example.org/hello/foo/../world.html")').length, 1, ":uri(equals:$url$)");
57+
5258
// find URNs
5359
equal($links.find(':uri(urn)').length, 2, ":uri(urn)");
5460

0 commit comments

Comments
 (0)