Repository: capricorn86/happy-dom
Pull request #412 (2022-03-16 05:30:03)

#411@minor: Adds support for document.getElementsByName
solve #411 


Patch:
diff --git a/packages/happy-dom/src/nodes/document/Document.ts b/packages/happy-dom/src/nodes/document/Document.ts
index dd1b523f..862e8de6 100644
--- a/packages/happy-dom/src/nodes/document/Document.ts
+++ b/packages/happy-dom/src/nodes/document/Document.ts
@@ -358,6 +358,31 @@ export default class Document extends Node implements IDocument {
 		return <Element>ParentNodeUtility.getElementById(this, id);
 	}
 
+	/**
+	 * Returns an element by Name.
+	 *
+	 * @returns Matching element.
+	 * @param name
+	 */
+	public getElementsByName(name: string): INodeList<IElement> {
+		const _getElementsByName = (
+			_parentNode: IElement | IDocumentFragment | IDocument,
+			_name: string
+		): INodeList<IElement> => {
+			const matches = HTMLCollectionFactory.create();
+			for (const child of _parentNode.children) {
+				if ((child.getAttributeNS(null, 'name') || '') === _name) {
+					matches.push(child);
+				}
+				for (const match of _getElementsByName(<IElement>child, _name)) {
+					matches.push(match);
+				}
+			}
+			return matches;
+		};
+		return _getElementsByName(this, name);
+	}
+
 	/**
 	 * Clones a node.
 	 *
diff --git a/packages/happy-dom/src/nodes/document/IDocument.ts b/packages/happy-dom/src/nodes/document/IDocument.ts
index 267240e1..78ffd9c8 100644
--- a/packages/happy-dom/src/nodes/document/IDocument.ts
+++ b/packages/happy-dom/src/nodes/document/IDocument.ts
@@ -17,6 +17,7 @@ import IHTMLScriptElement from '../html-script-element/IHTMLScriptElement';
 import CSSStyleSheet from '../../css/CSSStyleSheet';
 import Location from '../../location/Location';
 import DocumentReadyStateEnum from './DocumentReadyStateEnum';
+import INodeList from '../node/INodeList';
 
 /**
  * Document.
@@ -150,6 +151,14 @@ export default interface IDocument extends IParentNode {
 	 */
 	getElementById(id: string): IElement;
 
+	/**
+	 * Returns an elements by name.
+	 *
+	 * @param name Name.
+	 * @returns Matching element.
+	 */
+	getElementsByName(name: string): INodeList<IElement>;
+
 	/**
 	 * Clones a node.
 	 *


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 59a7cc37..ab54580d 100644
--- a/packages/happy-dom/test/nodes/document/Document.test.ts
+++ b/packages/happy-dom/test/nodes/document/Document.test.ts
@@ -476,6 +476,16 @@ describe('Document', () => {
 		});
 	});
 
+	describe('getElementsByName()', () => {
+		it('Returns elements by name.', () => {
+			const parent = document.createElement('div');
+			parent.innerHTML = `<img alt="" name="image" src=""/><img alt="" name="image" src=""/><img alt="" name="image" src=""/><img alt="" name="image" src=""/><meta name="test"><p name="test"><span name="test">test</span></p></meta>`;
+			document.appendChild(parent);
+			expect(document.getElementsByName('image').length).toBe(4);
+			expect(document.getElementsByName('test').length).toBe(3);
+		});
+	});
+
 	describe('appendChild()', () => {
 		it('Updates the children property when appending an element child.', () => {
 			const div = document.createElement('div');