diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f1f1652..54655c02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/scriptlets/set-attr.js b/src/scriptlets/set-attr.js index 418c64b4..bc13701e 100644 --- a/src/scriptlets/set-attr.js +++ b/src/scriptlets/set-attr.js @@ -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) { diff --git a/tests/scriptlets/set-attr.test.js b/tests/scriptlets/set-attr.test.js index bc903f33..98ff33ff 100644 --- a/tests/scriptlets/set-attr.test.js +++ b/tests/scriptlets/set-attr.test.js @@ -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;