Skip to content

Commit

Permalink
AG-17113 add path parameter to set cookie scriptlets
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 055f551
Author: Slava Leleka <[email protected]>
Date:   Thu Oct 27 14:26:42 2022 +0300

    disallow to set unsupported cookie path

commit 76ffe8f
Author: Slava Leleka <[email protected]>
Date:   Thu Oct 27 14:24:31 2022 +0300

    fix tests/helpers/index imports

commit cdab0fe
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 25 19:23:57 2022 +0300

    fix description for prepareCookie path arg

commit 06f1d03
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 25 19:12:40 2022 +0300

    fix description for prepareCookie path arg

commit ac00fb1
Merge: d435aac e05fefc
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 25 19:11:48 2022 +0300

    Merge branch 'fix/AG-17113' into fix/AG-17113_01

commit e05fefc
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 25 15:26:21 2022 +0300

    add path parameter to set cookie scriptlets
  • Loading branch information
slavaleleka committed Oct 27, 2022
1 parent d435aac commit 0bc5d92
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
19 changes: 17 additions & 2 deletions src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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}`;

Expand Down
14 changes: 10 additions & 4 deletions src/scriptlets/set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand All @@ -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;
Expand Down
15 changes: 10 additions & 5 deletions src/scriptlets/set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions tests/helpers/index.test.js
Original file line number Diff line number Diff line change
@@ -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';
21 changes: 16 additions & 5 deletions wiki/about-scriptlets.md
Original file line number Diff line number Diff line change
Expand Up @@ -1557,12 +1557,13 @@ example.org#%#//scriptlet('set-constant', 'document.third', 'trueFunc', 'checkin
### <a id="set-cookie-reload"></a> ⚡️ 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
Expand All @@ -1574,24 +1575,29 @@ 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)
* * *
### <a id="set-cookie"></a> ⚡️ 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
Expand All @@ -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)
Expand Down

0 comments on commit 0bc5d92

Please sign in to comment.