// 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) { // 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) testCases := []struct { name string input string expected string }{ { name: "h1 without id", input: `

Heading without ID

`, expected: `

Heading without ID

`, }, { name: "h2 without id", input: `

Another Heading

`, expected: `

Another Heading

`, }, { name: "h3 without id", input: `

Third Level

`, expected: `

Third Level

`, }, { name: "h1 with existing id should keep it", input: `

Heading with ID

`, expected: `

Heading with ID

`, }, { name: "h1 with user-content prefix should not double prefix", input: `

Already Prefixed

`, expected: `

Already Prefixed

`, }, { name: "heading with special characters", input: `

What is Wine Staging?

`, expected: `

What is Wine Staging?

`, }, { name: "heading with nested elements", input: `

Bold and Italic

`, expected: `

Bold and Italic

`, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var result strings.Builder ctx := NewTestRenderContext().WithEnableHeadingIDGeneration(true) err := PostProcessDefault(ctx, strings.NewReader(tc.input), &result) assert.NoError(t, err) assert.Equal(t, tc.expected, strings.TrimSpace(result.String())) }) } } func TestProcessNodeAttrID_SkipHeadingIDForComments(t *testing.T) { // 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. testCases := []struct { name string input string expected string }{ { name: "h1 without id in comment context", input: `

Heading without ID

`, expected: `

Heading without ID

`, }, { name: "h2 without id in comment context", input: `

Another Heading

`, expected: `

Another Heading

`, }, { name: "h1 with existing id should still be prefixed", input: `

Heading with ID

`, expected: `

Heading with ID

`, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { var result strings.Builder // Default context without EnableHeadingIDGeneration (simulates comment rendering) err := PostProcessDefault(NewTestRenderContext(), strings.NewReader(tc.input), &result) assert.NoError(t, err) assert.Equal(t, tc.expected, strings.TrimSpace(result.String())) }) } }