Skip to content

Commit

Permalink
fix(repeat): use utility fns for ie11 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
bigopon committed Jan 16, 2020
1 parent d346a81 commit a59a2de
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/repeat.ts
Expand Up @@ -286,13 +286,13 @@ export class Repeat extends AbstractRepeater {
// if the template has more than 1 immediate child element
// it's a repeat put on a <template/> element
// not valid for matcher binding
if (template.children.length > 1) {
if (getChildrenCount(template) > 1) {
return undefined;
}
// if the root element does not have any instruction
// it means there's no matcher binding
// no need to do any further work
const repeatedElement = template.firstElementChild;
const repeatedElement = getFirstElementChild(template);
if (!repeatedElement.hasAttribute('au-target-id')) {
return undefined;
}
Expand Down Expand Up @@ -383,3 +383,35 @@ const extractMatcherBindingExpression = (instructions: Record<string, TargetInst
}
}
};

/**
* Calculate the number of child elements of an element
*
* Note: API .childElementCount/.children are not available in IE11
*/
const getChildrenCount = (el: Element | DocumentFragment) => {
const childNodes = el.childNodes;
let count = 0;
for (let i = 0, ii = childNodes.length; ii > i; ++i) {
if (childNodes[i].nodeType === /* element */1) {
++count;
}
}
return count;
};

/**
* Get the first child element of an element / doc fragment
*
* Note: API .firstElementChild is not available in IE11
*/
const getFirstElementChild = (el: Element | DocumentFragment) => {
let firstChild = el.firstChild as Element;
while (firstChild !== null) {
if (firstChild.nodeType === /* element */1) {
return firstChild;
}
firstChild = firstChild.nextSibling as Element;
}
return null;
};

0 comments on commit a59a2de

Please sign in to comment.