Skip to content

Commit

Permalink
Remove fallback code for old browsers that don't support <template>
Browse files Browse the repository at this point in the history
  • Loading branch information
robertknight committed Nov 22, 2024
1 parent 1870fa8 commit df35314
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
10 changes: 1 addition & 9 deletions h/static/scripts/tests/util/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,11 @@ describe('util/dom', () => {
});

describe('cloneTemplate', () => {
it('clones the first child of the template when <template> is supported', () => {
it('clones the first child of the template', () => {
const template = document.createElement('template');
template.innerHTML = '<div id="child"></div>';
const clone = domUtil.cloneTemplate(template);
assert.deepEqual(clone.outerHTML, '<div id="child"></div>');
});

it('clones the first child of the template when <template> is not supported', () => {
const template = createDOM(
'<fake-template><div id="child"></div></fake-template>',
);
const clone = domUtil.cloneTemplate(template);
assert.deepEqual(clone.outerHTML, '<div id="child"></div>');
});
});
});
28 changes: 2 additions & 26 deletions h/static/scripts/util/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,36 +49,12 @@ export function findRefs(el) {
return map;
}

/**
* Return the first child of `node` which is an `Element`.
*
* Work around certain browsers (IE, Edge) not supporting firstElementChild on
* Document, DocumentFragment.
*
* @param {Node} node
*/
function firstElementChild(node) {
for (let i = 0; i < node.childNodes.length; i++) {
if (node.childNodes[i].nodeType === Node.ELEMENT_NODE) {
return node.childNodes[i];
}
}
// istanbul ignore next
return null;
}

/**
* Clone the content of a <template> element and return the first child Element.
*
* @param {HTMLTemplateElement} templateEl
*/
export function cloneTemplate(templateEl) {
if (templateEl.content) {
// <template> supported natively.
const content = templateEl.content.cloneNode(true);
return firstElementChild(content);
} else {
// <template> not supported. Browser just treats it as an unknown Element.
return templateEl.firstElementChild.cloneNode(true);
}
const content = templateEl.content.cloneNode(true);
return content.firstElementChild;
}

0 comments on commit df35314

Please sign in to comment.