nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: capricorn86/happy-dom
Pull request #128 (2020-10-20 23:38:28)

#123@minor: Thanks to @beckerei we now have support for Element.match…


Patch:
diff --git a/packages/happy-dom/src/nodes/basic/element/Element.ts b/packages/happy-dom/src/nodes/basic/element/Element.ts
index 6f143c9e..e22807c5 100644
--- a/packages/happy-dom/src/nodes/basic/element/Element.ts
+++ b/packages/happy-dom/src/nodes/basic/element/Element.ts
@@ -5,6 +5,7 @@ import DOMRect from './DOMRect';
 import Range from './Range';
 import ClassList from './ClassList';
 import QuerySelector from '../../../query-selector/QuerySelector';
+import SelectorItem from '../../../query-selector/SelectorItem';
 import MutationRecord from '../../../mutation-observer/MutationRecord';
 import MutationTypeConstant from '../../../mutation-observer/MutationType';
 import NamespaceURI from '../../../html-config/NamespaceURI';
@@ -558,6 +559,16 @@ export default class Element extends Node implements IElement {
 		return new Range();
 	}
 
+	/**
+	 * The matches() method checks to see if the Element would be selected by the provided selectorString.
+	 *
+	 * @param selector Selector.
+	 * @returns "true" if matching.
+	 */
+	public matches(selector: string): boolean {
+		return new SelectorItem(selector).match(this);
+	}
+
 	/**
 	 * Query CSS selector to find matching nodes.
 	 *
diff --git a/packages/happy-dom/src/nodes/basic/element/IElement.ts b/packages/happy-dom/src/nodes/basic/element/IElement.ts
index 4245e12c..e36971fe 100644
--- a/packages/happy-dom/src/nodes/basic/element/IElement.ts
+++ b/packages/happy-dom/src/nodes/basic/element/IElement.ts
@@ -132,6 +132,14 @@ export default interface IElement extends IChildNode, INonDocumentTypeChildNode,
 	 */
 	createTextRange(): Range;
 
+	/**
+	 * The matches() method checks to see if the Element would be selected by the provided selectorString.
+	 *
+	 * @param selector Selector.
+	 * @returns "true" if matching.
+	 */
+	matches(selector: string): boolean;
+
 	/**
 	 * Returns an elements by tag name.
 	 *


Test patch:
diff --git a/packages/happy-dom/test/nodes/basic/element/Element.test.ts b/packages/happy-dom/test/nodes/basic/element/Element.test.ts
index 33bc8049..8e822d98 100644
--- a/packages/happy-dom/test/nodes/basic/element/Element.test.ts
+++ b/packages/happy-dom/test/nodes/basic/element/Element.test.ts
@@ -290,6 +290,16 @@ describe('Element', () => {
 		});
 	});
 
+	describe('matches()', () => {
+		test('Checks if the element matches a selector string.', () => {
+			const element = document.createElement('div');
+
+			element.className = 'container active';
+
+			expect(element.matches('.container.active')).toBe(true);
+		});
+	});
+
 	describe('querySelectorAll()', () => {
 		test('Query CSS selector to find matching elements.', () => {
 			const element = document.createElement('div');
Repository: capricorn86/happy-dom
Pull request #149 (2020-11-10 14:56:16)

#143@patch: Fixes bug where the selector ".class1 > *" did not work.


Patch:
diff --git a/packages/happy-dom/src/query-selector/QuerySelector.ts b/packages/happy-dom/src/query-selector/QuerySelector.ts
index 9c566860..e7bf5c66 100644
--- a/packages/happy-dom/src/query-selector/QuerySelector.ts
+++ b/packages/happy-dom/src/query-selector/QuerySelector.ts
@@ -4,7 +4,7 @@ import Document from '../nodes/document/Document';
 import Node from '../nodes/node/Node';
 import SelectorItem from './SelectorItem';
 
-const SELECTOR_PART_REGEXP = /(\[[^\]]+\]|[a-zA-Z0-9-_.#":()\]]+)|([ ,>]+)/g;
+const SELECTOR_PART_REGEXP = /(\[[^\]]+\]|[a-zA-Z0-9-_.#"*:()\]]+)|([ ,>]+)/g;
 
 // The above one seem to work fine and is faster, but this one can be useful if more rules need to be added as it is more "correct".
 // const SELECTOR_PART_REGEXP = /([a-zA-Z0-9-$.]+|\[[a-zA-Z0-9-]+\]|\[[a-zA-Z0-9$-~|^$*]+[ ]*=[ ]*"[^"]+"\])|([ ,]+)/g;


Test patch:
diff --git a/packages/happy-dom/test/query-selector/QuerySelector.test.ts b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
index 134b0224..406cbd78 100644
--- a/packages/happy-dom/test/query-selector/QuerySelector.test.ts
+++ b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
@@ -51,6 +51,15 @@ describe('QuerySelector', () => {
 			expect(elements[3]).toBe(container.children[0].children[1].children[1]);
 		});
 
+		test('Returns all elements matching ".class1 > .class1 > *".', () => {
+			const container = document.createElement('div');
+			container.innerHTML = QuerySelectorHTML;
+			const elements = container.querySelectorAll('.class1 > .class1 > *');
+			expect(elements.length).toBe(2);
+			expect(elements[0]).toBe(container.children[0].children[1].children[0]);
+			expect(elements[1]).toBe(container.children[0].children[1].children[1]);
+		});
+
 		test('Returns all elements matching "div > div > span".', () => {
 			const container = document.createElement('div');
 			container.innerHTML = QuerySelectorHTML;
Repository: capricorn86/happy-dom
Pull request #161 (2020-11-21 14:32:54)

#160@minor: Adds support for window.getComputedStyle().


Patch:
diff --git a/packages/happy-dom/src/window/Window.ts b/packages/happy-dom/src/window/Window.ts
index 60b0de87..4562a4a2 100644
--- a/packages/happy-dom/src/window/Window.ts
+++ b/packages/happy-dom/src/window/Window.ts
@@ -27,6 +27,7 @@ import File from '../file/File';
 import DOMException from '../exception/DOMException';
 import FileReader from '../file/FileReader';
 import History from '../history/History';
+import CSSStyleDeclaration from '../css/CSSStyleDeclaration';
 
 /**
  * Handles the Window.
@@ -163,11 +164,14 @@ export default class Window extends EventTarget implements NodeJS.Global {
 	/**
 	 * Returns an object containing the values of all CSS properties of an element.
 	 *
-	 * @note This method has not been implemented. It is just here for compatibility.
-	 * @returns Empty object.
+	 * @param element Element.
+	 * @returns CSS style declaration
 	 */
-	public getComputedStyle(): {} {
-		return {};
+	public getComputedStyle(element: HTMLElement): CSSStyleDeclaration {
+		if (!element.style || !element.isConnected) {
+			return new CSSStyleDeclaration();
+		}
+		return element.style;
 	}
 
 	/**


Test patch:
diff --git a/packages/happy-dom/test/window/Window.test.ts b/packages/happy-dom/test/window/Window.test.ts
index d4458bc6..b118f3f2 100644
--- a/packages/happy-dom/test/window/Window.test.ts
+++ b/packages/happy-dom/test/window/Window.test.ts
@@ -1,3 +1,4 @@
+import CSSStyleDeclaration from '../../src/css/CSSStyleDeclaration';
 import Window from '../../src/window/Window';
 
 describe('Window', () => {
@@ -7,6 +8,23 @@ describe('Window', () => {
 		window = new Window();
 	});
 
+	describe('getComputedStyle()', () => {
+		test('Returns a CSSStyleDeclaration object set in the property HTMLElement.style.', () => {
+			const element = window.document.createElement('div');
+			element.style.direction = 'rtl';
+			window.document.body.appendChild(element);
+			expect(window.getComputedStyle(element) instanceof CSSStyleDeclaration).toBe(true);
+			expect(window.getComputedStyle(element).direction).toBe('rtl');
+		});
+
+		test('Returns an empty CSSStyleDeclaration if the element is not connected to the DOM.', () => {
+			const element = window.document.createElement('div');
+			element.style.direction = 'rtl';
+			expect(window.getComputedStyle(element) instanceof CSSStyleDeclaration).toBe(true);
+			expect(window.getComputedStyle(element).direction).toBe('');
+		});
+	});
+
 	describe('setTimeout()', () => {
 		test('Sets a timeout.', done => {
 			const timeoutId = window.setTimeout(() => done());
Repository: capricorn86/happy-dom
Pull request #199 (2021-02-01 21:50:57)

#196@minor: Adds support for Node.parentElement.


Patch:
diff --git a/packages/happy-dom/src/nodes/node/INode.ts b/packages/happy-dom/src/nodes/node/INode.ts
index 0c5033b5..a37d3a9b 100644
--- a/packages/happy-dom/src/nodes/node/INode.ts
+++ b/packages/happy-dom/src/nodes/node/INode.ts
@@ -1,9 +1,11 @@
 import IEventTarget from '../../event/IEventTarget';
 import IDocument from '../document/IDocument';
+import IElement from '../element/IElement';
 
 export default interface INode extends IEventTarget {
 	readonly ownerDocument: IDocument;
 	readonly parentNode: INode;
+	readonly parentElement: IElement;
 	readonly nodeType: number;
 	readonly childNodes: INode[];
 	isConnected: boolean;
diff --git a/packages/happy-dom/src/nodes/node/Node.ts b/packages/happy-dom/src/nodes/node/Node.ts
index b890c889..17047260 100644
--- a/packages/happy-dom/src/nodes/node/Node.ts
+++ b/packages/happy-dom/src/nodes/node/Node.ts
@@ -6,6 +6,7 @@ import Event from '../../event/Event';
 import INode from './INode';
 import DOMException from '../../exception/DOMException';
 import IDocument from '../document/IDocument';
+import IElement from '../element/IElement';
 
 /**
  * Node
@@ -164,6 +165,19 @@ export default class Node extends EventTarget implements INode {
 		return null;
 	}
 
+	/**
+	 * Returns parent element.
+	 *
+	 * @return Element.
+	 */
+	public get parentElement(): IElement {
+		let parent = this.parentNode;
+		while (parent && parent.nodeType !== Node.ELEMENT_NODE) {
+			parent = this.parentNode;
+		}
+		return <IElement>parent;
+	}
+
 	/**
 	 * Connected callback.
 	 */


Test patch:
diff --git a/packages/happy-dom/test/nodes/node/Node.test.ts b/packages/happy-dom/test/nodes/node/Node.test.ts
index c24f59e4..41f5b21d 100644
--- a/packages/happy-dom/test/nodes/node/Node.test.ts
+++ b/packages/happy-dom/test/nodes/node/Node.test.ts
@@ -126,6 +126,25 @@ describe('Node', () => {
 		});
 	});
 
+	describe('get parentElement()', () => {
+		test('Returns parent element.', () => {
+			const div = document.createElement('div');
+			const span1 = document.createElement('span');
+			const text = document.createTextNode('text');
+
+			span1.appendChild(text);
+			div.appendChild(span1);
+
+			expect(text.parentElement).toBe(span1);
+		});
+
+		test('Returns null if there is no parent node.', () => {
+			const text = document.createTextNode('text');
+
+			expect(text.parentElement).toBe(null);
+		});
+	});
+
 	describe('connectedCallback()', () => {
 		test('Calls connected callback when a custom element is connected to DOM.', () => {
 			const customElement = document.createElement('custom-element');
Repository: capricorn86/happy-dom
Pull request #205 (2021-02-08 23:21:59)

#202@patch: Bugfix for query selector for h1 elements not working.


Patch:
diff --git a/packages/happy-dom/src/query-selector/SelectorItem.ts b/packages/happy-dom/src/query-selector/SelectorItem.ts
index 12466564..4e26beae 100644
--- a/packages/happy-dom/src/query-selector/SelectorItem.ts
+++ b/packages/happy-dom/src/query-selector/SelectorItem.ts
@@ -5,7 +5,7 @@ const ATTRIBUTE_REGEXP = /\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)([~|^$*]{0,1})[
 const ATTRIBUTE_NAME_REGEXP = /[^a-zA-Z0-9-$]/;
 const PSUEDO_REGEXP = /:([a-zA-Z-]+)\(([0-9n+-]+|odd|even)\)|:([a-zA-Z-]+)/g;
 const CLASS_REGEXP = /\.([a-zA-Z0-9-_$]+)/g;
-const TAG_NAME_REGEXP = /^[a-zA-Z-]+/;
+const TAG_NAME_REGEXP = /^[a-zA-Z0-9-]+/;
 
 export default class SelectorItem {
 	public isAll: boolean;


Test patch:
diff --git a/packages/happy-dom/test/query-selector/QuerySelector.test.ts b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
index 1ca79ba0..39dd2cdf 100644
--- a/packages/happy-dom/test/query-selector/QuerySelector.test.ts
+++ b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
@@ -20,10 +20,10 @@ describe('QuerySelector', () => {
 			expect(elements[1]).toBe(container.children[0].children[1].children[1]);
 		});
 
-		test('Returns all b (bold) elements.', () => {
+		test('Returns all h1 (heading 1) elements.', () => {
 			const container = document.createElement('div');
 			container.innerHTML = QuerySelectorHTML;
-			const elements = container.querySelectorAll('b');
+			const elements = container.querySelectorAll('h1');
 			expect(elements.length).toBe(2);
 			expect(elements[0]).toBe(container.children[0].children[0]);
 			expect(elements[1]).toBe(container.children[1].children[0]);
@@ -221,7 +221,7 @@ describe('QuerySelector', () => {
 		test('Returns all elements with an attribute value that contains a specified value using "[class*="s1 cl"]" or has the tag "b".', () => {
 			const container = document.createElement('div');
 			container.innerHTML = QuerySelectorHTML;
-			const elements = container.querySelectorAll('[class*="s1 cl"], b');
+			const elements = container.querySelectorAll('[class*="s1 cl"], h1');
 
 			expect(elements.length).toBe(6);
 			expect(elements[0]).toBe(container.children[0]);
diff --git a/packages/happy-dom/test/query-selector/data/QuerySelectorHTML.ts b/packages/happy-dom/test/query-selector/data/QuerySelectorHTML.ts
index b5cc4b80..947c91e1 100644
--- a/packages/happy-dom/test/query-selector/data/QuerySelectorHTML.ts
+++ b/packages/happy-dom/test/query-selector/data/QuerySelectorHTML.ts
@@ -1,7 +1,7 @@
 export default `
 	<div class="class1 class2">
 		<!-- Comment 1 !-->
-		<b>Bold</b>
+		<h1>Heading1</h1>
 		<!-- Comment 2 !-->
 		<div class="class1 class2">
 			<span class="class1 class2" attr1="value1" attr2="word1 word2" attr3="bracket[]bracket">Span1</span>
@@ -10,7 +10,7 @@ export default `
 	</div>
 	<div>
 		<!-- Comment 1 !-->
-		<b>Bold</b>
+		<h1>Heading1</h1>
 		<!-- Comment 2 !-->
 	</div>
 `.trim();
Repository: capricorn86/happy-dom
Pull request #260 (2021-04-28 18:51:09)

#250@patch: Makes it possible to use underscores (_) in query selectors.


Patch:
diff --git a/packages/happy-dom/src/query-selector/SelectorItem.ts b/packages/happy-dom/src/query-selector/SelectorItem.ts
index 4e26beae..f5a773f0 100644
--- a/packages/happy-dom/src/query-selector/SelectorItem.ts
+++ b/packages/happy-dom/src/query-selector/SelectorItem.ts
@@ -2,7 +2,7 @@ import DOMException from '../exception/DOMException';
 import Element from '../nodes/element/Element';
 
 const ATTRIBUTE_REGEXP = /\[([a-zA-Z0-9-_]+)\]|\[([a-zA-Z0-9-_]+)([~|^$*]{0,1})[ ]*=[ ]*["']{0,1}([^"']+)["']{0,1}\]/g;
-const ATTRIBUTE_NAME_REGEXP = /[^a-zA-Z0-9-$]/;
+const ATTRIBUTE_NAME_REGEXP = /[^a-zA-Z0-9-_$]/;
 const PSUEDO_REGEXP = /:([a-zA-Z-]+)\(([0-9n+-]+|odd|even)\)|:([a-zA-Z-]+)/g;
 const CLASS_REGEXP = /\.([a-zA-Z0-9-_$]+)/g;
 const TAG_NAME_REGEXP = /^[a-zA-Z0-9-]+/;


Test patch:
diff --git a/packages/happy-dom/test/query-selector/QuerySelector.test.ts b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
index f1900d90..0c8a2cd3 100644
--- a/packages/happy-dom/test/query-selector/QuerySelector.test.ts
+++ b/packages/happy-dom/test/query-selector/QuerySelector.test.ts
@@ -136,6 +136,16 @@ describe('QuerySelector', () => {
 			expect(elements[1]).toBe(container.children[0].children[1].children[1]);
 		});
 
+		test('Returns all elements with tag name and matching attributes using "span[_attr1]".', () => {
+			const container = document.createElement('div');
+			container.innerHTML = QuerySelectorHTML.replace(/ attr1/gm, '_attr1');
+			const elements = container.querySelectorAll('span[_attr1]');
+
+			expect(elements.length).toBe(2);
+			expect(elements[0]).toBe(container.children[0].children[1].children[0]);
+			expect(elements[1]).toBe(container.children[0].children[1].children[1]);
+		});
+
 		test('Returns all elements with tag name and multiple matching attributes using "span[attr1="value1"][attr2="word1 word2"]".', () => {
 			const container = document.createElement('div');
 			container.innerHTML = QuerySelectorHTML;
Repository: capricorn86/happy-dom
Pull request #292 (2021-09-25 14:11:10)

#290@patch: The method window.customElements.get() should return unde…
…fined when an element tag name has not been defined.


Patch:
diff --git a/packages/happy-dom/src/custom-element/CustomElementRegistry.ts b/packages/happy-dom/src/custom-element/CustomElementRegistry.ts
index 07d2976e..53184e96 100644
--- a/packages/happy-dom/src/custom-element/CustomElementRegistry.ts
+++ b/packages/happy-dom/src/custom-element/CustomElementRegistry.ts
@@ -57,7 +57,7 @@ export default class CustomElementRegistry {
 	 */
 	public get(tagName: string): typeof HTMLElement {
 		const name = tagName.toLowerCase();
-		return this._registry[name] ? this._registry[name].elementClass : null;
+		return this._registry[name] ? this._registry[name].elementClass : undefined;
 	}
 
 	/**


Test patch:
diff --git a/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts b/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts
index 839f3296..c347d432 100644
--- a/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts
+++ b/packages/happy-dom/test/custom-element/CustomElementRegistry.test.ts
@@ -41,6 +41,17 @@ describe('CustomElementRegistry', () => {
 		});
 	});
 
+	describe('get()', () => {
+		it('Returns element class if the tag name has been defined..', () => {
+			customElements.define('custom-element', CustomElement);
+			expect(customElements.get('custom-element')).toBe(CustomElement);
+		});
+
+		it('Returns undefined if the tag name has not been defined.', () => {
+			expect(customElements.get('custom-element')).toBe(undefined);
+		});
+	});
+
 	describe('whenDefined()', () => {
 		it('Returns a promise which is fulfilled when an element is defined.', done => {
 			customElements.whenDefined('custom-element').then(done);
Repository: capricorn86/happy-dom
Pull request #293 (2021-09-25 14:28:01)

#291@minor: Adds support for Element.getAttributeNames().


Patch:
diff --git a/packages/happy-dom/src/nodes/element/Element.ts b/packages/happy-dom/src/nodes/element/Element.ts
index 4ce9a6c9..745a1277 100644
--- a/packages/happy-dom/src/nodes/element/Element.ts
+++ b/packages/happy-dom/src/nodes/element/Element.ts
@@ -483,6 +483,15 @@ export default class Element extends Node implements IElement {
 		this.setAttributeNode(attribute);
 	}
 
+	/**
+	 * Returns attribute names.
+	 *
+	 * @returns Attribute names.
+	 */
+	public getAttributeNames(): string[] {
+		return Object.keys(this._attributes);
+	}
+
 	/**
 	 * Returns attribute value.
 	 *


Test patch:
diff --git a/packages/happy-dom/test/nodes/element/Element.test.ts b/packages/happy-dom/test/nodes/element/Element.test.ts
index 0e12faed..81b4c211 100644
--- a/packages/happy-dom/test/nodes/element/Element.test.ts
+++ b/packages/happy-dom/test/nodes/element/Element.test.ts
@@ -999,6 +999,15 @@ describe('Element', () => {
 		});
 	});
 
+	describe('getAttributeNames()', () => {
+		it('Returns attribute names.', () => {
+			element.setAttributeNS(NAMESPACE_URI, 'global:local1', 'value1');
+			element.setAttribute('key1', 'value1');
+			element.setAttribute('key2', '');
+			expect(element.getAttributeNames()).toEqual(['global:local1', 'key1', 'key2']);
+		});
+	});
+
 	describe('hasAttribute()', () => {
 		it('Returns "true" if an element has an attribute.', () => {
 			element.setAttribute('key1', 'value1');
Repository: capricorn86/happy-dom
Pull request #331 (2022-01-18 00:42:40)

#330@minor: Add document.location.
Fixes https://github.com/capricorn86/happy-dom/issues/330

I used a getter instead of a static property for 3 reasons:
- we want to have the exact same object as `window.location` so that changes made in one will also impact the other (so I didn't use `new Location()`)
- the document has a view `this._defaultView` that can be updated, so `document.location` should always be up-to-date with the linked view
- it won't be overridable 

I also added a test that passes `npm test`

![image](https://user-images.githubusercontent.com/22725671/149851846-827c34b3-6dff-4f5e-9138-58ca2ff60cba.png)


--- 

Note: this is my first PR to the project, so I'm not sure I followed every guideline


Patch:
diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts
index 739e5bf1..3bb452d8 100644
--- a/packages/happy-dom/src/nodes/document/Document.ts
+++ b/packages/happy-dom/src/nodes/document/Document.ts
@@ -33,6 +33,7 @@ import IHTMLLinkElement from '../html-link-element/IHTMLLinkElement';
 import IHTMLStyleElement from '../html-style-element/IHTMLStyleElement';
 import DocumentReadyStateEnum from './DocumentReadyStateEnum';
 import DocumentReadyStateManager from './DocumentReadyStateManager';
+import Location from '../../location/Location';
 
 /**
  * Document.
@@ -219,6 +220,15 @@ export default class Document extends Node implements IDocument {
 		return this._activeElement || this.body || this.documentElement || null;
 	}
 
+	/**
+	 * Returns location.
+	 *
+	 * @returns Location.
+	 */
+	public get location(): Location {
+		return this._defaultView.location;
+	}
+
 	/**
 	 * Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
 	 *


Test patch:
diff --git a/packages/happy-dom/test/nodes/document/Document.test.ts b/packages/happy-dom/test/nodes/document/Document.test.ts
index 515d5275..a05a834a 100644
--- a/packages/happy-dom/test/nodes/document/Document.test.ts
+++ b/packages/happy-dom/test/nodes/document/Document.test.ts
@@ -274,6 +274,12 @@ describe('Document', () => {
 		});
 	});
 
+	describe('get location()', () => {
+		it('Returns the current location', () => {
+			expect(document.location === window.location).toBe(true);
+		});
+	});
+
 	describe('append()', () => {
 		it('Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.', () => {
 			const node1 = document.createComment('test1');