Add code for data-ck attributes which can occur on any tag

Chris Pollett [2023-04-28 05:Apr:th]
Add code for data-ck attributes which can occur on any tag
Filename
css/game.css
js/game.js
diff --git a/css/game.css b/css/game.css
index 102b4b9..fd7ad54 100644
--- a/css/game.css
+++ b/css/game.css
@@ -24,6 +24,10 @@ x-action
 /*
  * General useful classes
  */
+.none
+{
+    display: none;
+}
 .float-right
 {
     float: right;
diff --git a/js/game.js b/js/game.js
index da66bd6..c9f57ca 100644
--- a/js/game.js
+++ b/js/game.js
@@ -772,6 +772,47 @@ class Location
         }
         return [check_result, proceed, pause];
     }
+    /**
+     * A data-ck attribute on any tag other than an x-present tag can
+     * contain a Javascript boolean expression to control the display
+     * or non-display of that element. This is similar to a ck attribute
+     * of an x-present tag. This method evalutes data-ck expressions for
+     * each tag in the text in its section argument and adds a class="none"
+     * attribute to that tag if it evaluates to false (causing it not to
+     * display). The string after these substitutions is returned.
+     *
+     * @param {string} section of text to check for data-ck attributes and
+     *  for which to carry out the above described substitutions
+     * @return {string} after substitutions have been carried out.
+     */
+    evaluateDataChecks(section)
+    {
+        let quote = `(?:(?:'([^']*)')|(?:"([^"]*)"))`;
+        let old_section = "";
+        while (section != old_section) {
+            old_section = section;
+            let data_ck_pattern = new RegExp(
+                "\<(?:[^\>]+)(data\-ck\s*\=\s*(" + quote + "))(?:[^\>]*)\>",
+            'i');
+            let data_ck_match = section.match(data_ck_pattern);
+            if (!data_ck_match) {
+                continue;
+            }
+            let condition = (typeof data_ck_match[3] == 'string') ?
+                 data_ck_match[3] : data_ck_match[4];
+            if (typeof condition == 'string') {
+                let check_result = (condition.replace(/\s+/, "") != "") ?
+                    eval(condition) : true;
+                if (check_result) {
+                    section = section.replace(data_ck_match[1], " ");
+                } else {
+                    section = section.replace(data_ck_match[1],
+                        " class='none' ");
+                }
+            }
+        }
+        return section;
+    }
     /**
      * A given Location contains one or more x-present tags which are
      * used when rendering that location to the game-content area. This
@@ -787,11 +828,12 @@ class Location
     {
         let old_section = "";
         let quote = `(?:(?:'([^']*)')|(?:"([^"]*)"))`;
+        section = this.evaluateDataChecks(section);
         section = interpolateVariables(section);
         while (section != old_section) {
             old_section = section;
             let speaker_pattern = new RegExp(
-                "\<x-speaker([^\>]*)name\s*\=\s*("+quote+")([^\>]*)>",
+                "\<x-speaker([^\>]*)name\s*\=\s*(" + quote + ")([^\>]*)>",
                 'i');
             let expression_pattern = new RegExp(
                 "expression\s*\=\s*("+quote+")", 'i');
ViewGit