Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(renderer): order not preserved #2261

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nativescript-angular/element-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ViewExtensions {
nodeName: string;
parentNode: NgView;
nextSibling: NgView;
previousSibling: NgView;
firstChild: NgView;
lastChild: NgView;
ngCssClasses: Map<string, boolean>;
Expand All @@ -22,6 +23,7 @@ export abstract class InvisibleNode extends View implements NgView {
nodeName: string;
parentNode: NgView;
nextSibling: NgView;
previousSibling: NgView;
firstChild: NgView;
lastChild: NgView;
ngCssClasses: Map<string, boolean>;
Expand Down
16 changes: 4 additions & 12 deletions nativescript-angular/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { ViewUtil } from './view-util';
import { NgView, InvisibleNode } from './element-registry';
import { NativeScriptDebug } from './trace';

export interface ElementReference {
previous: NgView;
next: NgView;
}

@Injectable()
export class NativeScriptRenderer extends Renderer2 {
data: { [key: string]: any } = Object.create(null);
Expand All @@ -31,8 +26,8 @@ export class NativeScriptRenderer extends Renderer2 {
}

@profile
insertBefore(parent: NgView, newChild: NgView, refChild: NgView | ElementReference): void {
let { previous, next } = refChild instanceof View ? this.nextSibling(refChild) : refChild;
insertBefore(parent: NgView, newChild: NgView, refChild: NgView): void {
let { previous, next } = refChild instanceof View ? { previous: refChild.previousSibling, next: refChild } : { previous: null, next: null };
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.insertBefore child: ${newChild} ` + `parent: ${parent} previous: ${previous} next: ${next}`);
}
Expand Down Expand Up @@ -68,15 +63,12 @@ export class NativeScriptRenderer extends Renderer2 {
}

@profile
nextSibling(node: NgView): ElementReference {
nextSibling(node: NgView): NgView {
if (NativeScriptDebug.isLogEnabled()) {
NativeScriptDebug.rendererLog(`NativeScriptRenderer.nextSibling of ${node} is ${node.nextSibling}`);
}

return {
previous: node,
next: node.nextSibling,
};
return node.nextSibling;
}

@profile
Expand Down
10 changes: 9 additions & 1 deletion nativescript-angular/view-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ export class ViewUtil {

if (previous) {
previous.nextSibling = child;
child.previousSibling = previous;
} else {
parent.firstChild = child;
}

if (next) {
child.nextSibling = next;
next.previousSibling = child;
} else {
this.appendToQueue(parent, child);
}
Expand All @@ -81,6 +83,7 @@ export class ViewUtil {

if (parent.lastChild) {
parent.lastChild.nextSibling = view;
view.previousSibling = parent.lastChild;
}

parent.lastChild = view;
Expand Down Expand Up @@ -152,23 +155,28 @@ export class ViewUtil {
parent.firstChild = null;
parent.lastChild = null;
child.nextSibling = null;
child.previousSibling = null;
return;
}

if (parent.firstChild === child) {
parent.firstChild = child.nextSibling;
}

const previous = this.findPreviousElement(parent, child);
const previous = child.previousSibling;
if (parent.lastChild === child) {
parent.lastChild = previous;
}

if (previous) {
previous.nextSibling = child.nextSibling;
if (child.nextSibling) {
child.nextSibling.previousSibling = previous;
}
}

child.nextSibling = null;
child.previousSibling = null;
}

// NOTE: This one is O(n) - use carefully
Expand Down