From f167fa62f9c35c3e0a4e42a9444601c51a1e8b70 Mon Sep 17 00:00:00 2001
From: Mike Shiyan <dev@mike.pp.ua>
Date: Fri, 4 Dec 2015 13:30:00 +0200
Subject: [PATCH] Issue #2623122 by pingwin4eg: Workaround jQuery.cookie()
 inconsistency

---
 styleswitcher.js | 50 +++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 43 insertions(+), 7 deletions(-)

diff --git a/styleswitcher.js b/styleswitcher.js
index e09b713..64690ea 100644
--- a/styleswitcher.js
+++ b/styleswitcher.js
@@ -8,6 +8,47 @@
 Drupal.styleSwitcher = Drupal.styleSwitcher || {};
 
 /**
+ * Gets the style active for current user or saves the style key to the cookie.
+ *
+ * This function is used instead of jQuery.cookie() because the latest behaves
+ * differently in different versions and may break Style Switcher on certain
+ * sites: the Drupal 7 bundled jquery.cookie.js (is very old) does not encode
+ * cookie names; but jQuery Update module and Drupal 8 both provide newer
+ * version of the plugin which does encode cookie names.
+ *
+ * @param styleName
+ *   (optional) Style key to save.
+ *
+ * @return
+ *   The key of active style in case if styleName is not provided and
+ *   corresponding cookie exists in visitor's browser.
+ */
+Drupal.styleSwitcher.cookie = function (styleName) {
+  var key = 'styleswitcher[' + Drupal.settings.styleSwitcher.theme + ']';
+
+  if (typeof styleName != 'undefined') {
+    var expires = new Date();
+    expires.setTime(expires.getTime() + (Drupal.settings.styleSwitcher.cookieExpire * 1000));
+
+    // Follow PHP way: do not encode cookie name but encode the value. This
+    // consistency must be preserved because both PHP and JS are used for work
+    // with styleswitcher cookies.
+    document.cookie = key + '=' + encodeURIComponent(styleName) + '; expires=' + expires.toUTCString() + '; path=' + Drupal.settings.basePath;
+  }
+  else if (document.cookie) {
+    var cookies = document.cookie.split('; ');
+
+    for (var i = 0; i < cookies.length; i++) {
+      var parts = cookies[i].split('=');
+
+      if (parts[0] == key) {
+        return decodeURIComponent(parts[1]);
+      }
+    }
+  }
+};
+
+/**
  * Given the style object, switches stylesheets.
  *
  * @param style
@@ -15,11 +56,7 @@ Drupal.styleSwitcher = Drupal.styleSwitcher || {};
  */
 Drupal.styleSwitcher.switchStyle = function (style) {
   // Update the cookie first.
-  $.cookie('styleswitcher[' + style.theme + ']', style.name, {
-    path: Drupal.settings.basePath,
-    // Number of days the cookie must live.
-    expires: Drupal.settings.styleSwitcher.cookieExpire / 86400
-  });
+  Drupal.styleSwitcher.cookie(style.name);
 
   // Now switch the stylesheet. Path is absolute URL with scheme.
   $('#styleswitcher-css').attr('href', style.path);
@@ -68,8 +105,7 @@ Drupal.styleSwitcher.killOverlay = function () {
 Drupal.behaviors.styleSwitcher = {
   attach: function (context, settings) {
     // Set active link. It is not set from PHP, because of pages caching.
-    var theme = settings.styleSwitcher.theme;
-    var activeStyle = $.cookie('styleswitcher[' + theme + ']') || settings.styleSwitcher.default;
+    var activeStyle = Drupal.styleSwitcher.cookie() || settings.styleSwitcher.default;
     Drupal.styleSwitcher.switchActiveLink(activeStyle);
 
     $('.style-switcher', context).once('styleswitcher').click(function () {
-- 
1.9.4.msysgit.1

