Skip to content

Commit

Permalink
Merge pull request #1299 from capricorn86/1051-support-elementscrolli…
Browse files Browse the repository at this point in the history
…ntoview

feat: [#1051] Adds support for Element.scrollIntoView()
  • Loading branch information
capricorn86 authored Mar 12, 2024
2 parents 0dfe51d + c9f42fb commit 8c56b04
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
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

0 comments on commit 8c56b04

Please sign in to comment.