NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / jsonp-url / jsonp-url.js
blobb7914e7add9e63b8abc22367c6f7080127a3159b
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('jsonp-url', function (Y, NAME) {
10 var JSONPRequest = Y.JSONPRequest,
11     getByPath    = Y.Object.getValue,
12     noop         = function () {};
14 /**
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.
18  *
19  * @module jsonp
20  * @submodule jsonp-url
21  * @for JSONPRequest
22  */
24 Y.mix(JSONPRequest.prototype, {
25     /**
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.
29      *
30      * @property _pattern
31      * @type RegExp
32      * @default /\bcallback=.*?(?=&|$)/i
33      * @protected
34      */
35     _pattern: /\bcallback=(.*?)(?=&|$)/i,
37     /**
38      * Template used by the default URL formatter to add the callback function
39      * name to the url.
40      *
41      * @property _template
42      * @type String
43      * @default "callback={callback}"
44      * @protected
45      */
46     _template: "callback={callback}",
48     /**
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>
52      *
53      * <p>If the callback is declared inline, the corresponding function will
54      * be returned.  Otherwise null.</p>
55      *
56      * @method _defaultCallback
57      * @param url {String} the url to search in
58      * @return {Function} the callback function if found, or null
59      * @protected
60      */
61     _defaultCallback: function (url) {
62         var match = url.match(this._pattern),
63             keys  = [],
64             i = 0,
65             locator, path, callback;
67         if (match) {
68             // Strip the ["string keys"] and [1] array indexes
69             locator = match[1]
70                 .replace(/\[(['"])(.*?)\1\]/g,
71                     function (x, $1, $2) {
72                         keys[i] = $2;
73                         return '.@' + (i++);
74                     })
75                 .replace(/\[(\d+)\]/g,
76                     function (x, $1) {
77                         keys[i] = parseInt($1, 10) | 0;
78                         return '.@' + (i++);
79                     })
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)];
88                     }
89                 }
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) ||
94                            getByPath(Y, path) ||
95                            getByPath(Y, path.slice(1));
96             }
97         }
99         return callback || noop;
100     },
102     /**
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      * &quot;format&quot; property in the config object to the constructor.
107      *
108      * @method _format
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
113      * @protected
114      */
115     _format: function (url, proxy) {
116         var callbackRE = /\{callback\}/,
117             callback, lastChar;
119         if (callbackRE.test(url)) {
120             return url.replace(callbackRE, proxy);
121         }
123         callback = this._template.replace(callbackRE, proxy);
125         if (this._pattern.test(url)) {
126             return url.replace(this._pattern, callback);
127         } else {
128             lastChar = url.slice(-1);
129             if (lastChar !== '&' && lastChar !== '?') {
130                 url += (url.indexOf('?') > -1) ? '&' : '?';
131             }
132             return url + callback;
133         }
134     }
136 }, true);
139 }, '3.13.0', {"requires": ["jsonp"]});