From 0980ce9359f42bda107277798fbaa2fca946140b Mon Sep 17 00:00:00 2001 From: Thedro Neely Date: Thu, 24 Mar 2022 16:16:32 -0400 Subject: public/js/app: Ensure dictionary access does not capture code blocks Traverse upwards and check if the text element has parent
.
---
 public/js/app.js | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/public/js/app.js b/public/js/app.js
index 2e61595..3c6d91f 100644
--- a/public/js/app.js
+++ b/public/js/app.js
@@ -856,6 +856,16 @@ function preload(url) {
     }
   }
 
+  function notInside(element, name) {
+    if (typeof element.parentElement === "undefined" ||
+      element.parentElement === null
+    ) { return true; }
+    var parentTag = element.parentElement;
+    var tagName = element.tagName;
+    if (tagName === name) { return false; }
+    return notInside(parentTag, name);
+  }
+
   function dictionary(event) {
     if (
       event.ctrlKey == true ||
@@ -865,8 +875,9 @@ function preload(url) {
       return;
     }
     var word = "" + window.getSelection();
-    var container = window.getSelection().anchorNode.parentElement.tagName;
-    if (word > "" && word.length > 1 && container !== "CODE") {
+    var container = window.getSelection().anchorNode.parentElement;
+
+    if (word > "" && word.length > 1 && notInside(container, 'PRE')) {
       dictionary_access(word);
     }
   }
-- 
cgit v1.2.3