From 0bc5d92f7a40be31dd4f1315bd35023fd8d0375f Mon Sep 17 00:00:00 2001 From: Slava Leleka Date: Thu, 27 Oct 2022 15:01:17 +0300 Subject: [PATCH] AG-17113 add path parameter to set cookie scriptlets Squashed commit of the following: commit 055f551b9f168b1e507a2e8f8bfe3a32c9c01f65 Author: Slava Leleka Date: Thu Oct 27 14:26:42 2022 +0300 disallow to set unsupported cookie path commit 76ffe8f67e27ae8cefe237e94d27f4171d884710 Author: Slava Leleka Date: Thu Oct 27 14:24:31 2022 +0300 fix tests/helpers/index imports commit cdab0fe3cd88a39897231e3903808752672ffcbc Author: Slava Leleka Date: Tue Oct 25 19:23:57 2022 +0300 fix description for prepareCookie path arg commit 06f1d03a91d3f2539fab6c09e2c2683eb89e9d69 Author: Slava Leleka Date: Tue Oct 25 19:12:40 2022 +0300 fix description for prepareCookie path arg commit ac00fb121d657ea33cf2979d7b23a6795a33e27d Merge: d435aac e05fefc Author: Slava Leleka Date: Tue Oct 25 19:11:48 2022 +0300 Merge branch 'fix/AG-17113' into fix/AG-17113_01 commit e05fefc8317ba3e5fc3c2f85478e400ee8f97f7c Author: Slava Leleka Date: Tue Oct 25 15:26:21 2022 +0300 add path parameter to set cookie scriptlets --- src/helpers/cookie-utils.js | 19 +++++++++++++++++-- src/scriptlets/set-cookie-reload.js | 14 ++++++++++---- src/scriptlets/set-cookie.js | 15 ++++++++++----- tests/helpers/index.test.js | 10 +++++----- wiki/about-scriptlets.md | 21 ++++++++++++++++----- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/helpers/cookie-utils.js b/src/helpers/cookie-utils.js index 447d6f077..13766524e 100644 --- a/src/helpers/cookie-utils.js +++ b/src/helpers/cookie-utils.js @@ -1,15 +1,19 @@ import { nativeIsNaN } from './number-utils'; + /** * Prepares cookie string if given parameters are ok * @param {string} name cookie name to set * @param {string} value cookie value to set + * @param {string} path cookie path to set, 'none' for no path * @returns {string|null} cookie string if ok OR null if not */ -export const prepareCookie = (name, value) => { +export const prepareCookie = (name, value, path) => { if (!name || !value) { return null; } + const log = console.log.bind(console); // eslint-disable-line no-console + let valueToSet; if (value === 'true') { valueToSet = 'true'; @@ -34,16 +38,27 @@ export const prepareCookie = (name, value) => { } else if (/^\d+$/.test(value)) { valueToSet = parseFloat(value); if (nativeIsNaN(valueToSet)) { + log(`Invalid cookie value: '${value}'`); return null; } if (Math.abs(valueToSet) < 0 || Math.abs(valueToSet) > 15) { + log(`Invalid cookie value: '${value}'`); return null; } } else { return null; } - const pathToSet = 'path=/;'; + let pathToSet; + if (path === '/') { + pathToSet = 'path=/'; + } else if (path === 'none') { + pathToSet = ''; + } else { + log(`Invalid cookie path: '${path}'`); + return null; + } + // eslint-disable-next-line max-len const cookieData = `${encodeURIComponent(name)}=${encodeURIComponent(valueToSet)}; ${pathToSet}`; diff --git a/src/scriptlets/set-cookie-reload.js b/src/scriptlets/set-cookie-reload.js index 97a14c755..037c589d7 100644 --- a/src/scriptlets/set-cookie-reload.js +++ b/src/scriptlets/set-cookie-reload.js @@ -8,12 +8,13 @@ import { * @scriptlet set-cookie-reload * * @description - * Sets a cookie with the specified name and value, and then reloads the current page. + * Sets a cookie with the specified name and value, and path, + * and reloads the current page after the cookie setting. * If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet. * * **Syntax** * ``` - * example.org#%#//scriptlet('set-cookie-reload', name, value) + * example.org#%#//scriptlet('set-cookie-reload', name, value[, path]) * ``` * * - `name` - required, cookie name to be set @@ -25,15 +26,20 @@ import { * - `yes` / `Yes` / `Y` * - `no` * - `ok` / `OK` + * - `path` - optional, cookie path, defaults to `/`; possible values: + * - `/` — root path + * - `none` — to set no path at all * * **Examples** * ``` * example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok') * * example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') + * + * example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none') * ``` */ -export function setCookieReload(source, name, value) { +export function setCookieReload(source, name, value, path = '/') { const isCookieSetWithValue = (name, value) => { return document.cookie.split(';') .some((cookieStr) => { @@ -52,7 +58,7 @@ export function setCookieReload(source, name, value) { return; } - const cookieData = prepareCookie(name, value); + const cookieData = prepareCookie(name, value, path); if (cookieData) { document.cookie = cookieData; diff --git a/src/scriptlets/set-cookie.js b/src/scriptlets/set-cookie.js index d6f23ad75..441b368eb 100644 --- a/src/scriptlets/set-cookie.js +++ b/src/scriptlets/set-cookie.js @@ -5,11 +5,11 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index'; * @scriptlet set-cookie * * @description - * Sets a cookie with the specified name and value. Cookie path defaults to root. + * Sets a cookie with the specified name, value, and path. * * **Syntax** * ``` - * example.org#%#//scriptlet('set-cookie', name, value) + * example.org#%#//scriptlet('set-cookie', name, value[, path]) * ``` * * - `name` - required, cookie name to be set @@ -21,17 +21,22 @@ import { hit, nativeIsNaN, prepareCookie } from '../helpers/index'; * - `yes` / `Yes` / `Y` * - `no` * - `ok` / `OK` + * - `path` - optional, cookie path, defaults to `/`; possible values: + * - `/` — root path + * - `none` — to set no path at all * * **Examples** * ``` - * example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1') + * example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1') * * example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true') + * + * example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none') * ``` */ /* eslint-enable max-len */ -export function setCookie(source, name, value) { - const cookieData = prepareCookie(name, value); +export function setCookie(source, name, value, path = '/') { + const cookieData = prepareCookie(name, value, path); if (cookieData) { hit(source); diff --git a/tests/helpers/index.test.js b/tests/helpers/index.test.js index b606a3cdf..d5adb43a6 100644 --- a/tests/helpers/index.test.js +++ b/tests/helpers/index.test.js @@ -1,5 +1,5 @@ -import './get-number-from-string.test'; -import './match-stack-trace.test'; -import './noop-promise-resolve.test'; -import './parse-match-props.test'; -import './to-regexp.test'; +import './fetch-utils.test'; +import './match-stack.test'; +import './noop.test'; +import './number-utils.test'; +import './string-utils.test'; diff --git a/wiki/about-scriptlets.md b/wiki/about-scriptlets.md index ac43183ba..d7f769e3a 100644 --- a/wiki/about-scriptlets.md +++ b/wiki/about-scriptlets.md @@ -1557,12 +1557,13 @@ example.org#%#//scriptlet('set-constant', 'document.third', 'trueFunc', 'checkin ### ⚡️ set-cookie-reload -Sets a cookie with the specified name and value, and then reloads the current page. +Sets a cookie with the specified name and value, and path, +and reloads the current page after the cookie setting. If reloading option is not needed, use [set-cookie](#set-cookie) scriptlet. **Syntax** ``` -example.org#%#//scriptlet('set-cookie-reload', name, value) +example.org#%#//scriptlet('set-cookie-reload', name, value[, path]) ``` - `name` - required, cookie name to be set @@ -1574,12 +1575,17 @@ example.org#%#//scriptlet('set-cookie-reload', name, value) - `yes` / `Yes` / `Y` - `no` - `ok` / `OK` +- `path` - optional, cookie path, defaults to `/`; possible values: + - `/` — root path + - `none` — to set no path at all **Examples** ``` example.org#%#//scriptlet('set-cookie-reload', 'checking', 'ok') example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') + +example.org#%#//scriptlet('set-cookie-reload', 'cookie-set', 'true', 'none') ``` [Scriptlet source](../src/scriptlets/set-cookie-reload.js) @@ -1587,11 +1593,11 @@ example.org#%#//scriptlet('set-cookie-reload', 'gdpr-settings-cookie', '1') ### ⚡️ set-cookie -Sets a cookie with the specified name and value. Cookie path defaults to root. +Sets a cookie with the specified name, value, and path. **Syntax** ``` -example.org#%#//scriptlet('set-cookie', name, value) +example.org#%#//scriptlet('set-cookie', name, value[, path]) ``` - `name` - required, cookie name to be set @@ -1603,12 +1609,17 @@ example.org#%#//scriptlet('set-cookie', name, value) - `yes` / `Yes` / `Y` - `no` - `ok` / `OK` +- `path` - optional, cookie path, defaults to `/`; possible values: + - `/` — root path + - `none` — to set no path at all **Examples** ``` -example.org#%#//scriptlet('set-cookie', 'ReadlyCookieConsent', '1') +example.org#%#//scriptlet('set-cookie', 'CookieConsent', '1') example.org#%#//scriptlet('set-cookie', 'gdpr-settings-cookie', 'true') + +example.org#%#//scriptlet('set-cookie', 'cookie_consent', 'ok', 'none') ``` [Scriptlet source](../src/scriptlets/set-cookie.js)