MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / jsonp-url / jsonp-url-debug.js
blob8a413bbf576a598f92e1052268bffcc2a66684b8
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('jsonp-url', function(Y) {
9 var JSONPRequest = Y.JSONPRequest,
10     getByPath    = Y.Object.getValue,
11     noop         = function () {};
13 /**
14  * Adds support for parsing complex callback identifiers from the jsonp url.
15  * This includes callback=foo[1]bar.baz["goo"] as well as referencing methods
16  * in the YUI instance.
17  *
18  * @module jsonp
19  * @submodule jsonp-url
20  * @for JSONPRequest
21  */
23 Y.mix(JSONPRequest.prototype, {
24     /**
25      * RegExp used by the default URL formatter to insert the generated callback
26      * name into the JSONP url.  Looks for a query param callback=.  If a value
27      * is assigned, it will be clobbered.
28      *
29      * @property _pattern
30      * @type RegExp
31      * @default /\bcallback=.*?(?=&|$)/i
32      * @protected
33      */
34     _pattern: /\bcallback=(.*?)(?=&|$)/i,
36     /**
37      * Template used by the default URL formatter to add the callback function
38      * name to the url.
39      *
40      * @property _template
41      * @type String
42      * @default "callback={callback}"
43      * @protected
44      */
45     _template: "callback={callback}",
47     /**
48      * <p>Parses the url for a callback named explicitly in the string.
49      * Override this if the target JSONP service uses a different query
50      * parameter or url format.</p>
51      *
52      * <p>If the callback is declared inline, the corresponding function will
53      * be returned.  Otherwise null.</p>
54      *
55      * @method _defaultCallback
56      * @param url {String} the url to search in
57      * @return {Function} the callback function if found, or null
58      * @protected
59      */
60     _defaultCallback: function (url) {
61         var match = url.match(this._pattern),
62             keys  = [],
63             i = 0,
64             locator, path, callback;
66         if (match) {
67             // Strip the ["string keys"] and [1] array indexes
68             locator = match[1]
69                 .replace(/\[(['"])(.*?)\1\]/g,
70                     function (x, $1, $2) {
71                         keys[i] = $2;
72                         return '.@' + (i++);
73                     })
74                 .replace(/\[(\d+)\]/g,
75                     function (x, $1) {
76                         keys[i] = parseInt($1, 10) | 0;
77                         return '.@' + (i++);
78                     })
79                 .replace(/^\./, ''); // remove leading dot
81             // Validate against problematic characters.
82             if (!/[^\w\.\$@]/.test(locator)) {
83                 path = locator.split('.');
84                 for (i = path.length - 1; i >= 0; --i) {
85                     if (path[i].charAt(0) === '@') {
86                         path[i] = keys[parseInt(path[i].substr(1), 10)];
87                     }
88                 }
90                 // First look for a global function, then the Y, then try the Y
91                 // again from the second token (to support "callback=Y.handler")
92                 callback = getByPath(Y.config.win, path) ||
93                            getByPath(Y, path) ||
94                            getByPath(Y, path.slice(1));
95             }
96         }
98         return callback || noop;
99     },
101     /**
102      * URL formatter that looks for callback= in the url and appends it
103      * if not present.  The supplied proxy name will be assigned to the query
104      * param.  Override this method by passing a function as the
105      * &quot;format&quot; property in the config object to the constructor.
106      *
107      * @method _format
108      * @param url { String } the original url
109      * @param proxy {String} the function name that will be used as a proxy to
110      *      the configured callback methods.
111      * @return {String} fully qualified JSONP url
112      * @protected
113      */
114     _format: function (url, proxy) {
115         var callback = this._template.replace(/\{callback\}/, proxy),
116             lastChar;
118         if (this._pattern.test(url)) {
119             return url.replace(this._pattern, callback);
120         } else {
121             lastChar = url.slice(-1);
122             if (lastChar !== '&' && lastChar !== '?') {
123                 url += (url.indexOf('?') > -1) ? '&' : '?';
124             }
125             return url + callback;
126         }
127     }
129 }, true);
132 }, '3.5.1' ,{requires:['jsonp']});