Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.

Commit 10fbf28

Browse files
committedApr 29, 2021
refactor: compile elements instead of nodes
1 parent 1c5d00d commit 10fbf28

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed
 

‎src/core/compile.ts

+33-20
Original file line numberDiff line numberDiff line change
@@ -108,32 +108,39 @@ export const collectAndInitDirectives = (
108108
return [directives, removeDupesFromArray(nodeDeps)];
109109
};
110110

111-
export const flattenNodeChildren = (
112-
rootNode: HTMLElement,
111+
export const flattenElementChildren = (
112+
rootElement: HTMLElement,
113113
isListGroup = false,
114-
ignoreRootNode = false
114+
ignoreRootElement = false
115115
): HTMLElement[] => {
116116
const collection: HTMLElement[] = [];
117-
const isList = isListRenderScope(rootNode);
118-
const isUnderList = isUnderListRenderScope(rootNode);
117+
const isList = isListRenderScope(rootElement);
118+
const isUnderList = isUnderListRenderScope(rootElement);
119119

120120
// Return nothing if it isn't list compilation and is a list or under a list
121121
if (!isListGroup && (isList || isUnderList)) return collection;
122-
// Add root node to return array if it isn't a list or under a list
123-
if (!ignoreRootNode && (!isListGroup || !isList)) collection.push(rootNode);
122+
// Add root elem to return array if it isn't a list or under a list
123+
if (!ignoreRootElement && (!isListGroup || !isList)) collection.push(rootElement);
124124

125125
// Is not a list or under a list, but pass if is a list group
126126
if (isListGroup || (!isList && !isUnderList)) {
127-
for (const childNode of rootNode.childNodes) {
128-
if (childNode.nodeType === Node.ELEMENT_NODE) {
129-
if (!isListGroup && isListRenderScope(childNode as HTMLElement)) {
127+
for (const childElement of rootElement.children) {
128+
// Check if childElement has attributes
129+
if (childElement instanceof HTMLElement) {
130+
if (!isListGroup && isListRenderScope(childElement)) {
130131
// Push root if it is a list render (don't want to push unrendered template)
131-
collection.push(childNode as HTMLElement);
132+
collection.push(childElement);
132133
} else {
133134
// Skip over nested components (independent compile request)
134-
if ((childNode as HTMLElement).hasAttribute(`${DIRECTIVE_PREFIX}state`)) continue;
135+
if (childElement.hasAttribute(`${DIRECTIVE_PREFIX}state`)) continue;
135136
// Push all children into array (recursive flattening)
136-
collection.push(...flattenNodeChildren(childNode as HTMLElement, isListGroup));
137+
collection.push(
138+
...flattenElementChildren(
139+
childElement,
140+
isListGroup,
141+
childElement.attributes.length === 0
142+
)
143+
);
137144
}
138145
}
139146
}
@@ -142,20 +149,26 @@ export const flattenNodeChildren = (
142149
return collection;
143150
};
144151

145-
export const compile = (el: HTMLElement, state: State = {}, ignoreRootNode = false): ASTNode[] => {
152+
export const compile = (
153+
el: HTMLElement,
154+
state: State = {},
155+
ignoreRootElement = false
156+
): ASTNode[] => {
146157
const ast: ASTNode[] = [];
147158
const isListGroup =
148159
getElementCustomProp(el, COMPONENT_FLAG) !== undefined && isListRenderScope(el);
149-
const nodes: HTMLElement[] = flattenNodeChildren(el, isListGroup, ignoreRootNode);
160+
const elements: HTMLElement[] = flattenElementChildren(el, isListGroup, ignoreRootElement);
150161
const maskDirective = `${DIRECTIVE_PREFIX}mask`;
151162

163+
console.log(elements);
164+
152165
/* istanbul ignore next */
153-
nodes.forEach((node) => {
154-
if (node.hasAttribute(maskDirective)) {
155-
node.removeAttribute(maskDirective);
166+
elements.forEach((element) => {
167+
if (element.hasAttribute(maskDirective)) {
168+
element.removeAttribute(maskDirective);
156169
}
157-
if (hasDirectiveRE().test(node.outerHTML)) {
158-
const newASTNode = createASTNode(node, state);
170+
if (hasDirectiveRE().test(element.outerHTML)) {
171+
const newASTNode = createASTNode(element, state);
159172
if (newASTNode) ast.push(newASTNode);
160173
}
161174
});

‎src/core/directives/bind.ts

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const bindDirective = ({ el, parts, data, state }: DirectiveProps): void
99
switch (parts[1]) {
1010
case 'class': {
1111
const classes = data.compute(state);
12-
console.log(classes);
1312
// Accept just providing classes regularly
1413
if (typeof classes === 'string') {
1514
return el.setAttribute('class', formatAcceptableWhitespace(`${el.className} ${classes}`));

‎src/core/directives/on.ts

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export const onDirective = ({ el, parts, data, state }: DirectiveProps): void =>
4343
options.once = eventProps.includes('once');
4444
options.passive = eventProps.includes('passive');
4545

46-
4746
target.addEventListener(parts[1], handler, options);
4847

4948
setElementCustomProp(el, `__on_${parts[1]}_registered`, true);

0 commit comments

Comments
 (0)
This repository has been archived.