« get me outta code hell

html: stringify some attributes when inspecting tag - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-12-30 10:12:49 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 12:33:03 -0400
commit5dd9fa5fb9807b5682b10b8d1f29f90a9ccc42f0 (patch)
tree4a4fae1d22dc7962a76ba2d99eb35c729b745544 /src
parent8d3cf8442954113f52949b430152fe2d47784a49 (diff)
html: stringify some attributes when inspecting tag
Diffstat (limited to 'src')
-rw-r--r--src/util/html.js39
1 files changed, 36 insertions, 3 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 06afbb4f..ba31173d 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -410,7 +410,7 @@ export class Tag {
         itemContent = item.toString();
       } catch (caughtError) {
         throw new Error(
-          `Error stringifying child #${index + 1} ` +
+          `Error in child #${index + 1} ` +
           `of ${inspect(this, {compact: true})}: ` +
           inspect(item, {compact: true}),
           {cause: caughtError});
@@ -448,11 +448,44 @@ export class Tag {
   [inspect.custom](depth, opts) {
     const lines = [];
 
+    const niceAttributes = ['id', 'class'];
+    const attributes = new Attributes();
+
+    for (const attribute of niceAttributes) {
+      if (this.attributes.has(attribute)) {
+        const value = this.attributes.get(attribute);
+
+        let string;
+        let suffix = '';
+
+        if (Array.isArray(value)) {
+          string = value[0].toString();
+          if (value.length > 1) {
+            suffix = ` (+${value.length - 1})`;
+          }
+        } else {
+          string = value.toString();
+        }
+
+        const trim =
+          (string.length > 15
+            ? `${string.slice(0, 12)}...`
+            : string);
+
+        attributes.set(attribute, trim + suffix);
+      }
+    }
+
+    const attributesPart =
+      (attributes.blank
+        ? ``
+        : ` ${attributes}`);
+
     const heading =
       (this.tagName
         ? (empty(this.content)
-            ? `Tag <${this.tagName} />`
-            : `Tag <${this.tagName}> (${this.content.length} items)`)
+            ? `Tag <${this.tagName + attributesPart} />`
+            : `Tag <${this.tagName + attributesPart}> (${this.content.length} items)`)
         : (empty(this.content)
             ? `Tag (no name)`
             : `Tag (no name, ${this.content.length} items)`));