Skip to content

Commit

Permalink
Merge pull request #897 from btea/task/896-document-links
Browse files Browse the repository at this point in the history
#896@minor: Add document.links.
  • Loading branch information
capricorn86 committed May 11, 2023
2 parents 359818a + ae6e4ce commit 7f5bd17
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/nodes/document/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ export default class Document extends Node implements IDocument {
}
}

/**
* Returns a collection of all area elements and a elements in a document with a value for the href attribute.
*/
public get links(): IHTMLCollection<IHTMLElement> {
return <IHTMLCollection<IHTMLElement>>this.querySelectorAll('a[href],area[href]');
}

/**
* Last element child.
*
Expand Down
1 change: 1 addition & 0 deletions packages/happy-dom/src/nodes/document/IDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export default interface IDocument extends IParentNode {
readonly documentURI: string;
readonly visibilityState: VisibilityStateEnum;
readonly hidden: boolean;
readonly links: IHTMLCollection<IHTMLElement>;
cookie: string;
title: string;

Expand Down
22 changes: 22 additions & 0 deletions packages/happy-dom/test/nodes/document/Document.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ describe('Document', () => {
});
});

describe('get links()', () => {
it('Returns a elements.', () => {
const link1 = document.createElement('a');
const link2 = document.createElement('a');
link1.setAttribute('href', '');

document.body.appendChild(link1);
document.body.appendChild(link2);

let links = document.links;

expect(links.length).toBe(1);
expect(links[0]).toBe(link1);

link2.setAttribute('href', '');
links = document.links;
expect(links.length).toBe(2);
expect(links[0]).toBe(link1);
expect(links[1]).toBe(link2);
});
});

describe('get scripts()', () => {
it('Returns script elements.', () => {
const div = document.createElement('div');
Expand Down

0 comments on commit 7f5bd17

Please sign in to comment.