Skip to content

Commit

Permalink
Improve set-attr scriptlet
Browse files Browse the repository at this point in the history
Added true and false values
  • Loading branch information
AdamWr committed Feb 6, 2023
1 parent 641057e commit b1932eb
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/scriptlets/set-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
* - `value` — the value to assign to the attribute, defaults to ''. Possible values:
* - `''` - empty string
* - positive decimal integer `<= 32767`
* - `true`
* - `false`
*
* **Examples**
* 1. Set attribute by selector
Expand Down Expand Up @@ -56,11 +58,16 @@ export function setAttr(source, selector, attr, value = '') {
if (!selector || !attr) {
return;
}

const allowedValues = ['true', 'false'];

// Drop strings that cant be parsed into number, negative numbers and numbers below 32767
if (value.length !== 0
&& (nativeIsNaN(parseInt(value, 10))
|| parseInt(value, 10) < 0
|| parseInt(value, 10) > 32767)) {
|| parseInt(value, 10) > 32767)
&& !allowedValues.includes(value.toLowerCase())
) {
return;
}

Expand Down
85 changes: 85 additions & 0 deletions tests/scriptlets/set-attr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,88 @@ test('selector + attr + too big of a number', (assert) => {
// Clean up test elements
matchElem.remove();
});

test('selector + attr + true', (assert) => {
createHit();
const attr = 'test-attr-true';
const value = 'true';
const matchClassName = 'testClassTrue';
const mismatchClassName = 'none';

const matchElem = createElem(matchClassName);
const mismatchElem = createElem(mismatchClassName);

const scriptletArgs = [`.${matchClassName}`, attr, value];
runScriptlet(name, scriptletArgs);

assert.ok(matchElem.getAttribute(attr), `Attr ${attr} added to selector-matched element`);
assert.ok(matchElem.getAttribute(attr) === value, `New attr value ${value} is correct`);
assert.notOk(mismatchElem.getAttribute(attr), `Attr ${attr} is not added to mismatch element`);

assert.strictEqual(window.hit, 'FIRED');
clearGlobalProps('hit');

const done = assert.async();

changeAttr(matchElem, attr);
setTimeout(() => {
assert.ok(matchElem.getAttribute(attr) === value, `New attr val ${value} is still correct`);
assert.strictEqual(window.hit, 'FIRED');
// Clean up test elements
matchElem.remove();
mismatchElem.remove();
done();
}, 30);
});

test('selector + attr + False', (assert) => {
createHit();
const attr = 'test-attr-False';
const value = 'False';
const matchClassName = 'testClassFalse';
const mismatchClassName = 'none';

const matchElem = createElem(matchClassName);
const mismatchElem = createElem(mismatchClassName);

const scriptletArgs = [`.${matchClassName}`, attr, value];
runScriptlet(name, scriptletArgs);

assert.ok(matchElem.getAttribute(attr), `Attr ${attr} added to selector-matched element`);
assert.ok(matchElem.getAttribute(attr) === value, `New attr value ${value} is correct`);
assert.notOk(mismatchElem.getAttribute(attr), `Attr ${attr} is not added to mismatch element`);

assert.strictEqual(window.hit, 'FIRED');
clearGlobalProps('hit');

const done = assert.async();

changeAttr(matchElem, attr);
setTimeout(() => {
assert.ok(matchElem.getAttribute(attr) === value, `New attr val ${value} is still correct`);
assert.strictEqual(window.hit, 'FIRED');
// Clean up test elements
matchElem.remove();
mismatchElem.remove();
done();
}, 30);
});

test('selector + attr + not allowed string', (assert) => {
createHit();
const attr = 'test-attr';
const value = 'trueNotAllowed';
const matchClassName = 'testClassNotAllowed';

const matchElem = createElem(matchClassName);

const scriptletArgs = [`.${matchClassName}`, attr, value];
runScriptlet(name, scriptletArgs);

assert.notOk(matchElem.getAttribute(attr), `Attr ${attr} is not added`);

assert.strictEqual(window.hit, undefined, 'hit should not be fired');
clearGlobalProps('hit');
// Clean up test elements
matchElem.remove();
});

0 comments on commit b1932eb

Please sign in to comment.