Skip to content

Commit

Permalink
capricorn86#1176@patch: Implement customElements.getName().
Browse files Browse the repository at this point in the history
  • Loading branch information
mash-graz committed Dec 9, 2023
1 parent 9a0062b commit 806e960
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/happy-dom/src/custom-element/CustomElementRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@ export default class CustomElementRegistry {
this._callbacks[upperTagName].push(resolve);
});
}

/**
* Reverse lookup searching for tagName by given element class.
*
* @param elementClass Class constructor.
* @returns First found Tag name or `null`.
*/
public getName(elementClass: typeof HTMLElement): string | null {
const tagName = Object.keys(this._registry).find(
(k) => this._registry[k].elementClass === elementClass
);
return !!tagName ? tagName : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ describe('CustomElementRegistry', () => {
});
});
});

describe('getName()', () => {
it('Returns null if no tagName is found in the registry for element class', () => {
expect(customElements.getName(CustomElement)).toBe(null);
});

it('Returns Tag name if element class is found in registry', () => {
customElements.define('custom-element', CustomElement);
expect(customElements.getName(CustomElement)).toMatch(/custom-element/i);
});
});
});

0 comments on commit 806e960

Please sign in to comment.