-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.js
More file actions
103 lines (73 loc) · 2.58 KB
/
Copy pathpath.js
File metadata and controls
103 lines (73 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
* Path v 1.2 2011-02-12
*
* Copyright (c) 2011, Aino
* Licensed under the MIT license.
*/
(function($) {
var window = this,
absUrl = function( url ) {
var l = window.location, h, p, f, i, n;
if ( /^\w+:/.test( url ) ) {
return url.toString();
}
h = l.protocol + '//' + l.host;
if ( url.indexOf( '/' ) === 0 ) {
return h + url.toString();
}
p = l.pathname.replace( /\/[^\/]*$/, '' );
f = url.match(/\.\.\//g);
if ( f ) {
n = url.substring(f.length * 3);
for ( i = f.length; i--; ) {
p = p.substring(0, p.lastIndexOf('/'));
}
} else {
n = url.toString();
}
return h + p + '/' + n;
},
compareArray = function( o, t ) {
var i;
if ( o.length !== t.length ) {
return false;
}
for ( i = 0; i < t.length; i++ ) {
if ( o[i] !== t[i] ) {
return false;
}
}
return true;
},
trim = function( url ) {
return url.replace(/#.*$/,'').replace(/\?.*/,'').replace(/\/$/,'');
},
location = trim( window.location.href ).split('/'),
isAnchor = function( elem ) {
return !!( elem.href && elem.nodeName.toUpperCase() === 'A' );
},
href = function( elem ) {
return trim( absUrl( elem.href ) ).split('/');
};
$.extend( $.expr[":"], {
path : function( elem ) {
if ( !isAnchor( elem ) ) {
return false;
}
var anchor = href( elem );
// if anchor points to root, activate if arrays are equal
if ( anchor.length === 3) {
return compareArray( location, anchor );
}
return ( location.length < anchor.length ) ? false :
compareArray( location.slice( 0, anchor.length ), anchor );
},
current: function( elem ) {
if ( !isAnchor( elem ) ) {
return false;
}
var anchor = href( elem );
return compareArray( location, anchor );
}
});
}( jQuery ));