« get me outta code hell

html: clone tags & templates passed in via slots - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-04-15 20:13:00 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-04-15 20:13:00 -0300
commit21583d436d2eec6137fc84e6fbd9866a5cf8b860 (patch)
tree1a4ae8e0f965c878442b9cb6ef2ba7ec488121ce /src/util
parented422988035ce2e67464c544267adce4df4f5f35 (diff)
html: clone tags & templates passed in via slots
I'm not 100% sure the right behavior here in the long run (whether
we should be doing a deep clone or not), so for now I haven't added
any specific tests. Snapshot tests covering uses of templates which
depend on cloning (i.e. parents which reuse a given template) will
do better to make sure everything keeps working like it should.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util/html.js b/src/util/html.js
index a1d6962a..e2cbf776 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -190,6 +190,10 @@ export class Tag {
     this.content = content;
   }
 
+  clone() {
+    return new Tag(this.tagName, this.attributes, this.content);
+  }
+
   set tagName(value) {
     if (value === undefined || value === null) {
       this.tagName = '';
@@ -489,6 +493,12 @@ export class Template {
     this.#description = description;
   }
 
+  clone() {
+    const clone = new Template(this.#description);
+    clone.setSlots(this.#slotValues);
+    return clone;
+  }
+
   static validateDescription(description) {
     if (typeof description !== 'object') {
       throw new TypeError(`Expected object, got ${typeof description}`);
@@ -688,6 +698,10 @@ export class Template {
         return blank();
       }
 
+      if (providedValue instanceof Tag || providedValue instanceof Template) {
+        return providedValue.clone();
+      }
+
       return providedValue;
     }