Skip to content

Commit

Permalink
update js-cookie to v2.2.0 (was 2.1.3) (#1)
Browse files Browse the repository at this point in the history
* update js-cookie to v2.2.0 (was 2.1.3)

* Update Readme, with explanation and js.cookies version
  • Loading branch information
agustibr committed Feb 25, 2019
1 parent 77de1d5 commit 2a1d499
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 67 deletions.
10 changes: 6 additions & 4 deletions README.md
Expand Up @@ -2,6 +2,8 @@

[![Gem Version](https://badge.fury.io/rb/cookiesalert.svg)](https://badge.fury.io/rb/cookiesalert)

Easily add a Cookie consent banner, upon the user’s first visit to the site, to alert the users about the cookies and get consent for setting them.

## Installation

Add this line to your application's Gemfile:
Expand Down Expand Up @@ -29,7 +31,7 @@ $( document ).ready(function() {
});
```

Javascript optional parameters for select "#text_div" and add listener in "#button_confirm" for accept cookies:
Javascript optional parameters to select "#text_div" and add listener in "#button_confirm" to accept cookies:
```js
cookies.check({
div : '#div',
Expand All @@ -47,12 +49,12 @@ Add optional styles in "application.css":

Import optional view layout:
```erb
<%= render "cookies/alert", :advice => "Cookies Text", :link => "Link Advice", :button => "Button" %>
<%= render "cookies/alert", advice: "Cookies Text", link: "Link Advice", button: "Button" %>
```

Render optional parameters:
```erb
<%= render "cookies/alert", :advice => "Cookies Text", :link => link_to('Link Advice', root_path, target: '_blank'), :button => image_tag('cross_cookies.svg') %>
<%= render "cookies/alert", advice: "Cookies Text", link: link_to('Link Advice', root_path, target: '_blank'), button: image_tag('cross_cookies.svg') %>
```

## Used Javascript Linter
Expand Down Expand Up @@ -87,7 +89,7 @@ module.exports = {
```

## Dependences included:
-[JavaScript Cookie v2.1.3](https://github.com/js-cookie/js-cookie)
- [JavaScript Cookie v2.2.0](https://github.com/js-cookie/js-cookie)

# Contributing

Expand Down
131 changes: 69 additions & 62 deletions app/assets/javascripts/cookiesalert/cookie.js
@@ -1,12 +1,12 @@
/*!
* JavaScript Cookie v2.1.3
* JavaScript Cookie v2.2.0
* https://github.com/js-cookie/js-cookie
*
* Copyright 2006, 2015 Klaus Hartl & Fagner Brack
* Released under the MIT license
*/
;(function (factory) {
var registeredInModuleLoader = false;
var registeredInModuleLoader;
if (typeof define === 'function' && define.amd) {
define(factory);
registeredInModuleLoader = true;
Expand Down Expand Up @@ -36,117 +36,124 @@
return result;
}

function decode (s) {
return s.replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent);
}

function init (converter) {
function api (key, value, attributes) {
var result;
function api() {}

function set (key, value, attributes) {
if (typeof document === 'undefined') {
return;
}

// Write
attributes = extend({
path: '/'
}, api.defaults, attributes);

if (typeof attributes.expires === 'number') {
attributes.expires = new Date(new Date() * 1 + attributes.expires * 864e+5);
}

if (arguments.length > 1) {
attributes = extend({
path: '/'
}, api.defaults, attributes);
// We're using "expires" because "max-age" is not supported by IE
attributes.expires = attributes.expires ? attributes.expires.toUTCString() : '';

if (typeof attributes.expires === 'number') {
var expires = new Date();
expires.setMilliseconds(expires.getMilliseconds() + attributes.expires * 864e+5);
attributes.expires = expires;
try {
var result = JSON.stringify(value);
if (/^[\{\[]/.test(result)) {
value = result;
}
} catch (e) {}

try {
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);

if (!converter.write) {
value = encodeURIComponent(String(value))
.replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent);
} else {
value = converter.write(value, key);
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;
}

key = encodeURIComponent(String(key));
key = key.replace(/%(23|24|26|2B|5E|60|7C)/g, decodeURIComponent);
key = key.replace(/[\(\)]/g, escape);

return (document.cookie = [
key, '=', value,
attributes.expires ? '; expires=' + attributes.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
attributes.path ? '; path=' + attributes.path : '',
attributes.domain ? '; domain=' + attributes.domain : '',
attributes.secure ? '; secure' : ''
].join(''));
// 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];
}

// Read
return (document.cookie = key + '=' + value + stringifiedAttributes);
}

if (!key) {
result = {};
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. Also prevents odd result when
// calling "get()"
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var rdecode = /(%[0-9A-Z]{2})+/g;
var i = 0;

for (; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var cookie = parts.slice(1).join('=');

if (cookie.charAt(0) === '"') {
if (!json && cookie.charAt(0) === '"') {
cookie = cookie.slice(1, -1);
}

try {
var name = parts[0].replace(rdecode, decodeURIComponent);
cookie = converter.read ?
converter.read(cookie, name) : converter(cookie, name) ||
cookie.replace(rdecode, decodeURIComponent);
var name = decode(parts[0]);
cookie = (converter.read || converter)(cookie, name) ||
decode(cookie);

if (this.json) {
if (json) {
try {
cookie = JSON.parse(cookie);
} catch (e) {}
}

jar[name] = cookie;

if (key === name) {
result = cookie;
break;
}

if (!key) {
result[name] = cookie;
}
} catch (e) {}
}

return result;
return key ? jar[key] : jar;
}

api.set = api;
api.set = set;
api.get = function (key) {
return api.call(api, key);
return get(key, false /* read as raw */);
};
api.getJSON = function () {
return api.apply({
json: true
}, [].slice.call(arguments));
api.getJSON = function (key) {
return get(key, true /* read as json */);
};
api.defaults = {};

api.remove = function (key, attributes) {
api(key, '', extend(attributes, {
set(key, '', extend(attributes, {
expires: -1
}));
};

api.defaults = {};

api.withConverter = init;

return api;
Expand Down
2 changes: 1 addition & 1 deletion lib/cookiesalert/version.rb
@@ -1,3 +1,3 @@
module Cookiesalert
VERSION = "0.1.1"
VERSION = "0.1.2"
end

0 comments on commit 2a1d499

Please sign in to comment.