Stabilize repeated layout block IDs
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

This commit is contained in:
Snider 2026-04-15 01:23:12 +01:00
parent 066bb8e92d
commit ebbd050df4
2 changed files with 20 additions and 0 deletions

View file

@ -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()

View file

@ -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))