From d9ea373b81b41b90009e3da0de09498d0b89d908 Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Thu, 9 Jan 2020 22:15:13 +0000 Subject: [PATCH] Upgrade to the newer js-cookie library that replaced jquery.cookie. Fixes #2832. Commit ready for merge. - Legacy-Id: 17211 --- ietf/bower.json | 5 +- .../externals/static/jquery.cookie/bower.json | 18 -- .../static/jquery.cookie/jquery.cookie.js | 117 ------------- ietf/externals/static/js-cookie/bower.json | 17 ++ .../static/js-cookie/src/js.cookie.js | 163 ++++++++++++++++++ ietf/static/ietf/js/ietf.js | 6 +- ietf/templates/base.html | 2 +- ietf/templates/meeting/landscape_edit.html | 4 +- ietf/templates/meeting/room_edit.html | 4 +- ietf/templates/meeting/session_details.html | 4 +- 10 files changed, 194 insertions(+), 146 deletions(-) delete mode 100644 ietf/externals/static/jquery.cookie/bower.json delete mode 100644 ietf/externals/static/jquery.cookie/jquery.cookie.js create mode 100644 ietf/externals/static/js-cookie/bower.json create mode 100644 ietf/externals/static/js-cookie/src/js.cookie.js diff --git a/ietf/bower.json b/ietf/bower.json index 793a7f6cb..2ac6daa33 100644 --- a/ietf/bower.json +++ b/ietf/bower.json @@ -10,8 +10,8 @@ "fullcalendar": "~4", "highcharts": "~6", "html5shiv": "~3", + "js-cookie": "~2", "jquery": "~1", - "jquery.cookie": "~1", "jquery.tablesorter": "~2", "respond": "~1", "select2": "~3", @@ -56,6 +56,9 @@ "modules/offline-exporting.js", "modules/map.js" ] + }, + "js-cookie": { + "main": "src/js.cookie.js" } } } diff --git a/ietf/externals/static/jquery.cookie/bower.json b/ietf/externals/static/jquery.cookie/bower.json deleted file mode 100644 index 2d8c25b0b..000000000 --- a/ietf/externals/static/jquery.cookie/bower.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name": "jquery.cookie", - "version": "1.4.1", - "main": [ - "./jquery.cookie.js" - ], - "dependencies": { - "jquery": ">=1.2" - }, - "ignore": [ - "test", - ".*", - "*.json", - "*.md", - "*.txt", - "Gruntfile.js" - ] -} diff --git a/ietf/externals/static/jquery.cookie/jquery.cookie.js b/ietf/externals/static/jquery.cookie/jquery.cookie.js deleted file mode 100644 index c7f3a59b5..000000000 --- a/ietf/externals/static/jquery.cookie/jquery.cookie.js +++ /dev/null @@ -1,117 +0,0 @@ -/*! - * jQuery Cookie Plugin v1.4.1 - * https://github.com/carhartl/jquery-cookie - * - * Copyright 2013 Klaus Hartl - * Released under the MIT license - */ -(function (factory) { - if (typeof define === 'function' && define.amd) { - // AMD - define(['jquery'], factory); - } else if (typeof exports === 'object') { - // CommonJS - factory(require('jquery')); - } else { - // Browser globals - factory(jQuery); - } -}(function ($) { - - var pluses = /\+/g; - - function encode(s) { - return config.raw ? s : encodeURIComponent(s); - } - - function decode(s) { - return config.raw ? s : decodeURIComponent(s); - } - - function stringifyCookieValue(value) { - return encode(config.json ? JSON.stringify(value) : String(value)); - } - - function parseCookieValue(s) { - if (s.indexOf('"') === 0) { - // This is a quoted cookie as according to RFC2068, unescape... - s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\'); - } - - try { - // Replace server-side written pluses with spaces. - // If we can't decode the cookie, ignore it, it's unusable. - // If we can't parse the cookie, ignore it, it's unusable. - s = decodeURIComponent(s.replace(pluses, ' ')); - return config.json ? JSON.parse(s) : s; - } catch(e) {} - } - - function read(s, converter) { - var value = config.raw ? s : parseCookieValue(s); - return $.isFunction(converter) ? converter(value) : value; - } - - var config = $.cookie = function (key, value, options) { - - // Write - - if (value !== undefined && !$.isFunction(value)) { - options = $.extend({}, config.defaults, options); - - if (typeof options.expires === 'number') { - var days = options.expires, t = options.expires = new Date(); - t.setTime(+t + days * 864e+5); - } - - return (document.cookie = [ - encode(key), '=', stringifyCookieValue(value), - options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE - options.path ? '; path=' + options.path : '', - options.domain ? '; domain=' + options.domain : '', - options.secure ? '; secure' : '' - ].join('')); - } - - // Read - - var result = key ? undefined : {}; - - // To prevent the for loop in the first place assign an empty array - // in case there are no cookies at all. Also prevents odd result when - // calling $.cookie(). - var cookies = document.cookie ? document.cookie.split('; ') : []; - - for (var i = 0, l = cookies.length; i < l; i++) { - var parts = cookies[i].split('='); - var name = decode(parts.shift()); - var cookie = parts.join('='); - - if (key && key === name) { - // If second argument (value) is a function it's a converter... - result = read(cookie, value); - break; - } - - // Prevent storing a cookie that we couldn't decode. - if (!key && (cookie = read(cookie)) !== undefined) { - result[name] = cookie; - } - } - - return result; - }; - - config.defaults = {}; - - $.removeCookie = function (key, options) { - if ($.cookie(key) === undefined) { - return false; - } - - // Must not alter options, thus extending a fresh object... - $.cookie(key, '', $.extend({}, options, { expires: -1 })); - return !$.cookie(key); - }; - -})); diff --git a/ietf/externals/static/js-cookie/bower.json b/ietf/externals/static/js-cookie/bower.json new file mode 100644 index 000000000..9678d99ff --- /dev/null +++ b/ietf/externals/static/js-cookie/bower.json @@ -0,0 +1,17 @@ +{ + "name": "js-cookie", + "license": "MIT", + "main": [ + "src/js.cookie.js" + ], + "ignore": [ + "test", + "Gruntfile.js", + "package.json", + ".gitignore", + ".eslintintignore", + ".eslintrc", + ".tm_properties", + ".travis.yml" + ] +} diff --git a/ietf/externals/static/js-cookie/src/js.cookie.js b/ietf/externals/static/js-cookie/src/js.cookie.js new file mode 100644 index 000000000..80a755124 --- /dev/null +++ b/ietf/externals/static/js-cookie/src/js.cookie.js @@ -0,0 +1,163 @@ +/*! + * JavaScript Cookie v2.2.1 + * https://github.com/js-cookie/js-cookie + * + * Copyright 2006, 2015 Klaus Hartl & Fagner Brack + * Released under the MIT license + */ +;(function (factory) { + var registeredInModuleLoader; + if (typeof define === 'function' && define.amd) { + define(factory); + registeredInModuleLoader = true; + } + if (typeof exports === 'object') { + module.exports = factory(); + registeredInModuleLoader = true; + } + if (!registeredInModuleLoader) { + var OldCookies = window.Cookies; + var api = window.Cookies = factory(); + api.noConflict = function () { + window.Cookies = OldCookies; + return api; + }; + } +}(function () { + function extend () { + var i = 0; + var result = {}; + for (; i < arguments.length; i++) { + var attributes = arguments[ i ]; + for (var key in attributes) { + result[key] = attributes[key]; + } + } + return result; + } + + function decode (s) { + return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); + } + + function init (converter) { + function api() {} + + function set (key, value, attributes) { + if (typeof document === 'undefined') { + return; + } + + attributes = extend({ + path: '/' + }, api.defaults, attributes); + + if (typeof attributes.expires === 'number') { + attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5); + } + + // We're using "expires" because "max-age" is not supported by IE + attributes.expires = attributes.expires ? attributes.expires.toUTCString() : ''; + + try { + var result = JSON.stringify(value); + if (/^[\{\[]/.test(result)) { + value = result; + } + } catch (e) {} + + value = converter.write ? + converter.write(value, key) : + encodeURIComponent(String(value)) + .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); + + key = encodeURIComponent(String(key)) + .replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent) + .replace(/[\(\)]/g, escape); + + var stringifiedAttributes = ''; + for (var attributeName in attributes) { + if (!attributes[attributeName]) { + continue; + } + stringifiedAttributes += '; ' + attributeName; + if (attributes[attributeName] === true) { + continue; + } + + // Considers RFC 6265 section 5.2: + // ... + // 3. If the remaining unparsed-attributes contains a %x3B (";") + // character: + // Consume the characters of the unparsed-attributes up to, + // not including, the first %x3B (";") character. + // ... + stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]; + } + + return (document.cookie = key + '=' + value + stringifiedAttributes); + } + + function get (key, json) { + if (typeof document === 'undefined') { + return; + } + + var jar = {}; + // To prevent the for loop in the first place assign an empty array + // in case there are no cookies at all. + var cookies = document.cookie ? document.cookie.split('; ') : []; + var i = 0; + + for (; i < cookies.length; i++) { + var parts = cookies[i].split('='); + var cookie = parts.slice(1).join('='); + + if (!json && cookie.charAt(0) === '"') { + cookie = cookie.slice(1, -1); + } + + try { + var name = decode(parts[0]); + cookie = (converter.read || converter)(cookie, name) || + decode(cookie); + + if (json) { + try { + cookie = JSON.parse(cookie); + } catch (e) {} + } + + jar[name] = cookie; + + if (key === name) { + break; + } + } catch (e) {} + } + + return key ? jar[key] : jar; + } + + api.set = set; + api.get = function (key) { + return get(key, false /* read as raw */); + }; + api.getJSON = function (key) { + return get(key, true /* read as json */); + }; + api.remove = function (key, attributes) { + set(key, '', extend(attributes, { + expires: -1 + })); + }; + + api.defaults = {}; + + api.withConverter = init; + + return api; + } + + return init(function () {}); +})); diff --git a/ietf/static/ietf/js/ietf.js b/ietf/static/ietf/js/ietf.js index 8d4372755..bbe740c3d 100644 --- a/ietf/static/ietf/js/ietf.js +++ b/ietf/static/ietf/js/ietf.js @@ -7,7 +7,7 @@ jQuery.ajaxSetup({ crossDomain: false, // obviates need for sameOrigin test beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type)) { - xhr.setRequestHeader("X-CSRFToken", $.cookie('csrftoken')); + xhr.setRequestHeader("X-CSRFToken", Cookies.get('csrftoken')); } } }); @@ -15,10 +15,10 @@ jQuery.ajaxSetup({ // Remember the state of the "browsehappy" alert $('#browsehappy .close').click(function(e) { e.preventDefault(); - $.cookie('browsehappy', 'closed', { path: '/' }); + Cookies.set('browsehappy', 'closed'); }); -if(typeof $.cookie('browsehappy') === 'undefined') { +if(typeof Cookies.get('browsehappy') === 'undefined') { $('#browsehappy').show(); } diff --git a/ietf/templates/base.html b/ietf/templates/base.html index fd4510ca3..6b345df5a 100644 --- a/ietf/templates/base.html +++ b/ietf/templates/base.html @@ -169,7 +169,7 @@ {% endcomment %} - + {% block js %}{% endblock %} diff --git a/ietf/templates/meeting/landscape_edit.html b/ietf/templates/meeting/landscape_edit.html index 0c61fea86..91d7785e2 100644 --- a/ietf/templates/meeting/landscape_edit.html +++ b/ietf/templates/meeting/landscape_edit.html @@ -25,13 +25,13 @@ {% block js %} - + - + - +