Skip to content

Commit 1f11daf

Browse files
Merge pull request #391 from bigopon/fix-ie11-compat
fix(repeat): use utility fns for ie11 compat
2 parents d346a81 + a59a2de commit 1f11daf

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/repeat.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ export class Repeat extends AbstractRepeater {
286286
// if the template has more than 1 immediate child element
287287
// it's a repeat put on a <template/> element
288288
// not valid for matcher binding
289-
if (template.children.length > 1) {
289+
if (getChildrenCount(template) > 1) {
290290
return undefined;
291291
}
292292
// if the root element does not have any instruction
293293
// it means there's no matcher binding
294294
// no need to do any further work
295-
const repeatedElement = template.firstElementChild;
295+
const repeatedElement = getFirstElementChild(template);
296296
if (!repeatedElement.hasAttribute('au-target-id')) {
297297
return undefined;
298298
}
@@ -383,3 +383,35 @@ const extractMatcherBindingExpression = (instructions: Record<string, TargetInst
383383
}
384384
}
385385
};
386+
387+
/**
388+
* Calculate the number of child elements of an element
389+
*
390+
* Note: API .childElementCount/.children are not available in IE11
391+
*/
392+
const getChildrenCount = (el: Element | DocumentFragment) => {
393+
const childNodes = el.childNodes;
394+
let count = 0;
395+
for (let i = 0, ii = childNodes.length; ii > i; ++i) {
396+
if (childNodes[i].nodeType === /* element */1) {
397+
++count;
398+
}
399+
}
400+
return count;
401+
};
402+
403+
/**
404+
* Get the first child element of an element / doc fragment
405+
*
406+
* Note: API .firstElementChild is not available in IE11
407+
*/
408+
const getFirstElementChild = (el: Element | DocumentFragment) => {
409+
let firstChild = el.firstChild as Element;
410+
while (firstChild !== null) {
411+
if (firstChild.nodeType === /* element */1) {
412+
return firstChild;
413+
}
414+
firstChild = firstChild.nextSibling as Element;
415+
}
416+
return null;
417+
};

0 commit comments

Comments
 (0)