feat(elements): implement extractProjectableNodes()
This commit is contained in:

committed by
Victor Berchet

parent
24f17f913a
commit
6b30fbf94e
54
packages/elements/src/extract-projectable-nodes.ts
Normal file
54
packages/elements/src/extract-projectable-nodes.ts
Normal file
@ -0,0 +1,54 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// NOTE: This is a (slightly improved) version of what is used in ngUpgrade's
|
||||
// `DowngradeComponentAdapter`.
|
||||
// TODO(gkalpak): Investigate if it makes sense to share the code.
|
||||
|
||||
import {isElement, matchesSelector} from './utils';
|
||||
|
||||
export function extractProjectableNodes(host: HTMLElement, ngContentSelectors: string[]): Node[][] {
|
||||
const nodes = host.childNodes;
|
||||
const projectableNodes: Node[][] = ngContentSelectors.map(() => []);
|
||||
let wildcardIndex = -1;
|
||||
|
||||
ngContentSelectors.some((selector, i) => {
|
||||
if (selector === '*') {
|
||||
wildcardIndex = i;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
for (let i = 0, ii = nodes.length; i < ii; ++i) {
|
||||
const node = nodes[i];
|
||||
const ngContentIndex = findMatchingIndex(node, ngContentSelectors, wildcardIndex);
|
||||
|
||||
if (ngContentIndex !== -1) {
|
||||
projectableNodes[ngContentIndex].push(node);
|
||||
}
|
||||
}
|
||||
|
||||
return projectableNodes;
|
||||
}
|
||||
|
||||
function findMatchingIndex(node: Node, selectors: string[], defaultIndex: number): number {
|
||||
let matchingIndex = defaultIndex;
|
||||
|
||||
if (isElement(node)) {
|
||||
selectors.some((selector, i) => {
|
||||
if ((selector !== '*') && matchesSelector(node, selector)) {
|
||||
matchingIndex = i;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
return matchingIndex;
|
||||
}
|
Reference in New Issue
Block a user