Skip to content

Commit

Permalink
MDL-77174 core: Add getElement to core/normalise
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 31, 2023
1 parent a1d5d1b commit 42d3e7d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/amd/build/normalise.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/amd/build/normalise.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions lib/amd/src/normalise.js
Expand Up @@ -65,3 +65,27 @@ export const getFirst = nodes => {
const list = getList(nodes);
return list[0];
};

/**
* Normalise a single node into an HTMLElement.
*
* @param {jQuery|Y.Ndoe|HTMLELement} node The node to normalise
* @returns {HTMLElement}
*/
export const getElement = (node) => {
if (node instanceof HTMLElement) {
return node;
}

if (node?._node) {
// This is likely a YUI Node.
// We can use (node instanceof Y.Node) but we would have to load YUI to do some.
return node._node;
}

if (node instanceof jQuery && node.length > 0) {
return node.get(0);
}

return null;
};

0 comments on commit 42d3e7d

Please sign in to comment.