r78103 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78102‎ | r78103 | r78104 >
Date:23:14, 8 December 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Updated cookie plugin
Modified paths:
  • /trunk/phase3/resources/jquery/jquery.cookie.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/jquery/jquery.cookie.js
@@ -1,19 +1,15 @@
2 -/*jslint browser: true */ /*global jQuery: true */
3 -
42 /**
5 - * jQuery Cookie plugin
 3+ * Cookie plugin
64 *
7 - * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 5+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
86 * Dual licensed under the MIT and GPL licenses:
97 * http://www.opensource.org/licenses/mit-license.php
108 * http://www.gnu.org/licenses/gpl.html
119 *
1210 */
1311
14 -// TODO JsDoc
15 -
1612 /**
17 - * Create a cookie with the given key and value and other optional parameters.
 13+ * Create a cookie with the given name and value and other optional parameters.
1814 *
1915 * @example $.cookie('the_cookie', 'the_value');
2016 * @desc Set the value of a cookie.
@@ -25,16 +21,16 @@
2622 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
2723 * used when the cookie was set.
2824 *
29 - * @param key String The key of the cookie.
30 - * @param value String The value of the cookie.
31 - * @param options Object An object literal containing key/value pairs to provide optional cookie attributes.
32 - * @option expires Number|Date Either an integer specifying the expiration date from now on in days or a Date object.
 25+ * @param String name The name of the cookie.
 26+ * @param String value The value of the cookie.
 27+ * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 28+ * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
3329 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
3430 * If set to null or omitted, the cookie will be a session cookie and will not be retained
3531 * when the the browser exits.
36 - * @option path String The value of the path atribute of the cookie (default: path of page that created the cookie).
37 - * @option domain String The value of the domain attribute of the cookie (default: domain of page that created the cookie).
38 - * @option secure Boolean If true, the secure attribute of the cookie will be set and the cookie transmission will
 32+ * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 33+ * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 34+ * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
3935 * require a secure protocol (like HTTPS).
4036 * @type undefined
4137 *
@@ -44,12 +40,12 @@
4541 */
4642
4743 /**
48 - * Get the value of a cookie with the given key.
 44+ * Get the value of a cookie with the given name.
4945 *
5046 * @example $.cookie('the_cookie');
5147 * @desc Get the value of a cookie.
5248 *
53 - * @param key String The key of the cookie.
 49+ * @param String name The name of the cookie.
5450 * @return The value of the cookie.
5551 * @type String
5652 *
@@ -57,33 +53,44 @@
5854 * @cat Plugins/Cookie
5955 * @author Klaus Hartl/klaus.hartl@stilbuero.de
6056 */
61 -jQuery.cookie = function (key, value, options) {
62 -
63 - // key and value given, set cookie...
64 - if (arguments.length > 1 && (value === null || typeof value !== "object")) {
65 - options = jQuery.extend({}, options);
66 -
 57+jQuery.cookie = function(name, value, options) {
 58+ if (typeof value != 'undefined') { // name and value given, set cookie
 59+ options = options || {};
6760 if (value === null) {
 61+ value = '';
6862 options.expires = -1;
6963 }
70 -
71 - if (typeof options.expires === 'number') {
72 - var days = options.expires, t = options.expires = new Date();
73 - t.setDate(t.getDate() + days);
 64+ var expires = '';
 65+ if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
 66+ var date;
 67+ if (typeof options.expires == 'number') {
 68+ date = new Date();
 69+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
 70+ } else {
 71+ date = options.expires;
 72+ }
 73+ expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
7474 }
75 -
76 - return (document.cookie = [
77 - encodeURIComponent(key), '=',
78 - options.raw ? String(value) : encodeURIComponent(String(value)),
79 - options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
80 - options.path ? '; path=' + options.path : '',
81 - options.domain ? '; domain=' + options.domain : '',
82 - options.secure ? '; secure' : ''
83 - ].join(''));
 75+ // CAUTION: Needed to parenthesize options.path and options.domain
 76+ // in the following expressions, otherwise they evaluate to undefined
 77+ // in the packed version for some reason...
 78+ var path = options.path ? '; path=' + (options.path) : '';
 79+ var domain = options.domain ? '; domain=' + (options.domain) : '';
 80+ var secure = options.secure ? '; secure' : '';
 81+ document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
 82+ } else { // only name given, get cookie
 83+ var cookieValue = null;
 84+ if (document.cookie && document.cookie != '') {
 85+ var cookies = document.cookie.split(';');
 86+ for (var i = 0; i < cookies.length; i++) {
 87+ var cookie = jQuery.trim(cookies[i]);
 88+ // Does this cookie string begin with the name we want?
 89+ if (cookie.substring(0, name.length + 1) == (name + '=')) {
 90+ cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
 91+ break;
 92+ }
 93+ }
 94+ }
 95+ return cookieValue;
8496 }
85 -
86 - // key and possibly options given, get cookie...
87 - options = value || {};
88 - var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
89 - return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
9097 };

Comments

#Comment by Krinkle (talk | contribs)   12:46, 11 December 2010
 /**
- * jQuery Cookie plugin
+ * Cookie plugin
  *
- * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  * Dual licensed under the MIT and GPL licenses:

Has this been updated from the current version of the plugin or by you personally ? Reason being that this seems to be 4 years older...

#Comment by Krinkle (talk | contribs)   12:48, 11 December 2010
 /**
- * jQuery Cookie plugin
+ * Cookie plugin
  *
- * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
+ * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
  * Dual licensed under the MIT and GPL licenses:

Has this been updated to the current version of the plugin, or did you yourself make some changes ? Reason being that this one seems to be 4 years older....

#Comment by Trevor Parscal (WMF) (talk | contribs)   15:24, 13 December 2010

The latest version is a bit old - yes, but this is one is by the same author and is slightly cleaned up from the other one we had - it was on jquery.com as the most recent release.

Status & tagging log