2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3
Code licensed under the BSD License:
4
http://developer.yahoo.com/yui/license.html
8
YUI.add('jsonp-url', function(Y) {
10
var JSONPRequest = Y.JSONPRequest,
11
getByPath = Y.Object.getValue,
12
noop = function () {};
15
* Adds support for parsing complex callback identifiers from the jsonp url.
16
* This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
17
* in the YUI instance.
20
* @submodule jsonp-url
24
Y.mix(JSONPRequest.prototype, {
26
* RegExp used by the default URL formatter to insert the generated callback
27
* name into the JSONP url. Looks for a query param callback=. If a value
28
* is assigned, it will be clobbered.
32
* @default /\bcallback=.*?(?=&|$)/i
35
_pattern: /\bcallback=(.*?)(?=&|$)/i,
38
* Template used by the default URL formatter to add the callback function
43
* @default "callback={callback}"
46
_template: "callback={callback}",
49
* <p>Parses the url for a callback named explicitly in the string.
50
* Override this if the target JSONP service uses a different query
51
* parameter or url format.</p>
53
* <p>If the callback is declared inline, the corresponding function will
54
* be returned. Otherwise null.</p>
56
* @method _defaultCallback
57
* @param url {String} the url to search in
58
* @return {Function} the callback function if found, or null
61
_defaultCallback: function (url) {
62
var match = url.match(this._pattern),
65
locator, path, callback;
68
// Strip the ["string keys"] and [1] array indexes
70
.replace(/\[(['"])(.*?)\1\]/g,
71
function (x, $1, $2) {
75
.replace(/\[(\d+)\]/g,
77
keys[i] = parseInt($1, 10) | 0;
80
.replace(/^\./, ''); // remove leading dot
82
// Validate against problematic characters.
83
if (!/[^\w\.\$@]/.test(locator)) {
84
path = locator.split('.');
85
for (i = path.length - 1; i >= 0; --i) {
86
if (path[i].charAt(0) === '@') {
87
path[i] = keys[parseInt(path[i].substr(1), 10)];
91
// First look for a global function, then the Y, then try the Y
92
// again from the second token (to support "callback=Y.handler")
93
callback = getByPath(Y.config.win, path) ||
95
getByPath(Y, path.slice(1));
99
return callback || noop;
103
* URL formatter that looks for callback= in the url and appends it
104
* if not present. The supplied proxy name will be assigned to the query
105
* param. Override this method by passing a function as the
106
* "format" property in the config object to the constructor.
109
* @param url { String } the original url
110
* @param proxy {String} the function name that will be used as a proxy to
111
* the configured callback methods.
112
* @return {String} fully qualified JSONP url
115
_format: function (url, proxy) {
116
var callback = this._template.replace(/\{callback\}/, proxy),
119
if (this._pattern.test(url)) {
120
return url.replace(this._pattern, callback);
122
lastChar = url.slice(-1);
123
if (lastChar !== '&' && lastChar !== '?') {
124
url += (url.indexOf('?') > -1) ? '&' : '?';
126
return url + callback;
133
}, '3.2.0' ,{requires:['jsonp']});