Fix fragile tile position lookup
authorRaphaël Jakse <raphael.trivabble@jakse.fr>
Thu, 8 Oct 2020 19:59:58 +0000 (21:59 +0200)
committerRaphaël Jakse <raphael.trivabble@jakse.fr>
Thu, 8 Oct 2020 20:03:56 +0000 (22:03 +0200)
This code didn't work when Trivabble was served as text/html, which adds
a <tbody> tag in the DOM, while this tag is not added when Trivabble is
served as application/xhtml+xml.

public/trivabble.js

index 3df56233a75f777ac17b8d1b7cc05fca2c33ff73..08b6dbff68c37a3804f1461062ddccd3fb8ace86 100644 (file)
         rackBCR  = rack.getBoundingClientRect();
         bagBCR   = bag.getBoundingClientRect();
 
-        let from;
+        let from = null;
         let index;
 
-        let p = movingTile.parentNode;
-        let oldP = movingTile;
-        let oldOldP = null;
+        let p = movingTile;
 
-        while (p) {
-            if (p === board) {
+        while (p && !from) {
+            index = boardCells.indexOf(p);
+            if (index !== -1) {
                 from  = "board";
-                index = boardCells.indexOf(oldOldP);
-
                 break;
             }
 
-            if (p === rack) {
+            index = playerLetters.indexOf(p);
+            if (index !== -1) {
                 from  = "rack";
-                index = playerLetters.indexOf(oldP);
-
                 break;
             }
 
-            oldOldP = oldP;
-            oldP = p;
             p = p.parentNode;
         }
 
+        if (!from) {
+            fatalError(new Error("Error: did not find the parent of the moving tile"));
+            return;
+        }
+
         moveCMD = {
             cmd: "moveLetter",
             from: from,