Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/getScrollBarSize.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ExtendCSSStyleDeclaration = CSSStyleDeclaration & {

let cached: ScrollBarSize;

function measureScrollbarSize(ele?: HTMLElement): ScrollBarSize {
function measureScrollbarSize(ele?: HTMLElement, csp?: { nonce?: string }): ScrollBarSize {
const randomId = `rc-scrollbar-measure-${Math.random()
.toString(36)
.substring(7)}`;
Expand Down Expand Up @@ -53,6 +53,7 @@ ${widthStyle}
${heightStyle}
}`,
randomId,
{ csp },
);
} catch (e) {
// Can't wrap, just log error
Expand Down Expand Up @@ -98,7 +99,7 @@ export default function getScrollBarSize(fresh?: boolean): number {
return cached.width;
}

export function getTargetScrollBarSize(target: HTMLElement) {
export function getTargetScrollBarSize(target: HTMLElement, csp?: { nonce?: string }) {
Comment thread
danielthegray marked this conversation as resolved.
if (
typeof document === 'undefined' ||
!target ||
Expand All @@ -107,5 +108,5 @@ export function getTargetScrollBarSize(target: HTMLElement) {
return { width: 0, height: 0 };
}

return measureScrollbarSize(target);
return measureScrollbarSize(target, csp);
}
22 changes: 22 additions & 0 deletions tests/getScrollBarSize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,27 @@ describe('getScrollBarSize', () => {
height: 0,
});
});

it('should pass csp nonce to updateCSS', () => {
const updateCSSSpy = jest.spyOn(
require('../src/Dom/dynamicCSS'),
'updateCSS',
);

const target = document.createElement('div');
document.body.appendChild(target);

const nonce = 'test-nonce-123';
getTargetScrollBarSize(target, { nonce });

expect(updateCSSSpy).toHaveBeenCalledWith(
expect.any(String),
expect.any(String),
{ csp: { nonce } },
);

updateCSSSpy.mockRestore();
document.body.removeChild(target);
});
});
});