Skip to content

Commit

Permalink
Fix set-attr scriptlet — value cannot be set to 0 and 32767. #425
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit c576343
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Apr 26 14:49:02 2024 +0200

    Update changelog

commit 701c536
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Apr 26 11:25:34 2024 +0200

    Fix typo

commit 68661c9
Author: Adam Wróblewski <adam@adguard.com>
Date:   Fri Apr 26 11:21:37 2024 +0200

    Fix set-attr - value cannot be set to 0 and 32767
  • Loading branch information
AdamWr committed May 6, 2024
1 parent f939ce1 commit 23217f6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic

- `ruleText` option in the `IConfiguration`

### Fixed

- `set-attr` value cannot be set to minimum `0` and maximum `32767` possible value [#425]

[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v1.10.25...HEAD
[#425]: https://github.com/AdguardTeam/Scriptlets/issues/425
[#420]: https://github.com/AdguardTeam/Scriptlets/issues/420
[#382]: https://github.com/AdguardTeam/Scriptlets/issues/382

Expand Down
4 changes: 2 additions & 2 deletions src/scriptlets/set-attr.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ export function setAttr(source, selector, attr, value = '') {

const isValidValue = value.length === 0
|| (!nativeIsNaN(parseInt(value, 10))
&& parseInt(value, 10) > 0
&& parseInt(value, 10) < 32767)
&& parseInt(value, 10) >= 0
&& parseInt(value, 10) <= 32767)
|| allowedValues.includes(value.toLowerCase());

if (!shouldCopyValue && !isValidValue) {
Expand Down
32 changes: 32 additions & 0 deletions tests/scriptlets/set-attr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ test('selector + attr + eligible number', (assert) => {
}, 30);
});

test('selector + attr + 0 (minimum possible value)', (assert) => {
const value = '0';
const { targetSelector, targetElem, mismatchElem } = context;
const scriptletArgs = [targetSelector, TARGET_ATTR_NAME, value];

runScriptlet(name, scriptletArgs);

assert.strictEqual(targetElem.getAttribute(TARGET_ATTR_NAME), value, `New attr value ${value} is correct`);
assert.strictEqual(
mismatchElem.getAttribute(TARGET_ATTR_NAME),
null,
`Attr ${TARGET_ATTR_NAME} is not added to mismatch element`,
);
assert.strictEqual(window.hit, 'FIRED', 'hit function has been called');
});

test('selector + attr + 32767 (maximum possible value)', (assert) => {
const value = '32767';
const { targetSelector, targetElem, mismatchElem } = context;
const scriptletArgs = [targetSelector, TARGET_ATTR_NAME, value];

runScriptlet(name, scriptletArgs);

assert.strictEqual(targetElem.getAttribute(TARGET_ATTR_NAME), value, `New attr value ${value} is correct`);
assert.strictEqual(
mismatchElem.getAttribute(TARGET_ATTR_NAME),
null,
`Attr ${TARGET_ATTR_NAME} is not added to mismatch element`,
);
assert.strictEqual(window.hit, 'FIRED', 'hit function has been called');
});

test('selector + attr + empty string', (assert) => {
const value = '';
const { targetSelector, targetElem, mismatchElem } = context;
Expand Down

0 comments on commit 23217f6

Please sign in to comment.