jquery.URI.js
6.56 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*!
* URI.js - Mutating URLs
* jQuery Plugin
*
* Version: 1.19.0
*
* Author: Rodney Rehm
* Web: http://medialize.github.io/URI.js/jquery-uri-plugin.html
*
* Licensed under
* MIT License http://www.opensource.org/licenses/mit-license
*
*/
(function (root, factory) {
'use strict';
// https://github.com/umdjs/umd/blob/master/returnExports.js
if (typeof module === 'object' && module.exports) {
// Node
module.exports = factory(require('jquery'), require('./URI'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery', './URI'], factory);
} else {
// Browser globals (root is window)
factory(root.jQuery, root.URI);
}
}(this, function ($, URI) {
'use strict';
// FIXME: v2.0.0 renamce non-camelCase properties to uppercase
/*jshint camelcase: false */
var comparable = {};
var compare = {
// equals
'=': function(value, target) {
return value === target;
},
// ~= translates to value.match((?:^|\s)target(?:\s|$)) which is useless for URIs
// |= translates to value.match((?:\b)target(?:-|\s|$)) which is useless for URIs
// begins with
'^=': function(value, target) {
return !!(value + '').match(new RegExp('^' + escapeRegEx(target), 'i'));
},
// ends with
'$=': function(value, target) {
return !!(value + '').match(new RegExp(escapeRegEx(target) + '$', 'i'));
},
// contains
'*=': function(value, target, property) {
if (property === 'directory') {
// add trailing slash so /dir/ will match the deep-end as well
value += '/';
}
return !!(value + '').match(new RegExp(escapeRegEx(target), 'i'));
},
'equals:': function(uri, target) {
return uri.equals(target);
},
'is:': function(uri, target) {
return uri.is(target);
}
};
function escapeRegEx(string) {
// https://github.com/medialize/URI.js/commit/85ac21783c11f8ccab06106dba9735a31a86924d#commitcomment-821963
return string.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
}
function getUriProperty(elem) {
var nodeName = elem.nodeName.toLowerCase();
var property = URI.domAttributes[nodeName];
if (nodeName === 'input' && elem.type !== 'image') {
// compensate ambiguous <input> that is not an image
return undefined;
}
// NOTE: as we use a static mapping from element to attribute,
// the HTML5 attribute issue should not come up again
// https://github.com/medialize/URI.js/issues/69
return property;
}
function generateAccessor(property) {
return {
get: function(elem) {
return $(elem).uri()[property]();
},
set: function(elem, value) {
$(elem).uri()[property](value);
return value;
}
};
}
// populate lookup table and register $.attr('uri:accessor') handlers
$.each('origin authority directory domain filename fragment hash host hostname href password path pathname port protocol query resource scheme search subdomain suffix tld username'.split(' '), function(k, v) {
comparable[v] = true;
$.attrHooks['uri:' + v] = generateAccessor(v);
});
// pipe $.attr('src') and $.attr('href') through URI.js
var _attrHooks = {
get: function(elem) {
return $(elem).uri();
},
set: function(elem, value) {
return $(elem).uri().href(value).toString();
}
};
$.each(['src', 'href', 'action', 'uri', 'cite'], function(k, v) {
$.attrHooks[v] = {
set: _attrHooks.set
};
});
$.attrHooks.uri.get = _attrHooks.get;
// general URI accessor
$.fn.uri = function(uri) {
var $this = this.first();
var elem = $this.get(0);
var property = getUriProperty(elem);
if (!property) {
throw new Error('Element "' + elem.nodeName + '" does not have either property: href, src, action, cite');
}
if (uri !== undefined) {
var old = $this.data('uri');
if (old) {
return old.href(uri);
}
if (!(uri instanceof URI)) {
uri = URI(uri || '');
}
} else {
uri = $this.data('uri');
if (uri) {
return uri;
} else {
uri = URI($this.attr(property) || '');
}
}
uri._dom_element = elem;
uri._dom_attribute = property;
uri.normalize();
$this.data('uri', uri);
return uri;
};
// overwrite URI.build() to update associated DOM element if necessary
URI.prototype.build = function(deferBuild) {
if (this._dom_element) {
// cannot defer building when hooked into a DOM element
this._string = URI.build(this._parts);
this._deferred_build = false;
this._dom_element.setAttribute(this._dom_attribute, this._string);
this._dom_element[this._dom_attribute] = this._string;
} else if (deferBuild === true) {
this._deferred_build = true;
} else if (deferBuild === undefined || this._deferred_build) {
this._string = URI.build(this._parts);
this._deferred_build = false;
}
return this;
};
// add :uri() pseudo class selector to sizzle
var uriSizzle;
var pseudoArgs = /^([a-zA-Z]+)\s*([\^\$*]?=|:)\s*(['"]?)(.+)\3|^\s*([a-zA-Z0-9]+)\s*$/;
function uriPseudo (elem, text) {
var match, property, uri;
// skip anything without src|href|action and bad :uri() syntax
if (!getUriProperty(elem) || !text) {
return false;
}
match = text.match(pseudoArgs);
if (!match || (!match[5] && match[2] !== ':' && !compare[match[2]])) {
// abort because the given selector cannot be executed
// filers seem to fail silently
return false;
}
uri = $(elem).uri();
if (match[5]) {
return uri.is(match[5]);
} else if (match[2] === ':') {
property = match[1].toLowerCase() + ':';
if (!compare[property]) {
// filers seem to fail silently
return false;
}
return compare[property](uri, match[4]);
} else {
property = match[1].toLowerCase();
if (!comparable[property]) {
// filers seem to fail silently
return false;
}
return compare[match[2]](uri[property](), match[4], property);
}
return false;
}
if ($.expr.createPseudo) {
// jQuery >= 1.8
uriSizzle = $.expr.createPseudo(function (text) {
return function (elem) {
return uriPseudo(elem, text);
};
});
} else {
// jQuery < 1.8
uriSizzle = function (elem, i, match) {
return uriPseudo(elem, match[3]);
};
}
$.expr[':'].uri = uriSizzle;
// extending existing object rather than defining something new,
// return jQuery anyway
return $;
}));