// Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package markup import ( "strings" "testing" "github.com/stretchr/testify/assert" ) func TestProcessNodeAttrID_HTMLHeadingWithoutID(t *testing.T) { testCases := []struct { name string generateHeadingID bool input, expected string }{ // Test that HTML headings without id get an auto-generated id from their text content // when EnableHeadingIDGeneration is true (for repo files and wiki pages) { "h1 without id", true, `

Heading without ID

`, `

Heading without ID

`, }, { "h2 without id", true, `

Another Heading

`, `

Another Heading

`, }, { "h3 without id", true, `

Third Level

`, `

Third Level

`, }, { "h1 with existing id should keep it", true, `

Heading with ID

`, `

Heading with ID

`, }, { "h1 with user-content prefix should not double prefix", true, `

Already Prefixed

`, `

Already Prefixed

`, }, { "heading with special characters", true, `

What is Wine Staging?

`, `

What is Wine Staging?

`, }, { "heading with nested elements", true, `

Bold and Italic

`, `

Bold and Italic

`, }, // Test that HTML headings in comment-like contexts (issue comments) // do NOT get auto-generated IDs to avoid duplicate IDs on pages with multiple documents. // This is controlled by EnableHeadingIDGeneration which defaults to false. // Default context without EnableHeadingIDGeneration (simulates comment rendering) { "h1 without id in comment context", false, `

Heading without ID

`, `

Heading without ID

`, }, { "h2 without id in comment context", false, `

Another Heading

`, `

Another Heading

`, }, { "h1 with existing id should still be prefixed", false, `

Heading with ID

`, `

Heading with ID

`, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var result strings.Builder ctx := NewTestRenderContext() if tc.generateHeadingID { ctx.WithEnableHeadingIDGeneration(true) } err := PostProcessDefault(ctx, strings.NewReader(tc.input), &result) assert.NoError(t, err) assert.Equal(t, tc.expected, strings.TrimSpace(result.String())) }) } }