Skip to content

Commit

Permalink
fix(renderer): order not preserved (#2261)
Browse files Browse the repository at this point in the history
  • Loading branch information
edusperoni committed Sep 21, 2020
1 parent 1dca81b commit 07abb9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions nativescript-angular/element-registry.ts
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
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
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

0 comments on commit 07abb9e

Please sign in to comment.