|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package markup |
| 5 | + |
| 6 | +import ( |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +func TestProcessNodeAttrID_HTMLHeadingWithoutID(t *testing.T) { |
| 14 | + // Test that HTML headings without id get an auto-generated id from their text content |
| 15 | + // when EnableHeadingIDGeneration is true (for repo files and wiki pages) |
| 16 | + testCases := []struct { |
| 17 | + name string |
| 18 | + input string |
| 19 | + expected string |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "h1 without id", |
| 23 | + input: `<h1>Heading without ID</h1>`, |
| 24 | + expected: `<h1 id="user-content-heading-without-id">Heading without ID</h1>`, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "h2 without id", |
| 28 | + input: `<h2>Another Heading</h2>`, |
| 29 | + expected: `<h2 id="user-content-another-heading">Another Heading</h2>`, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "h3 without id", |
| 33 | + input: `<h3>Third Level</h3>`, |
| 34 | + expected: `<h3 id="user-content-third-level">Third Level</h3>`, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "h1 with existing id should keep it", |
| 38 | + input: `<h1 id="my-custom-id">Heading with ID</h1>`, |
| 39 | + expected: `<h1 id="user-content-my-custom-id">Heading with ID</h1>`, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "h1 with user-content prefix should not double prefix", |
| 43 | + input: `<h1 id="user-content-already-prefixed">Already Prefixed</h1>`, |
| 44 | + expected: `<h1 id="user-content-already-prefixed">Already Prefixed</h1>`, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "heading with special characters", |
| 48 | + input: `<h1>What is Wine Staging?</h1>`, |
| 49 | + expected: `<h1 id="user-content-what-is-wine-staging">What is Wine Staging?</h1>`, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "heading with nested elements", |
| 53 | + input: `<h2><strong>Bold</strong> and <em>Italic</em></h2>`, |
| 54 | + expected: `<h2 id="user-content-bold-and-italic"><strong>Bold</strong> and <em>Italic</em></h2>`, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tc := range testCases { |
| 59 | + t.Run(tc.name, func(t *testing.T) { |
| 60 | + var result strings.Builder |
| 61 | + ctx := NewTestRenderContext().WithEnableHeadingIDGeneration(true) |
| 62 | + err := PostProcessDefault(ctx, strings.NewReader(tc.input), &result) |
| 63 | + assert.NoError(t, err) |
| 64 | + assert.Equal(t, tc.expected, strings.TrimSpace(result.String())) |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestProcessNodeAttrID_SkipHeadingIDForComments(t *testing.T) { |
| 70 | + // Test that HTML headings in comment-like contexts (issue comments) |
| 71 | + // do NOT get auto-generated IDs to avoid duplicate IDs on pages with multiple documents. |
| 72 | + // This is controlled by EnableHeadingIDGeneration which defaults to false. |
| 73 | + testCases := []struct { |
| 74 | + name string |
| 75 | + input string |
| 76 | + expected string |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "h1 without id in comment context", |
| 80 | + input: `<h1>Heading without ID</h1>`, |
| 81 | + expected: `<h1>Heading without ID</h1>`, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "h2 without id in comment context", |
| 85 | + input: `<h2>Another Heading</h2>`, |
| 86 | + expected: `<h2>Another Heading</h2>`, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "h1 with existing id should still be prefixed", |
| 90 | + input: `<h1 id="my-custom-id">Heading with ID</h1>`, |
| 91 | + expected: `<h1 id="user-content-my-custom-id">Heading with ID</h1>`, |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tc := range testCases { |
| 96 | + t.Run(tc.name, func(t *testing.T) { |
| 97 | + var result strings.Builder |
| 98 | + // Default context without EnableHeadingIDGeneration (simulates comment rendering) |
| 99 | + err := PostProcessDefault(NewTestRenderContext(), strings.NewReader(tc.input), &result) |
| 100 | + assert.NoError(t, err) |
| 101 | + assert.Equal(t, tc.expected, strings.TrimSpace(result.String())) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
0 commit comments