weekly release 2.4dev
[moodle.git] / lib / yuilib / 3.7.1 / build / history-hash / history-hash-coverage.js
blob8c0aa65372697e9a449c7b034b9b28c8018d5a02
1 /*
2 YUI 3.7.1 (build 5627)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 if (typeof _yuitest_coverage == "undefined"){
8     _yuitest_coverage = {};
9     _yuitest_coverline = function(src, line){
10         var coverage = _yuitest_coverage[src];
11         if (!coverage.lines[line]){
12             coverage.calledLines++;
13         }
14         coverage.lines[line]++;
15     };
16     _yuitest_coverfunc = function(src, name, line){
17         var coverage = _yuitest_coverage[src],
18             funcId = name + ":" + line;
19         if (!coverage.functions[funcId]){
20             coverage.calledFunctions++;
21         }
22         coverage.functions[funcId]++;
23     };
25 _yuitest_coverage["build/history-hash/history-hash.js"] = {
26     lines: {},
27     functions: {},
28     coveredLines: 0,
29     calledLines: 0,
30     coveredFunctions: 0,
31     calledFunctions: 0,
32     path: "build/history-hash/history-hash.js",
33     code: []
35 _yuitest_coverage["build/history-hash/history-hash.js"].code=["YUI.add('history-hash', function (Y, NAME) {","","/**"," * Provides browser history management backed by"," * <code>window.location.hash</code>, as well as convenience methods for working"," * with the location hash and a synthetic <code>hashchange</code> event that"," * normalizes differences across browsers."," *"," * @module history"," * @submodule history-hash"," * @since 3.2.0"," * @class HistoryHash"," * @extends HistoryBase"," * @constructor"," * @param {Object} config (optional) Configuration object. See the HistoryBase"," *   documentation for details."," */","","var HistoryBase = Y.HistoryBase,","    Lang        = Y.Lang,","    YArray      = Y.Array,","    YObject     = Y.Object,","    GlobalEnv   = YUI.namespace('Env.HistoryHash'),","","    SRC_HASH    = 'hash',","","    hashNotifiers,","    oldHash,","    oldUrl,","    win             = Y.config.win,","    useHistoryHTML5 = Y.config.useHistoryHTML5;","","function HistoryHash() {","    HistoryHash.superclass.constructor.apply(this, arguments);","}","","Y.extend(HistoryHash, HistoryBase, {","    // -- Initialization -------------------------------------------------------","    _init: function (config) {","        var bookmarkedState = HistoryHash.parseHash();","","        // If an initialState was provided, merge the bookmarked state into it","        // (the bookmarked state wins).","        config = config || {};","","        this._initialState = config.initialState ?","                Y.merge(config.initialState, bookmarkedState) : bookmarkedState;","","        // Subscribe to the synthetic hashchange event (defined below) to handle","        // changes.","        Y.after('hashchange', Y.bind(this._afterHashChange, this), win);","","        HistoryHash.superclass._init.apply(this, arguments);","    },","","    // -- Protected Methods ----------------------------------------------------","    _change: function (src, state, options) {","        // Stringify all values to ensure that comparisons don't fail after","        // they're coerced to strings in the location hash.","        YObject.each(state, function (value, key) {","            if (Lang.isValue(value)) {","                state[key] = value.toString();","            }","        });","","        return HistoryHash.superclass._change.call(this, src, state, options);","    },","","    _storeState: function (src, newState) {","        var decode  = HistoryHash.decode,","            newHash = HistoryHash.createHash(newState);","","        HistoryHash.superclass._storeState.apply(this, arguments);","","        // Update the location hash with the changes, but only if the new hash","        // actually differs from the current hash (this avoids creating multiple","        // history entries for a single state).","        //","        // We always compare decoded hashes, since it's possible that the hash","        // could be set incorrectly to a non-encoded value outside of","        // HistoryHash.","        if (src !== SRC_HASH && decode(HistoryHash.getHash()) !== decode(newHash)) {","            HistoryHash[src === HistoryBase.SRC_REPLACE ? 'replaceHash' : 'setHash'](newHash);","        }","    },","","    // -- Protected Event Handlers ---------------------------------------------","","    /**","     * Handler for hashchange events.","     *","     * @method _afterHashChange","     * @param {Event} e","     * @protected","     */","    _afterHashChange: function (e) {","        this._resolveChanges(SRC_HASH, HistoryHash.parseHash(e.newHash), {});","    }","}, {","    // -- Public Static Properties ---------------------------------------------","    NAME: 'historyHash',","","    /**","     * Constant used to identify state changes originating from","     * <code>hashchange</code> events.","     *","     * @property SRC_HASH","     * @type String","     * @static","     * @final","     */","    SRC_HASH: SRC_HASH,","","    /**","     * <p>","     * Prefix to prepend when setting the hash fragment. For example, if the","     * prefix is <code>!</code> and the hash fragment is set to","     * <code>#foo=bar&baz=quux</code>, the final hash fragment in the URL will","     * become <code>#!foo=bar&baz=quux</code>. This can be used to help make an","     * Ajax application crawlable in accordance with Google's guidelines at","     * <a href=\"http://code.google.com/web/ajaxcrawling/\">http://code.google.com/web/ajaxcrawling/</a>.","     * </p>","     *","     * <p>","     * Note that this prefix applies to all HistoryHash instances. It's not","     * possible for individual instances to use their own prefixes since they","     * all operate on the same URL.","     * </p>","     *","     * @property hashPrefix","     * @type String","     * @default ''","     * @static","     */","    hashPrefix: '',","","    // -- Protected Static Properties ------------------------------------------","","    /**","     * Regular expression used to parse location hash/query strings.","     *","     * @property _REGEX_HASH","     * @type RegExp","     * @protected","     * @static","     * @final","     */","    _REGEX_HASH: /([^\\?#&]+)=([^&]+)/g,","","    // -- Public Static Methods ------------------------------------------------","","    /**","     * Creates a location hash string from the specified object of key/value","     * pairs.","     *","     * @method createHash","     * @param {Object} params object of key/value parameter pairs","     * @return {String} location hash string","     * @static","     */","    createHash: function (params) {","        var encode = HistoryHash.encode,","            hash   = [];","","        YObject.each(params, function (value, key) {","            if (Lang.isValue(value)) {","                hash.push(encode(key) + '=' + encode(value));","            }","        });","","        return hash.join('&');","    },","","    /**","     * Wrapper around <code>decodeURIComponent()</code> that also converts +","     * chars into spaces.","     *","     * @method decode","     * @param {String} string string to decode","     * @return {String} decoded string","     * @static","     */","    decode: function (string) {","        return decodeURIComponent(string.replace(/\\+/g, ' '));","    },","","    /**","     * Wrapper around <code>encodeURIComponent()</code> that converts spaces to","     * + chars.","     *","     * @method encode","     * @param {String} string string to encode","     * @return {String} encoded string","     * @static","     */","    encode: function (string) {","        return encodeURIComponent(string).replace(/%20/g, '+');","    },","","    /**","     * Gets the raw (not decoded) current location hash, minus the preceding '#'","     * character and the hashPrefix (if one is set).","     *","     * @method getHash","     * @return {String} current location hash","     * @static","     */","    getHash: (Y.UA.gecko ? function () {","        // Gecko's window.location.hash returns a decoded string and we want all","        // encoding untouched, so we need to get the hash value from","        // window.location.href instead. We have to use UA sniffing rather than","        // feature detection, since the only way to detect this would be to","        // actually change the hash.","        var location = Y.getLocation(),","            matches  = /#(.*)$/.exec(location.href),","            hash     = matches && matches[1] || '',","            prefix   = HistoryHash.hashPrefix;","","        return prefix && hash.indexOf(prefix) === 0 ?","                    hash.replace(prefix, '') : hash;","    } : function () {","        var location = Y.getLocation(),","            hash     = location.hash.substring(1),","            prefix   = HistoryHash.hashPrefix;","","        // Slight code duplication here, but execution speed is of the essence","        // since getHash() is called every 50ms to poll for changes in browsers","        // that don't support native onhashchange. An additional function call","        // would add unnecessary overhead.","        return prefix && hash.indexOf(prefix) === 0 ?","                    hash.replace(prefix, '') : hash;","    }),","","    /**","     * Gets the current bookmarkable URL.","     *","     * @method getUrl","     * @return {String} current bookmarkable URL","     * @static","     */","    getUrl: function () {","        return location.href;","    },","","    /**","     * Parses a location hash string into an object of key/value parameter","     * pairs. If <i>hash</i> is not specified, the current location hash will","     * be used.","     *","     * @method parseHash","     * @param {String} hash (optional) location hash string","     * @return {Object} object of parsed key/value parameter pairs","     * @static","     */","    parseHash: function (hash) {","        var decode = HistoryHash.decode,","            i,","            len,","            matches,","            param,","            params = {},","            prefix = HistoryHash.hashPrefix,","            prefixIndex;","","        hash = Lang.isValue(hash) ? hash : HistoryHash.getHash();","","        if (prefix) {","            prefixIndex = hash.indexOf(prefix);","","            if (prefixIndex === 0 || (prefixIndex === 1 && hash.charAt(0) === '#')) {","                hash = hash.replace(prefix, '');","            }","        }","","        matches = hash.match(HistoryHash._REGEX_HASH) || [];","","        for (i = 0, len = matches.length; i < len; ++i) {","            param = matches[i].split('=');","            params[decode(param[0])] = decode(param[1]);","        }","","        return params;","    },","","    /**","     * Replaces the browser's current location hash with the specified hash","     * and removes all forward navigation states, without creating a new browser","     * history entry. Automatically prepends the <code>hashPrefix</code> if one","     * is set.","     *","     * @method replaceHash","     * @param {String} hash new location hash","     * @static","     */","    replaceHash: function (hash) {","        var location = Y.getLocation(),","            base     = location.href.replace(/#.*$/, '');","","        if (hash.charAt(0) === '#') {","            hash = hash.substring(1);","        }","","        location.replace(base + '#' + (HistoryHash.hashPrefix || '') + hash);","    },","","    /**","     * Sets the browser's location hash to the specified string. Automatically","     * prepends the <code>hashPrefix</code> if one is set.","     *","     * @method setHash","     * @param {String} hash new location hash","     * @static","     */","    setHash: function (hash) {","        var location = Y.getLocation();","","        if (hash.charAt(0) === '#') {","            hash = hash.substring(1);","        }","","        location.hash = (HistoryHash.hashPrefix || '') + hash;","    }","});","","// -- Synthetic hashchange Event -----------------------------------------------","","// TODO: YUIDoc currently doesn't provide a good way to document synthetic DOM","// events. For now, we're just documenting the hashchange event on the YUI","// object, which is about the best we can do until enhancements are made to","// YUIDoc.","","/**","Synthetic <code>window.onhashchange</code> event that normalizes differences","across browsers and provides support for browsers that don't natively support","<code>onhashchange</code>.","","This event is provided by the <code>history-hash</code> module.","","@example","","    YUI().use('history-hash', function (Y) {","      Y.on('hashchange', function (e) {","        // Handle hashchange events on the current window.","      }, Y.config.win);","    });","","@event hashchange","@param {EventFacade} e Event facade with the following additional","  properties:","","<dl>","  <dt>oldHash</dt>","  <dd>","    Previous hash fragment value before the change.","  </dd>","","  <dt>oldUrl</dt>","  <dd>","    Previous URL (including the hash fragment) before the change.","  </dd>","","  <dt>newHash</dt>","  <dd>","    New hash fragment value after the change.","  </dd>","","  <dt>newUrl</dt>","  <dd>","    New URL (including the hash fragment) after the change.","  </dd>","</dl>","@for YUI","@since 3.2.0","**/","","hashNotifiers = GlobalEnv._notifiers;","","if (!hashNotifiers) {","    hashNotifiers = GlobalEnv._notifiers = [];","}","","Y.Event.define('hashchange', {","    on: function (node, subscriber, notifier) {","        // Ignore this subscription if the node is anything other than the","        // window or document body, since those are the only elements that","        // should support the hashchange event. Note that the body could also be","        // a frameset, but that's okay since framesets support hashchange too.","        if (node.compareTo(win) || node.compareTo(Y.config.doc.body)) {","            hashNotifiers.push(notifier);","        }","    },","","    detach: function (node, subscriber, notifier) {","        var index = YArray.indexOf(hashNotifiers, notifier);","","        if (index !== -1) {","            hashNotifiers.splice(index, 1);","        }","    }","});","","oldHash = HistoryHash.getHash();","oldUrl  = HistoryHash.getUrl();","","if (HistoryBase.nativeHashChange) {","    // Wrap the browser's native hashchange event if there's not already a","    // global listener.","    if (!GlobalEnv._hashHandle) {","        GlobalEnv._hashHandle = Y.Event.attach('hashchange', function (e) {","            var newHash = HistoryHash.getHash(),","                newUrl  = HistoryHash.getUrl();","","            // Iterate over a copy of the hashNotifiers array since a subscriber","            // could detach during iteration and cause the array to be re-indexed.","            YArray.each(hashNotifiers.concat(), function (notifier) {","                notifier.fire({","                    _event : e,","                    oldHash: oldHash,","                    oldUrl : oldUrl,","                    newHash: newHash,","                    newUrl : newUrl","                });","            });","","            oldHash = newHash;","            oldUrl  = newUrl;","        }, win);","    }","} else {","    // Begin polling for location hash changes if there's not already a global","    // poll running.","    if (!GlobalEnv._hashPoll) {","        GlobalEnv._hashPoll = Y.later(50, null, function () {","            var newHash = HistoryHash.getHash(),","                facade, newUrl;","","            if (oldHash !== newHash) {","                newUrl = HistoryHash.getUrl();","","                facade = {","                    oldHash: oldHash,","                    oldUrl : oldUrl,","                    newHash: newHash,","                    newUrl : newUrl","                };","","                oldHash = newHash;","                oldUrl  = newUrl;","","                YArray.each(hashNotifiers.concat(), function (notifier) {","                    notifier.fire(facade);","                });","            }","        }, null, true);","    }","}","","Y.HistoryHash = HistoryHash;","","// HistoryHash will never win over HistoryHTML5 unless useHistoryHTML5 is false.","if (useHistoryHTML5 === false || (!Y.History && useHistoryHTML5 !== true &&","        (!HistoryBase.html5 || !Y.HistoryHTML5))) {","    Y.History = HistoryHash;","}","","","}, '3.7.1', {\"requires\": [\"event-synthetic\", \"history-base\", \"yui-later\"]});"];
36 _yuitest_coverage["build/history-hash/history-hash.js"].lines = {"1":0,"19":0,"33":0,"34":0,"37":0,"40":0,"44":0,"46":0,"51":0,"53":0,"60":0,"61":0,"62":0,"66":0,"70":0,"73":0,"82":0,"83":0,"97":0,"162":0,"165":0,"166":0,"167":0,"171":0,"184":0,"197":0,"214":0,"219":0,"222":0,"230":0,"242":0,"256":0,"265":0,"267":0,"268":0,"270":0,"271":0,"275":0,"277":0,"278":0,"279":0,"282":0,"296":0,"299":0,"300":0,"303":0,"315":0,"317":0,"318":0,"321":0,"376":0,"378":0,"379":0,"382":0,"388":0,"389":0,"394":0,"396":0,"397":0,"402":0,"403":0,"405":0,"408":0,"409":0,"410":0,"415":0,"416":0,"425":0,"426":0,"432":0,"433":0,"434":0,"437":0,"438":0,"440":0,"447":0,"448":0,"450":0,"451":0,"458":0,"461":0,"463":0};
37 _yuitest_coverage["build/history-hash/history-hash.js"].functions = {"HistoryHash:33":0,"_init:39":0,"(anonymous 2):60":0,"_change:57":0,"_storeState:69":0,"_afterHashChange:96":0,"(anonymous 3):165":0,"createHash:161":0,"decode:183":0,"encode:196":0,"(anonymous 4):208":0,"}:221":0,"getUrl:241":0,"parseHash:255":0,"replaceHash:295":0,"setHash:314":0,"on:383":0,"detach:393":0,"(anonymous 6):415":0,"(anonymous 5):409":0,"(anonymous 8):450":0,"(anonymous 7):433":0,"(anonymous 1):1":0};
38 _yuitest_coverage["build/history-hash/history-hash.js"].coveredLines = 82;
39 _yuitest_coverage["build/history-hash/history-hash.js"].coveredFunctions = 23;
40 _yuitest_coverline("build/history-hash/history-hash.js", 1);
41 YUI.add('history-hash', function (Y, NAME) {
43 /**
44  * Provides browser history management backed by
45  * <code>window.location.hash</code>, as well as convenience methods for working
46  * with the location hash and a synthetic <code>hashchange</code> event that
47  * normalizes differences across browsers.
48  *
49  * @module history
50  * @submodule history-hash
51  * @since 3.2.0
52  * @class HistoryHash
53  * @extends HistoryBase
54  * @constructor
55  * @param {Object} config (optional) Configuration object. See the HistoryBase
56  *   documentation for details.
57  */
59 _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 1)", 1);
60 _yuitest_coverline("build/history-hash/history-hash.js", 19);
61 var HistoryBase = Y.HistoryBase,
62     Lang        = Y.Lang,
63     YArray      = Y.Array,
64     YObject     = Y.Object,
65     GlobalEnv   = YUI.namespace('Env.HistoryHash'),
67     SRC_HASH    = 'hash',
69     hashNotifiers,
70     oldHash,
71     oldUrl,
72     win             = Y.config.win,
73     useHistoryHTML5 = Y.config.useHistoryHTML5;
75 _yuitest_coverline("build/history-hash/history-hash.js", 33);
76 function HistoryHash() {
77     _yuitest_coverfunc("build/history-hash/history-hash.js", "HistoryHash", 33);
78 _yuitest_coverline("build/history-hash/history-hash.js", 34);
79 HistoryHash.superclass.constructor.apply(this, arguments);
82 _yuitest_coverline("build/history-hash/history-hash.js", 37);
83 Y.extend(HistoryHash, HistoryBase, {
84     // -- Initialization -------------------------------------------------------
85     _init: function (config) {
86         _yuitest_coverfunc("build/history-hash/history-hash.js", "_init", 39);
87 _yuitest_coverline("build/history-hash/history-hash.js", 40);
88 var bookmarkedState = HistoryHash.parseHash();
90         // If an initialState was provided, merge the bookmarked state into it
91         // (the bookmarked state wins).
92         _yuitest_coverline("build/history-hash/history-hash.js", 44);
93 config = config || {};
95         _yuitest_coverline("build/history-hash/history-hash.js", 46);
96 this._initialState = config.initialState ?
97                 Y.merge(config.initialState, bookmarkedState) : bookmarkedState;
99         // Subscribe to the synthetic hashchange event (defined below) to handle
100         // changes.
101         _yuitest_coverline("build/history-hash/history-hash.js", 51);
102 Y.after('hashchange', Y.bind(this._afterHashChange, this), win);
104         _yuitest_coverline("build/history-hash/history-hash.js", 53);
105 HistoryHash.superclass._init.apply(this, arguments);
106     },
108     // -- Protected Methods ----------------------------------------------------
109     _change: function (src, state, options) {
110         // Stringify all values to ensure that comparisons don't fail after
111         // they're coerced to strings in the location hash.
112         _yuitest_coverfunc("build/history-hash/history-hash.js", "_change", 57);
113 _yuitest_coverline("build/history-hash/history-hash.js", 60);
114 YObject.each(state, function (value, key) {
115             _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 2)", 60);
116 _yuitest_coverline("build/history-hash/history-hash.js", 61);
117 if (Lang.isValue(value)) {
118                 _yuitest_coverline("build/history-hash/history-hash.js", 62);
119 state[key] = value.toString();
120             }
121         });
123         _yuitest_coverline("build/history-hash/history-hash.js", 66);
124 return HistoryHash.superclass._change.call(this, src, state, options);
125     },
127     _storeState: function (src, newState) {
128         _yuitest_coverfunc("build/history-hash/history-hash.js", "_storeState", 69);
129 _yuitest_coverline("build/history-hash/history-hash.js", 70);
130 var decode  = HistoryHash.decode,
131             newHash = HistoryHash.createHash(newState);
133         _yuitest_coverline("build/history-hash/history-hash.js", 73);
134 HistoryHash.superclass._storeState.apply(this, arguments);
136         // Update the location hash with the changes, but only if the new hash
137         // actually differs from the current hash (this avoids creating multiple
138         // history entries for a single state).
139         //
140         // We always compare decoded hashes, since it's possible that the hash
141         // could be set incorrectly to a non-encoded value outside of
142         // HistoryHash.
143         _yuitest_coverline("build/history-hash/history-hash.js", 82);
144 if (src !== SRC_HASH && decode(HistoryHash.getHash()) !== decode(newHash)) {
145             _yuitest_coverline("build/history-hash/history-hash.js", 83);
146 HistoryHash[src === HistoryBase.SRC_REPLACE ? 'replaceHash' : 'setHash'](newHash);
147         }
148     },
150     // -- Protected Event Handlers ---------------------------------------------
152     /**
153      * Handler for hashchange events.
154      *
155      * @method _afterHashChange
156      * @param {Event} e
157      * @protected
158      */
159     _afterHashChange: function (e) {
160         _yuitest_coverfunc("build/history-hash/history-hash.js", "_afterHashChange", 96);
161 _yuitest_coverline("build/history-hash/history-hash.js", 97);
162 this._resolveChanges(SRC_HASH, HistoryHash.parseHash(e.newHash), {});
163     }
164 }, {
165     // -- Public Static Properties ---------------------------------------------
166     NAME: 'historyHash',
168     /**
169      * Constant used to identify state changes originating from
170      * <code>hashchange</code> events.
171      *
172      * @property SRC_HASH
173      * @type String
174      * @static
175      * @final
176      */
177     SRC_HASH: SRC_HASH,
179     /**
180      * <p>
181      * Prefix to prepend when setting the hash fragment. For example, if the
182      * prefix is <code>!</code> and the hash fragment is set to
183      * <code>#foo=bar&baz=quux</code>, the final hash fragment in the URL will
184      * become <code>#!foo=bar&baz=quux</code>. This can be used to help make an
185      * Ajax application crawlable in accordance with Google's guidelines at
186      * <a href="http://code.google.com/web/ajaxcrawling/">http://code.google.com/web/ajaxcrawling/</a>.
187      * </p>
188      *
189      * <p>
190      * Note that this prefix applies to all HistoryHash instances. It's not
191      * possible for individual instances to use their own prefixes since they
192      * all operate on the same URL.
193      * </p>
194      *
195      * @property hashPrefix
196      * @type String
197      * @default ''
198      * @static
199      */
200     hashPrefix: '',
202     // -- Protected Static Properties ------------------------------------------
204     /**
205      * Regular expression used to parse location hash/query strings.
206      *
207      * @property _REGEX_HASH
208      * @type RegExp
209      * @protected
210      * @static
211      * @final
212      */
213     _REGEX_HASH: /([^\?#&]+)=([^&]+)/g,
215     // -- Public Static Methods ------------------------------------------------
217     /**
218      * Creates a location hash string from the specified object of key/value
219      * pairs.
220      *
221      * @method createHash
222      * @param {Object} params object of key/value parameter pairs
223      * @return {String} location hash string
224      * @static
225      */
226     createHash: function (params) {
227         _yuitest_coverfunc("build/history-hash/history-hash.js", "createHash", 161);
228 _yuitest_coverline("build/history-hash/history-hash.js", 162);
229 var encode = HistoryHash.encode,
230             hash   = [];
232         _yuitest_coverline("build/history-hash/history-hash.js", 165);
233 YObject.each(params, function (value, key) {
234             _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 3)", 165);
235 _yuitest_coverline("build/history-hash/history-hash.js", 166);
236 if (Lang.isValue(value)) {
237                 _yuitest_coverline("build/history-hash/history-hash.js", 167);
238 hash.push(encode(key) + '=' + encode(value));
239             }
240         });
242         _yuitest_coverline("build/history-hash/history-hash.js", 171);
243 return hash.join('&');
244     },
246     /**
247      * Wrapper around <code>decodeURIComponent()</code> that also converts +
248      * chars into spaces.
249      *
250      * @method decode
251      * @param {String} string string to decode
252      * @return {String} decoded string
253      * @static
254      */
255     decode: function (string) {
256         _yuitest_coverfunc("build/history-hash/history-hash.js", "decode", 183);
257 _yuitest_coverline("build/history-hash/history-hash.js", 184);
258 return decodeURIComponent(string.replace(/\+/g, ' '));
259     },
261     /**
262      * Wrapper around <code>encodeURIComponent()</code> that converts spaces to
263      * + chars.
264      *
265      * @method encode
266      * @param {String} string string to encode
267      * @return {String} encoded string
268      * @static
269      */
270     encode: function (string) {
271         _yuitest_coverfunc("build/history-hash/history-hash.js", "encode", 196);
272 _yuitest_coverline("build/history-hash/history-hash.js", 197);
273 return encodeURIComponent(string).replace(/%20/g, '+');
274     },
276     /**
277      * Gets the raw (not decoded) current location hash, minus the preceding '#'
278      * character and the hashPrefix (if one is set).
279      *
280      * @method getHash
281      * @return {String} current location hash
282      * @static
283      */
284     getHash: (Y.UA.gecko ? function () {
285         // Gecko's window.location.hash returns a decoded string and we want all
286         // encoding untouched, so we need to get the hash value from
287         // window.location.href instead. We have to use UA sniffing rather than
288         // feature detection, since the only way to detect this would be to
289         // actually change the hash.
290         _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 4)", 208);
291 _yuitest_coverline("build/history-hash/history-hash.js", 214);
292 var location = Y.getLocation(),
293             matches  = /#(.*)$/.exec(location.href),
294             hash     = matches && matches[1] || '',
295             prefix   = HistoryHash.hashPrefix;
297         _yuitest_coverline("build/history-hash/history-hash.js", 219);
298 return prefix && hash.indexOf(prefix) === 0 ?
299                     hash.replace(prefix, '') : hash;
300     } : function () {
301         _yuitest_coverfunc("build/history-hash/history-hash.js", "}", 221);
302 _yuitest_coverline("build/history-hash/history-hash.js", 222);
303 var location = Y.getLocation(),
304             hash     = location.hash.substring(1),
305             prefix   = HistoryHash.hashPrefix;
307         // Slight code duplication here, but execution speed is of the essence
308         // since getHash() is called every 50ms to poll for changes in browsers
309         // that don't support native onhashchange. An additional function call
310         // would add unnecessary overhead.
311         _yuitest_coverline("build/history-hash/history-hash.js", 230);
312 return prefix && hash.indexOf(prefix) === 0 ?
313                     hash.replace(prefix, '') : hash;
314     }),
316     /**
317      * Gets the current bookmarkable URL.
318      *
319      * @method getUrl
320      * @return {String} current bookmarkable URL
321      * @static
322      */
323     getUrl: function () {
324         _yuitest_coverfunc("build/history-hash/history-hash.js", "getUrl", 241);
325 _yuitest_coverline("build/history-hash/history-hash.js", 242);
326 return location.href;
327     },
329     /**
330      * Parses a location hash string into an object of key/value parameter
331      * pairs. If <i>hash</i> is not specified, the current location hash will
332      * be used.
333      *
334      * @method parseHash
335      * @param {String} hash (optional) location hash string
336      * @return {Object} object of parsed key/value parameter pairs
337      * @static
338      */
339     parseHash: function (hash) {
340         _yuitest_coverfunc("build/history-hash/history-hash.js", "parseHash", 255);
341 _yuitest_coverline("build/history-hash/history-hash.js", 256);
342 var decode = HistoryHash.decode,
343             i,
344             len,
345             matches,
346             param,
347             params = {},
348             prefix = HistoryHash.hashPrefix,
349             prefixIndex;
351         _yuitest_coverline("build/history-hash/history-hash.js", 265);
352 hash = Lang.isValue(hash) ? hash : HistoryHash.getHash();
354         _yuitest_coverline("build/history-hash/history-hash.js", 267);
355 if (prefix) {
356             _yuitest_coverline("build/history-hash/history-hash.js", 268);
357 prefixIndex = hash.indexOf(prefix);
359             _yuitest_coverline("build/history-hash/history-hash.js", 270);
360 if (prefixIndex === 0 || (prefixIndex === 1 && hash.charAt(0) === '#')) {
361                 _yuitest_coverline("build/history-hash/history-hash.js", 271);
362 hash = hash.replace(prefix, '');
363             }
364         }
366         _yuitest_coverline("build/history-hash/history-hash.js", 275);
367 matches = hash.match(HistoryHash._REGEX_HASH) || [];
369         _yuitest_coverline("build/history-hash/history-hash.js", 277);
370 for (i = 0, len = matches.length; i < len; ++i) {
371             _yuitest_coverline("build/history-hash/history-hash.js", 278);
372 param = matches[i].split('=');
373             _yuitest_coverline("build/history-hash/history-hash.js", 279);
374 params[decode(param[0])] = decode(param[1]);
375         }
377         _yuitest_coverline("build/history-hash/history-hash.js", 282);
378 return params;
379     },
381     /**
382      * Replaces the browser's current location hash with the specified hash
383      * and removes all forward navigation states, without creating a new browser
384      * history entry. Automatically prepends the <code>hashPrefix</code> if one
385      * is set.
386      *
387      * @method replaceHash
388      * @param {String} hash new location hash
389      * @static
390      */
391     replaceHash: function (hash) {
392         _yuitest_coverfunc("build/history-hash/history-hash.js", "replaceHash", 295);
393 _yuitest_coverline("build/history-hash/history-hash.js", 296);
394 var location = Y.getLocation(),
395             base     = location.href.replace(/#.*$/, '');
397         _yuitest_coverline("build/history-hash/history-hash.js", 299);
398 if (hash.charAt(0) === '#') {
399             _yuitest_coverline("build/history-hash/history-hash.js", 300);
400 hash = hash.substring(1);
401         }
403         _yuitest_coverline("build/history-hash/history-hash.js", 303);
404 location.replace(base + '#' + (HistoryHash.hashPrefix || '') + hash);
405     },
407     /**
408      * Sets the browser's location hash to the specified string. Automatically
409      * prepends the <code>hashPrefix</code> if one is set.
410      *
411      * @method setHash
412      * @param {String} hash new location hash
413      * @static
414      */
415     setHash: function (hash) {
416         _yuitest_coverfunc("build/history-hash/history-hash.js", "setHash", 314);
417 _yuitest_coverline("build/history-hash/history-hash.js", 315);
418 var location = Y.getLocation();
420         _yuitest_coverline("build/history-hash/history-hash.js", 317);
421 if (hash.charAt(0) === '#') {
422             _yuitest_coverline("build/history-hash/history-hash.js", 318);
423 hash = hash.substring(1);
424         }
426         _yuitest_coverline("build/history-hash/history-hash.js", 321);
427 location.hash = (HistoryHash.hashPrefix || '') + hash;
428     }
431 // -- Synthetic hashchange Event -----------------------------------------------
433 // TODO: YUIDoc currently doesn't provide a good way to document synthetic DOM
434 // events. For now, we're just documenting the hashchange event on the YUI
435 // object, which is about the best we can do until enhancements are made to
436 // YUIDoc.
439 Synthetic <code>window.onhashchange</code> event that normalizes differences
440 across browsers and provides support for browsers that don't natively support
441 <code>onhashchange</code>.
443 This event is provided by the <code>history-hash</code> module.
445 @example
447     YUI().use('history-hash', function (Y) {
448       Y.on('hashchange', function (e) {
449         // Handle hashchange events on the current window.
450       }, Y.config.win);
451     });
453 @event hashchange
454 @param {EventFacade} e Event facade with the following additional
455   properties:
457 <dl>
458   <dt>oldHash</dt>
459   <dd>
460     Previous hash fragment value before the change.
461   </dd>
463   <dt>oldUrl</dt>
464   <dd>
465     Previous URL (including the hash fragment) before the change.
466   </dd>
468   <dt>newHash</dt>
469   <dd>
470     New hash fragment value after the change.
471   </dd>
473   <dt>newUrl</dt>
474   <dd>
475     New URL (including the hash fragment) after the change.
476   </dd>
477 </dl>
478 @for YUI
479 @since 3.2.0
482 _yuitest_coverline("build/history-hash/history-hash.js", 376);
483 hashNotifiers = GlobalEnv._notifiers;
485 _yuitest_coverline("build/history-hash/history-hash.js", 378);
486 if (!hashNotifiers) {
487     _yuitest_coverline("build/history-hash/history-hash.js", 379);
488 hashNotifiers = GlobalEnv._notifiers = [];
491 _yuitest_coverline("build/history-hash/history-hash.js", 382);
492 Y.Event.define('hashchange', {
493     on: function (node, subscriber, notifier) {
494         // Ignore this subscription if the node is anything other than the
495         // window or document body, since those are the only elements that
496         // should support the hashchange event. Note that the body could also be
497         // a frameset, but that's okay since framesets support hashchange too.
498         _yuitest_coverfunc("build/history-hash/history-hash.js", "on", 383);
499 _yuitest_coverline("build/history-hash/history-hash.js", 388);
500 if (node.compareTo(win) || node.compareTo(Y.config.doc.body)) {
501             _yuitest_coverline("build/history-hash/history-hash.js", 389);
502 hashNotifiers.push(notifier);
503         }
504     },
506     detach: function (node, subscriber, notifier) {
507         _yuitest_coverfunc("build/history-hash/history-hash.js", "detach", 393);
508 _yuitest_coverline("build/history-hash/history-hash.js", 394);
509 var index = YArray.indexOf(hashNotifiers, notifier);
511         _yuitest_coverline("build/history-hash/history-hash.js", 396);
512 if (index !== -1) {
513             _yuitest_coverline("build/history-hash/history-hash.js", 397);
514 hashNotifiers.splice(index, 1);
515         }
516     }
519 _yuitest_coverline("build/history-hash/history-hash.js", 402);
520 oldHash = HistoryHash.getHash();
521 _yuitest_coverline("build/history-hash/history-hash.js", 403);
522 oldUrl  = HistoryHash.getUrl();
524 _yuitest_coverline("build/history-hash/history-hash.js", 405);
525 if (HistoryBase.nativeHashChange) {
526     // Wrap the browser's native hashchange event if there's not already a
527     // global listener.
528     _yuitest_coverline("build/history-hash/history-hash.js", 408);
529 if (!GlobalEnv._hashHandle) {
530         _yuitest_coverline("build/history-hash/history-hash.js", 409);
531 GlobalEnv._hashHandle = Y.Event.attach('hashchange', function (e) {
532             _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 5)", 409);
533 _yuitest_coverline("build/history-hash/history-hash.js", 410);
534 var newHash = HistoryHash.getHash(),
535                 newUrl  = HistoryHash.getUrl();
537             // Iterate over a copy of the hashNotifiers array since a subscriber
538             // could detach during iteration and cause the array to be re-indexed.
539             _yuitest_coverline("build/history-hash/history-hash.js", 415);
540 YArray.each(hashNotifiers.concat(), function (notifier) {
541                 _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 6)", 415);
542 _yuitest_coverline("build/history-hash/history-hash.js", 416);
543 notifier.fire({
544                     _event : e,
545                     oldHash: oldHash,
546                     oldUrl : oldUrl,
547                     newHash: newHash,
548                     newUrl : newUrl
549                 });
550             });
552             _yuitest_coverline("build/history-hash/history-hash.js", 425);
553 oldHash = newHash;
554             _yuitest_coverline("build/history-hash/history-hash.js", 426);
555 oldUrl  = newUrl;
556         }, win);
557     }
558 } else {
559     // Begin polling for location hash changes if there's not already a global
560     // poll running.
561     _yuitest_coverline("build/history-hash/history-hash.js", 432);
562 if (!GlobalEnv._hashPoll) {
563         _yuitest_coverline("build/history-hash/history-hash.js", 433);
564 GlobalEnv._hashPoll = Y.later(50, null, function () {
565             _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 7)", 433);
566 _yuitest_coverline("build/history-hash/history-hash.js", 434);
567 var newHash = HistoryHash.getHash(),
568                 facade, newUrl;
570             _yuitest_coverline("build/history-hash/history-hash.js", 437);
571 if (oldHash !== newHash) {
572                 _yuitest_coverline("build/history-hash/history-hash.js", 438);
573 newUrl = HistoryHash.getUrl();
575                 _yuitest_coverline("build/history-hash/history-hash.js", 440);
576 facade = {
577                     oldHash: oldHash,
578                     oldUrl : oldUrl,
579                     newHash: newHash,
580                     newUrl : newUrl
581                 };
583                 _yuitest_coverline("build/history-hash/history-hash.js", 447);
584 oldHash = newHash;
585                 _yuitest_coverline("build/history-hash/history-hash.js", 448);
586 oldUrl  = newUrl;
588                 _yuitest_coverline("build/history-hash/history-hash.js", 450);
589 YArray.each(hashNotifiers.concat(), function (notifier) {
590                     _yuitest_coverfunc("build/history-hash/history-hash.js", "(anonymous 8)", 450);
591 _yuitest_coverline("build/history-hash/history-hash.js", 451);
592 notifier.fire(facade);
593                 });
594             }
595         }, null, true);
596     }
599 _yuitest_coverline("build/history-hash/history-hash.js", 458);
600 Y.HistoryHash = HistoryHash;
602 // HistoryHash will never win over HistoryHTML5 unless useHistoryHTML5 is false.
603 _yuitest_coverline("build/history-hash/history-hash.js", 461);
604 if (useHistoryHTML5 === false || (!Y.History && useHistoryHTML5 !== true &&
605         (!HistoryBase.html5 || !Y.HistoryHTML5))) {
606     _yuitest_coverline("build/history-hash/history-hash.js", 463);
607 Y.History = HistoryHash;
611 }, '3.7.1', {"requires": ["event-synthetic", "history-base", "yui-later"]});