From ebbd050df4d14b157de657b9ee0267765ccfa426 Mon Sep 17 00:00:00 2001 From: Snider Date: Wed, 15 Apr 2026 01:23:12 +0100 Subject: [PATCH] Stabilize repeated layout block IDs --- edge_test.go | 13 +++++++++++++ layout.go | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/edge_test.go b/edge_test.go index 8a52f18..6d2390b 100644 --- a/edge_test.go +++ b/edge_test.go @@ -496,6 +496,19 @@ func TestLayout_DuplicateVariantChars_Ugly(t *testing.T) { } } +func TestLayout_DuplicateVariantChars_UniqueBlockIDs_Good(t *testing.T) { + ctx := NewContext() + + layout := NewLayout("CCC").C(Raw("content")) + got := layout.Render(ctx) + + for _, want := range []string{`data-block="C"`, `data-block="C.1"`, `data-block="C.2"`} { + if !containsText(got, want) { + t.Fatalf("CCC variant should assign unique block ID %q, got:\n%s", want, got) + } + } +} + func TestLayout_EmptySlots_Ugly(t *testing.T) { ctx := NewContext() diff --git a/layout.go b/layout.go index e287eb2..07a31f1 100644 --- a/layout.go +++ b/layout.go @@ -180,6 +180,7 @@ func (l *Layout) Render(ctx *Context) string { b := newTextBuilder() rendered := 0 + rootCounts := make(map[byte]int) for i := range len(l.variant) { slot := l.variant[i] @@ -194,6 +195,12 @@ func (l *Layout) Render(ctx *Context) string { } bid := l.blockID(slot, rendered) + if l.path == "" { + if seen := rootCounts[slot]; seen > 0 { + bid = string(slot) + "." + strconv.Itoa(seen) + } + rootCounts[slot] = rootCounts[slot] + 1 + } b.WriteByte('<') b.WriteString(escapeHTML(meta.tag))