From 0c8ff2071280bea41b6c666610563d28f5d7b64b Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Thu, 11 Jul 2024 11:21:46 +0200
Subject: [PATCH] fix: empty datatable head

---
 examples/example4/template/test.html |  3 ++-
 source/template/prepare.go           | 14 ++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/examples/example4/template/test.html b/examples/example4/template/test.html
index 4c239f9..794b0ec 100644
--- a/examples/example4/template/test.html
+++ b/examples/example4/template/test.html
@@ -49,7 +49,7 @@
     </header>
 
     <monster-state>
-      <a href="mmmm" data-attributes="href path:anchors.mmmm.href,title path:anchors.mmmm.title,hreflang path:anchors.mmmm.hreflang" data-bob-reference="mmmm"><img width="16" height="16" alt="tick" title="yes" src="data:image/gif;base64,R0lGODdhEAAQAMwAAPj7+FmhUYjNfGuxYYDJdYTIeanOpT+DOTuANXi/bGOrWj6CONzv2sPjv2Cm
+      <a href="mmmm" data-attributes="title path:anchors.mmmm.title,href path:anchors.mmmm.href,hreflang path:anchors.mmmm.hreflang" data-bob-reference="mmmm"><img width="16" height="16" alt="tick" title="yes" src="data:image/gif;base64,R0lGODdhEAAQAMwAAPj7+FmhUYjNfGuxYYDJdYTIeanOpT+DOTuANXi/bGOrWj6CONzv2sPjv2Cm
   V1unU4zPgI/Sg6DJnJ3ImTh8Mtbs00aNP1CZSGy0YqLEn47RgXW8amasW7XWsmmvX2iuXiwAAAAAEAAQAAAFVyAgjmRpnihqGCkpDQ
   PbGkNUOFk6DZqgHCNGg2T4QAQBoIiRSAwBE4VA4FACKgkB5NGReASFZEmxsQ0whPDi9BiACYQAInXhwOUtgCUQoORFCGt/g4QAIQA7" id="tickyesdata-image-gi-4013311193" data-bob-reference="tickyesdata-image-gi-3753669760" data-attributes="src path:content.tickyesdata-image-gi-3753669760.src,alt path:content.tickyesdata-image-gi-3753669760.alt,title path:content.tickyesdata-image-gi-3753669760.title"/></a>
       <h1 class="deco"><span data-bob-reference="bad-request" data-replace-self="path:text.bad-request.text">Bad Request</span></h1>
@@ -99,6 +99,7 @@
         <div data-monster-head="channel order number" data-monster-replace="path:datatable-order-list-row.channelOrderID" data-bob-reference="channel-order-number" data-attributes="data-monster-head path:text.channel-order-number.text"></div>
         <div data-monster-head="resubmissionDate" data-monster-mode="hidden" data-bob-reference="resubmission-date" data-attributes="data-monster-head path:text.resubmission-date.text"></div>
         <div data-monster-head="payment type" data-monster-replace="path:datatable-order-list-row.localStrings.paymentType" data-bob-reference="payment-type" data-attributes="data-monster-head path:text.payment-type.text"></div>
+        <div data-monster-head=" " data-monster-replace="path:datatable-order-list-row.localStrings.paymentType" data-attributes="data-monster-head path:text.payment-type.text"></div>
       </template>
     </monster-datatable>
   
diff --git a/source/template/prepare.go b/source/template/prepare.go
index 4f55c2b..de3624c 100644
--- a/source/template/prepare.go
+++ b/source/template/prepare.go
@@ -554,7 +554,21 @@ func checkMonsterDatatableHead(n *html.Node, storage *types.PageData) {
 
 	for _, div := range list {
 
+		for i, attr := range div.Attr {
+			if attr.Key == constants.DataBobReferenceAttributeKey && attr.Val == "" {
+				div.Attr = append(div.Attr[:i], div.Attr[i+1:]...)
+			}
+		}
+
 		head := util.GetAttribute(div.Attr, "data-monster-head")
+
+		trimHead := strings.TrimSpace(head)
+
+		if trimHead == "" {
+			log.Warn("The head '" + buildLogContent(div, "") + "' has no content! Will use the content of the head as a key.")
+			continue
+		}
+
 		logContent := buildLogContent(div, head)
 
 		id := util.GetOrCreateReference(div, head)
-- 
GitLab