Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/capricorn86/happy-dom int…
Browse files Browse the repository at this point in the history
…o task/908-domparserparsefromstring-doesnt-decode-html-entities
  • Loading branch information
capricorn86 committed May 11, 2023
2 parents e711369 + 2291dc3 commit 5b3960a
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ export default class CSSStyleDeclarationElementStyle {
}
} else {
for (const element of options.elements) {
// Skip @-rules.
if (selectorText.startsWith('@')) {
continue;
}
const matchResult = QuerySelector.match(<IElement>element.element, selectorText);
if (matchResult) {
element.cssTexts.push({
Expand Down
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
2 changes: 1 addition & 1 deletion packages/happy-dom/src/xml-serializer/XMLSerializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export default class XMLSerializer {
}

if (this._options.includeShadowRoots && element.shadowRoot) {
innerHTML += `<template shadowroot="${element.shadowRoot.mode}">`;
innerHTML += `<template shadowrootmode="${element.shadowRoot.mode}">`;

for (const node of element.shadowRoot.childNodes) {
innerHTML += this.serializeToString(node);
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
52 changes: 26 additions & 26 deletions packages/happy-dom/test/xml-serializer/XMLSerializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,32 +154,32 @@ describe('XMLSerializer', () => {
new XMLSerializer({ includeShadowRoots: true }).serializeToString(div).replace(/[\s]/gm, '')
).toBe(
`
<div>
<custom-element key1="value1" key2="value2">
<span>Slotted content</span>
<template shadowroot="open">
<style>
:host {
display: block;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
span {
color: pink;
}
.class1 {
color: yellow;
}
</style>
<div>
<span class="class1">
key1 is "value1" and key2 is "value2".
</span>
<span><slot></slot></span>
</div>
</template>
</custom-element>
<custom-element key1="value4" key2="value5"></custom-element>
</div>`.replace(/[\s]/gm, '')
<div>
<custom-element key1="value1" key2="value2">
<span>Slotted content</span>
<template shadowrootmode="open">
<style>
:host {
display: block;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
span {
color: pink;
}
.class1 {
color: yellow;
}
</style>
<div>
<span class="class1">
key1 is "value1" and key2 is "value2".
</span>
<span><slot></slot></span>
</div>
</template>
</custom-element>
<custom-element key1="value4" key2="value5"></custom-element>
</div>`.replace(/[\s]/gm, '')
);
});

Expand Down

0 comments on commit 5b3960a

Please sign in to comment.