diff --git a/assets/cookie/cookie.js b/assets/cookie/cookie.js new file mode 100644 index 0000000..7432caa --- /dev/null +++ b/assets/cookie/cookie.js @@ -0,0 +1,127 @@ +kit.PluginManager.register('cookie', function(module, exports, require){ + + var defaultConverter = { + read: function (value) { + if (value[0] === '"') { + value = value.slice(1, -1) + } + return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent) + }, + write: function (value) { + return encodeURIComponent(value).replace( + /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, + decodeURIComponent + ) + } + }; + var assign = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i] + for (var key in source) { + target[key] = source[key] + } + } + return target + }; + function init (converter, defaultAttributes) { + function set (key, value, attributes) { + if (typeof document === 'undefined') { + return + } + + attributes = assign({}, defaultAttributes, attributes) + + if (typeof attributes.expires === 'number') { + attributes.expires = new Date(Date.now() + attributes.expires * 864e5) + } + if (attributes.expires) { + attributes.expires = attributes.expires.toUTCString() + } + + key = encodeURIComponent(key) + .replace(/%(2[346B]|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 + '=' + converter.write(value, key) + stringifiedAttributes) + } + + function get (key) { + if (typeof document === 'undefined' || (arguments.length && !key)) { + return + } + + // 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 jar = {} + for (var i = 0; i < cookies.length; i++) { + var parts = cookies[i].split('=') + var value = parts.slice(1).join('=') + + try { + var foundKey = decodeURIComponent(parts[0]) + jar[foundKey] = converter.read(value, foundKey) + + if (key === foundKey) { + break + } + } catch (e) {} + } + + return key ? jar[key] : jar + } + + return Object.create( + { + set: set, + get: get, + remove: function (key, attributes) { + set( + key, + '', + assign({}, attributes, { + expires: -1 + }) + ) + }, + withAttributes: function (attributes) { + return init(this.converter, assign({}, this.attributes, attributes)) + }, + withConverter: function (converter) { + return init(assign({}, this.converter, converter), this.attributes) + } + }, + { + attributes: { value: Object.freeze(defaultAttributes) }, + converter: { value: Object.freeze(converter) } + } + ) + } + + module.exports = init(defaultConverter, { path: '/' }); + + return module.exports; +}); diff --git a/assets/core/core.register.js b/assets/core/core.register.js index 52366af..a9b04c4 100644 --- a/assets/core/core.register.js +++ b/assets/core/core.register.js @@ -119,6 +119,11 @@ __PluginManager__.registerPlugins({ onInstall: function (plugin) { }, }, + cookie: { + path: __pmConfig__.assetsPath + '/cookie', + module: '/cookie.js', + type: 'loader', + }, }); diff --git a/assets/toolkit/toolkit.js b/assets/toolkit/toolkit.js index 6af2bb6..18c2793 100644 --- a/assets/toolkit/toolkit.js +++ b/assets/toolkit/toolkit.js @@ -25,133 +25,6 @@ kit.PluginManager.register('toolkit',function(module, exports, require) { var Config = new __Config(); - var __Cookie = function () { - - var defaultConverter = { - read: function (value) { - if (value[0] === '"') { - value = value.slice(1, -1) - } - return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent) - }, - write: function (value) { - return encodeURIComponent(value).replace( - /%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, - decodeURIComponent - ) - } - }; - var assign = function (target) { - for (var i = 1; i < arguments.length; i++) { - var source = arguments[i] - for (var key in source) { - target[key] = source[key] - } - } - return target - }; - function init (converter, defaultAttributes) { - function set (key, value, attributes) { - if (typeof document === 'undefined') { - return - } - - attributes = assign({}, defaultAttributes, attributes) - - if (typeof attributes.expires === 'number') { - attributes.expires = new Date(Date.now() + attributes.expires * 864e5) - } - if (attributes.expires) { - attributes.expires = attributes.expires.toUTCString() - } - - key = encodeURIComponent(key) - .replace(/%(2[346B]|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 + '=' + converter.write(value, key) + stringifiedAttributes) - } - - function get (key) { - if (typeof document === 'undefined' || (arguments.length && !key)) { - return - } - - // 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 jar = {} - for (var i = 0; i < cookies.length; i++) { - var parts = cookies[i].split('=') - var value = parts.slice(1).join('=') - - try { - var foundKey = decodeURIComponent(parts[0]) - jar[foundKey] = converter.read(value, foundKey) - - if (key === foundKey) { - break - } - } catch (e) {} - } - - return key ? jar[key] : jar - } - - return Object.create( - { - set: set, - get: get, - remove: function (key, attributes) { - set( - key, - '', - assign({}, attributes, { - expires: -1 - }) - ) - }, - withAttributes: function (attributes) { - return init(this.converter, assign({}, this.attributes, attributes)) - }, - withConverter: function (converter) { - return init(assign({}, this.converter, converter), this.attributes) - } - }, - { - attributes: { value: Object.freeze(defaultAttributes) }, - converter: { value: Object.freeze(converter) } - } - ) - } - - return init(defaultConverter, { path: '/' }); - }(); - var Cookie = new __Cookie(); - var __Helper = function () { function Helper() {