Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [#1051] Adds support for Element.scrollIntoView() #1299

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions packages/happy-dom/src/nodes/element/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,23 @@ export default class Element extends Node implements IElement {
this.scroll(x, y);
}

/**
* Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.
*
* @param [_options] Options.
*/
public scrollIntoView(
_options?:
| boolean
| {
behavior?: 'smooth' | 'instant' | 'auto';
block?: 'start' | 'center' | 'end' | 'nearest';
inline?: 'start' | 'center' | 'end' | 'nearest';
}
): void {
// Do nothing
}

/**
* @override
*/
Expand Down
15 changes: 15 additions & 0 deletions packages/happy-dom/src/nodes/element/IElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ export default interface IElement extends IChildNode, INonDocumentTypeChildNode,
*/
scrollTo(x: { top?: number; left?: number; behavior?: string } | number, y: number): void;

/**
* Scrolls the element's ancestor containers such that the element on which scrollIntoView() is called is visible to the user.
*
* @param [options] Options.
*/
scrollIntoView(
options?:
| boolean
| {
behavior?: 'smooth' | 'instant' | 'auto';
block?: 'start' | 'center' | 'end' | 'nearest';
inline?: 'start' | 'center' | 'end' | 'nearest';
}
): void;

/**
* Returns the size of an element and its position relative to the viewport.
*
Expand Down
30 changes: 21 additions & 9 deletions packages/happy-dom/test/nodes/element/Element.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,18 @@ describe('Element', () => {
});
});

describe('get scrollHeight()', () => {
it('Returns the scroll height.', () => {
expect(element.scrollHeight).toBe(0);
});
});

describe('get scrollWidth()', () => {
it('Returns the scroll width.', () => {
expect(element.scrollWidth).toBe(0);
});
});

describe('attributeChangedCallback()', () => {
it('Calls attribute changed callback when it is implemented by a custom element (web component).', () => {
const customElement = <CustomElement>document.createElement('custom-element');
Expand Down Expand Up @@ -1516,15 +1528,15 @@ describe('Element', () => {
});
}

describe('get scrollHeight()', () => {
it('Returns the scroll height.', () => {
expect(element.scrollHeight).toBe(0);
});
});

describe('get scrollWidth()', () => {
it('Returns the scroll width.', () => {
expect(element.scrollWidth).toBe(0);
describe('scrollIntoView()', () => {
it('Does nothing as it is currently not possible to implement.', () => {
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({
block: 'start',
inline: 'start',
behavior: 'auto'
});
});
});

Expand Down
Loading