Repository: capricorn86/happy-dom
Pull request #634 (2022-10-23 19:15:12)

#633@patch: Fix getElementByTagName doesn't support wildcard searches (improves jQuery support).


Patch:
diff --git a/packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts b/packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
index 9de04377..75572154 100644
--- a/packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
+++ b/packages/happy-dom/src/nodes/parent-node/ParentNodeUtility.ts
@@ -97,9 +97,10 @@ export default class ParentNodeUtility {
 	): IHTMLCollection<IElement> {
 		const upperTagName = tagName.toUpperCase();
 		const matches = HTMLCollectionFactory.create();
+		const includeAll = tagName === '*';
 
 		for (const child of parentNode.children) {
-			if (child.tagName === upperTagName) {
+			if (includeAll || child.tagName === upperTagName) {
 				matches.push(child);
 			}
 			for (const match of this.getElementsByTagName(<IElement>child, tagName)) {
@@ -125,9 +126,10 @@ export default class ParentNodeUtility {
 	): IHTMLCollection<IElement> {
 		const upperTagName = tagName.toUpperCase();
 		const matches = HTMLCollectionFactory.create();
+		const includeAll = tagName === '*';
 
 		for (const child of parentNode.children) {
-			if (child.tagName === upperTagName && child.namespaceURI === namespaceURI) {
+			if ((includeAll || child.tagName === upperTagName) && child.namespaceURI === namespaceURI) {
 				matches.push(child);
 			}
 			for (const match of this.getElementsByTagNameNS(<IElement>child, namespaceURI, tagName)) {


Test patch:
diff --git a/packages/happy-dom/test/nodes/parent-node/ParentNodeUtility.test.ts b/packages/happy-dom/test/nodes/parent-node/ParentNodeUtility.test.ts
index 0e2aac92..3a412f1e 100644
--- a/packages/happy-dom/test/nodes/parent-node/ParentNodeUtility.test.ts
+++ b/packages/happy-dom/test/nodes/parent-node/ParentNodeUtility.test.ts
@@ -155,6 +155,27 @@ describe('ParentNodeUtility', () => {
 				div4
 			]);
 		});
+
+		it('Returns all elements when tag name is *.', () => {
+			const parent = document.createElement('div');
+			const div1 = document.createElement('div');
+			const div2 = document.createElement('div');
+			const div3 = document.createElement('div');
+			const div4 = document.createElement('div');
+			const span1 = document.createElement('span');
+			const span2 = document.createElement('span');
+			const span3 = document.createElement('span');
+
+			parent.appendChild(div1);
+			div1.appendChild(div2);
+			div2.appendChild(span1);
+			span1.appendChild(div3);
+			div3.appendChild(span2);
+			div3.appendChild(span3);
+			span3.appendChild(div4);
+
+			expect(ParentNodeUtility.getElementsByTagName(parent, '*').length).toEqual(7);
+		});
 	});
 
 	describe('getElementsByTagNameNS()', () => {
@@ -164,7 +185,7 @@ describe('ParentNodeUtility', () => {
 			const div2 = document.createElement('div');
 			const div3 = document.createElementNS(NamespaceURI.svg, 'div');
 			const div4 = document.createElement('div');
-			const span1 = document.createElement('span');
+			const span1 = document.createElementNS(NamespaceURI.svg, 'span');
 			const span2 = document.createElement('span');
 			const span3 = document.createElement('span');
 
@@ -181,6 +202,29 @@ describe('ParentNodeUtility', () => {
 				div3
 			]);
 		});
+
+		it('Returns all elements when tag name is *.', () => {
+			const parent = document.createElement('div');
+			const div1 = document.createElementNS(NamespaceURI.svg, 'div');
+			const div2 = document.createElement('div');
+			const div3 = document.createElementNS(NamespaceURI.svg, 'div');
+			const div4 = document.createElement('div');
+			const span1 = document.createElementNS(NamespaceURI.svg, 'span');
+			const span2 = document.createElement('span');
+			const span3 = document.createElement('span');
+
+			parent.appendChild(div1);
+			div1.appendChild(div2);
+			div2.appendChild(span1);
+			span1.appendChild(div3);
+			div3.appendChild(span2);
+			div3.appendChild(span3);
+			span3.appendChild(div4);
+
+			expect(
+				ParentNodeUtility.getElementsByTagNameNS(parent, NamespaceURI.svg, '*').length
+			).toEqual(3);
+		});
 	});
 
 	describe('getElementByTagName()', () => {