Skip to content

Commit 66a35bf

Browse files
committed
AG-42075 Fix 'spoof-css' — DOMRect is set incorrectly. #498
Squashed commit of the following: commit e3b53b9 Merge: 38497ba ed3dcd8 Author: Adam Wróblewski <[email protected]> Date: Fri May 23 13:56:28 2025 +0200 Merge branch 'master' into fix/AG-42075 commit 38497ba Author: Slava Leleka <[email protected]> Date: Wed May 21 17:16:34 2025 +0300 Update changelog commit 861df71 Author: Adam Wróblewski <[email protected]> Date: Wed May 21 14:22:49 2025 +0200 Fix `DOMRect` in `spoof-css` scriptlet
1 parent ed3dcd8 commit 66a35bf

File tree

3 files changed

+67
-5
lines changed

3 files changed

+67
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
3333

3434
### Fixed
3535

36+
- `spoof-css` scriptlet — incorrect `DOMRect` setting [#498].
3637
- Escaping quotes in `trusted-replace-node-text` scriptlet [#440].
3738
- `trusted-suppress-native-method` scriptlet, `isMatchingSuspended` was not reset when the stack does not match,
3839
so in some cases given method was not prevented [#496].
@@ -44,6 +45,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
4445
[#477]: https://github.com/AdguardTeam/Scriptlets/issues/477
4546
[#496]: https://github.com/AdguardTeam/Scriptlets/issues/496
4647
[#497]: https://github.com/AdguardTeam/Scriptlets/issues/497
48+
[#498]: https://github.com/AdguardTeam/Scriptlets/issues/498
4749

4850
## [v2.1.7] - 2025-04-03
4951

src/scriptlets/spoof-css.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,13 @@ export function spoofCSS(source, selectors, cssPropertyName, cssPropertyValue) {
201201
}
202202

203203
const {
204-
top,
205-
bottom,
204+
x,
205+
y,
206206
height,
207207
width,
208-
left,
209-
right,
210208
} = rect;
211209

212-
const newDOMRect = new window.DOMRect(rect.x, rect.y, top, bottom, width, height, left, right);
210+
const newDOMRect = new window.DOMRect(x, y, width, height);
213211

214212
if (propToValueMap.has('top')) {
215213
setRectValue(newDOMRect, 'top', propToValueMap.get('top'));

tests/scriptlets/spoof-css.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,68 @@ test('One selector and one property - getBoundingClientRect height', (assert) =>
440440
matchStyle.remove();
441441
});
442442

443+
test('One selector and one property - check if "width" in getBoundingClientRect is set correctly', (assert) => {
444+
const EXPECTED_WIDTH = 100;
445+
const EXPECTED_VISIBILITY = 'hidden';
446+
const matchClassName = 'testClassClientRect';
447+
448+
const matchElem = createElem(matchClassName);
449+
const cssProperty = 'height: 100px !important; width: 100px !important; visibility: visible !important;';
450+
const matchStyle = addStyle(`.${matchClassName} { ${cssProperty} }`);
451+
452+
const cssNameProperty = 'visibility';
453+
const cssValueProperty = `${EXPECTED_VISIBILITY}`;
454+
455+
const scriptletArgs = [`.${matchClassName}`, cssNameProperty, cssValueProperty];
456+
runScriptlet(name, scriptletArgs);
457+
458+
const boundingClientRect = matchElem.getBoundingClientRect();
459+
const elStyleWidth = boundingClientRect.width;
460+
const elStyleVisibility = window.getComputedStyle(matchElem).visibility;
461+
462+
assert.strictEqual(elStyleWidth, EXPECTED_WIDTH, `width is set to ${EXPECTED_WIDTH}`);
463+
assert.strictEqual(elStyleVisibility, EXPECTED_VISIBILITY, `visibility is set to ${EXPECTED_VISIBILITY}`);
464+
assert.strictEqual(window.hit, 'FIRED');
465+
466+
clearGlobalProps('hit');
467+
matchElem.remove();
468+
matchStyle.remove();
469+
});
470+
471+
test('Two separated scriptlets - getBoundingClientRect - width and height', (assert) => {
472+
const EXPECTED_HEIGHT = 7000;
473+
const EXPECTED_WIDTH = 8000;
474+
const matchClassName = 'testClassClientRect';
475+
476+
const matchElem = createElem(matchClassName);
477+
const cssProperty = 'height: 100px !important; width: 100px !important;';
478+
const matchStyle = addStyle(`.${matchClassName} { ${cssProperty} }`);
479+
480+
const cssNamePropertyOne = 'height';
481+
const cssValuePropertyOne = `${EXPECTED_HEIGHT}`;
482+
483+
const scriptletArgsOne = [`.${matchClassName}`, cssNamePropertyOne, cssValuePropertyOne];
484+
runScriptlet(name, scriptletArgsOne);
485+
486+
const cssNamePropertyTwo = 'width';
487+
const cssValuePropertyTwo = `${EXPECTED_WIDTH}`;
488+
489+
const scriptletArgsTwo = [`.${matchClassName}`, cssNamePropertyTwo, cssValuePropertyTwo];
490+
runScriptlet(name, scriptletArgsTwo);
491+
492+
const boundingClientRect = matchElem.getBoundingClientRect();
493+
const elStyleHeight = boundingClientRect.height;
494+
const elStyleWidth = boundingClientRect.width;
495+
496+
assert.strictEqual(elStyleHeight, EXPECTED_HEIGHT, `height is set to ${EXPECTED_HEIGHT}`);
497+
assert.strictEqual(elStyleWidth, EXPECTED_WIDTH, `width is set to ${EXPECTED_WIDTH}`);
498+
assert.strictEqual(window.hit, 'FIRED');
499+
500+
clearGlobalProps('hit');
501+
matchElem.remove();
502+
matchStyle.remove();
503+
});
504+
443505
test('One selector and one property - getBoundingClientRect top', (assert) => {
444506
const EXPECTED_TOP = 2050;
445507
const matchClassName = 'testClassClientRect';

0 commit comments

Comments
 (0)