gitea源码

html_issue_test.go 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package markup_test
  4. import (
  5. "context"
  6. "html/template"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/modules/htmlutil"
  10. "code.gitea.io/gitea/modules/markup"
  11. "code.gitea.io/gitea/modules/markup/markdown"
  12. testModule "code.gitea.io/gitea/modules/test"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func TestRender_IssueList(t *testing.T) {
  17. defer testModule.MockVariableValue(&markup.RenderBehaviorForTesting.DisableAdditionalAttributes, true)()
  18. markup.Init(&markup.RenderHelperFuncs{
  19. RenderRepoIssueIconTitle: func(ctx context.Context, opts markup.RenderIssueIconTitleOptions) (template.HTML, error) {
  20. return htmlutil.HTMLFormat("<div>issue #%d</div>", opts.IssueIndex), nil
  21. },
  22. })
  23. test := func(input, expected string) {
  24. rctx := markup.NewTestRenderContext(markup.TestAppURL, map[string]string{
  25. "user": "test-user", "repo": "test-repo",
  26. "markupAllowShortIssuePattern": "true",
  27. "footnoteContextId": "12345",
  28. })
  29. out, err := markdown.RenderString(rctx, input)
  30. require.NoError(t, err)
  31. assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
  32. }
  33. t.Run("NormalIssueRef", func(t *testing.T) {
  34. test(
  35. "#12345",
  36. `<p><a href="/test-user/test-repo/issues/12345" class="ref-issue" rel="nofollow">#12345</a></p>`,
  37. )
  38. })
  39. t.Run("ListIssueRef", func(t *testing.T) {
  40. test(
  41. "* #12345",
  42. `<ul>
  43. <li><div>issue #12345</div></li>
  44. </ul>`,
  45. )
  46. })
  47. t.Run("ListIssueRefNormal", func(t *testing.T) {
  48. test(
  49. "* foo #12345 bar",
  50. `<ul>
  51. <li>foo <a href="/test-user/test-repo/issues/12345" class="ref-issue" rel="nofollow">#12345</a> bar</li>
  52. </ul>`,
  53. )
  54. })
  55. t.Run("ListTodoIssueRef", func(t *testing.T) {
  56. test(
  57. "* [ ] #12345",
  58. `<ul>
  59. <li class="task-list-item"><input type="checkbox" disabled="" data-source-position="2"/><div>issue #12345</div></li>
  60. </ul>`,
  61. )
  62. })
  63. t.Run("IssueFootnote", func(t *testing.T) {
  64. test(
  65. "foo[^1][^2]\n\n[^1]: bar\n[^2]: baz",
  66. `<p>foo<sup id="fnref:user-content-1-12345"><a href="#fn:user-content-1-12345" rel="nofollow">1 </a></sup><sup id="fnref:user-content-2-12345"><a href="#fn:user-content-2-12345" rel="nofollow">2 </a></sup></p>
  67. <div>
  68. <hr/>
  69. <ol>
  70. <li id="fn:user-content-1-12345">
  71. <p>bar <a href="#fnref:user-content-1-12345" rel="nofollow">↩︎</a></p>
  72. </li>
  73. <li id="fn:user-content-2-12345">
  74. <p>baz <a href="#fnref:user-content-2-12345" rel="nofollow">↩︎</a></p>
  75. </li>
  76. </ol>
  77. </div>`,
  78. )
  79. })
  80. }