From a1d1490a72733cf87e896b897473af93aaedcd9a Mon Sep 17 00:00:00 2001
From: "mathias.chouet" <mathias.chouet@irstea.fr>
Date: Wed, 27 Mar 2019 17:05:26 +0100
Subject: [PATCH] Fix nghyd#185 forbid to link parent with child

---
 src/nub.ts | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/src/nub.ts b/src/nub.ts
index 6fa4cc73..3b825897 100644
--- a/src/nub.ts
+++ b/src/nub.ts
@@ -257,10 +257,10 @@ export abstract class Nub extends ComputeNode implements IObservable {
         let res: LinkedValue[] = [];
         const symbol = src.symbol;
 
-        // if parameter comes from the same Nub, no linking is possible at all;
-        // different Structures in the same parent are different Nubs so they
-        // can get linked to each other
-        if (src.nubUid !== this.uid) {
+        // If parameter comes from the same Nub, its parent or any of its children,
+        // no linking is possible at all.
+        // Different Structures in the same parent can get linked to each other.
+        if (! this.isParentOrChildOf(src.nubUid)) {
 
             // 1. parameters
             for (const p of this._prms) {
@@ -312,6 +312,28 @@ export abstract class Nub extends ComputeNode implements IObservable {
         return res;
     }
 
+    /**
+     * Returns true if the given Nub UID is either of :
+     *  - the current Nub UID
+     *  - the current Nub's parent Nub UID
+     *  - the UID of any of the current Nub's children
+     */
+    public isParentOrChildOf(uid: string): boolean {
+        if (this.uid === uid) {
+            return true;
+        }
+        const parent = this.getParent();
+        if (parent && parent.uid === uid) {
+            return true;
+        }
+        for (const c of this.getChildren()) {
+            if (c.uid === uid) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /**
      * Returns true if
      *  - this Nub
-- 
GitLab