From 1f9e06201450e5afa72ee844e7af45c043d3541c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Jakse?= Date: Thu, 8 Oct 2020 21:59:58 +0200 Subject: [PATCH] Fix fragile tile position lookup This code didn't work when Trivabble was served as text/html, which adds a tag in the DOM, while this tag is not added when Trivabble is served as application/xhtml+xml. --- public/trivabble.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/public/trivabble.js b/public/trivabble.js index 3df5623..08b6dbf 100644 --- a/public/trivabble.js +++ b/public/trivabble.js @@ -612,33 +612,32 @@ 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,