~ted/lazr-js/annoying-debug-message

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/jsonp/jsonp-url.js

  • Committer: Launchpad Patch Queue Manager
  • Date: 2010-09-09 14:20:30 UTC
  • mfrom: (182.1.3 yui-3.2)
  • Revision ID: launchpad@pqm.canonical.com-20100909142030-13w6vo0ixfysxc15
[r=beuno] Update lazr-js to yui-3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.2.0
 
6
build: 2676
 
7
*/
 
8
YUI.add('jsonp-url', function(Y) {
 
9
 
 
10
var JSONPRequest = Y.JSONPRequest,
 
11
    getByPath    = Y.Object.getValue,
 
12
    noop         = function () {};
 
13
 
 
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
 */
 
23
 
 
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
     * @member _pattern
 
31
     * @type RegExp
 
32
     * @default /\bcallback=.*?(?=&|$)/i
 
33
     * @protected
 
34
     */
 
35
    _pattern: /\bcallback=(.*?)(?=&|$)/i,
 
36
 
 
37
    /**
 
38
     * Template used by the default URL formatter to add the callback function
 
39
     * name to the url.
 
40
     *
 
41
     * @member _template
 
42
     * @type String
 
43
     * @default "callback={callback}"
 
44
     * @protected
 
45
     */
 
46
    _template: "callback={callback}",
 
47
 
 
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;
 
66
 
 
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
 
81
 
 
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
                }
 
90
 
 
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
        }
 
98
 
 
99
        return callback || noop;
 
100
    },
 
101
 
 
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 callback = this._template.replace(/\{callback\}/, proxy),
 
117
            lastChar;
 
118
 
 
119
        if (this._pattern.test(url)) {
 
120
            return url.replace(this._pattern, callback);
 
121
        } else {
 
122
            lastChar = url.slice(-1);
 
123
            if (lastChar !== '&' && lastChar !== '?') {
 
124
                url += (url.indexOf('?') > -1) ? '&' : '?';
 
125
            }
 
126
            return url + callback;
 
127
        }
 
128
    }
 
129
 
 
130
}, true);
 
131
 
 
132
 
 
133
}, '3.2.0' ,{requires:['jsonp']});