[v8.0/forgejo] fix(UI): issue task list numbers, fix #4431 (#4735)

**Backport:** https://codeberg.org/forgejo/forgejo/pulls/4452

## Regexp fixes
This fixes #4431 by adjusting the related regular expressions. Also it allows to have multiple spaces between `-` or `*` and the following brackets.

Related is the following gitea PR: https://github.com/go-gitea/gitea/pull/3296. I wonder a bit why the author stated
> Also fixed the requirement for one character after the blank of - [ ] .

I don't see a reason why
```
- [x]
some task
```
shall not be a valid checkbox without any character after the `]`. E.g., this would render to
- [x]
some task

The third commit fixes this issue.

### Comments
- I wonder a bit why the regexp is written in such complicated way and if
  ```diff
  - (^\s*[-*]\s*\[[\sxX]\])|(
\s*[-*]\s*\[[\sxX]\])
  + (^|
)\s*[-*]\s*\[[\sxX]\]
  ```
  would work the same way. Edit: I tried and indeed the second regex works as expected.
- ~~Why it is actually matching the `\s` and not just the space (` `)? (but OK, no big issue)~~ Edit: `[\t]` is also allowed. Keep it!
- Check boxes in code blocks are counted as well, but I think that this is an edge case which is not a big issue as well.

### Before
![before](https://codeberg.org/attachments/cb4f879d-cdd2-4814-aed2-5de8b5437fb2)
### After
![after](https://codeberg.org/attachments/5d54648f-dd73-4577-9a80-729016219093)

## Javascript fixes
Additionally, while testing I figured out, that currently checkboxes with capital `X` like
```
- [X] another task
```
- [X] another task

cannot be unchecked. Thus, the second commit fixes that issue as well.

## Checklist

The [developer guide](https://forgejo.org/docs/next/developer/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests

- I added test coverage for Go changes...
  - [ ] in their respective `*_test.go` for unit tests.
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
  - [ ] in `web_src/js/*.test.js` if it can be unit tested.
  - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Co-authored-by: Robert Wolff <mahlzahn@posteo.de>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4735
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2024-07-31 14:16:11 +00:00 committed by Earl Warren
parent 8493433cfb
commit 118762dd6d
3 changed files with 56 additions and 7 deletions

View file

@ -153,8 +153,8 @@ type Issue struct {
}
var (
issueTasksPat = regexp.MustCompile(`(^\s*[-*]\s\[[\sxX]\]\s.)|(\n\s*[-*]\s\[[\sxX]\]\s.)`)
issueTasksDonePat = regexp.MustCompile(`(^\s*[-*]\s\[[xX]\]\s.)|(\n\s*[-*]\s\[[xX]\]\s.)`)
issueTasksPat = regexp.MustCompile(`(^|\n)\s*[-*]\s*\[[\sxX]\]`)
issueTasksDonePat = regexp.MustCompile(`(^|\n)\s*[-*]\s*\[[xX]\]`)
)
// IssueIndex represents the issue index table

View file

@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"path"
"regexp"
"strconv"
"strings"
"testing"
@ -237,9 +238,13 @@ func testNewIssue(t *testing.T, session *TestSession, user, repo, title, content
htmlDoc = NewHTMLParser(t, resp.Body)
val := htmlDoc.doc.Find("#issue-title-display").Text()
assert.Contains(t, val, title)
// test for first line only and if it contains only letters and spaces
contentFirstLine := strings.Split(content, "\n")[0]
patNotLetterOrSpace := regexp.MustCompile(`[^\p{L}\s]`)
if len(contentFirstLine) != 0 && !patNotLetterOrSpace.MatchString(contentFirstLine) {
val = htmlDoc.doc.Find(".comment .render-content p").First().Text()
assert.Equal(t, content, val)
assert.Equal(t, contentFirstLine, val)
}
return issueURL
}
@ -282,6 +287,50 @@ func TestNewIssue(t *testing.T) {
testNewIssue(t, session, "user2", "repo1", "Title", "Description")
}
func TestIssueCheckboxes(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user2")
issueURL := testNewIssue(t, session, "user2", "repo1", "Title", `- [x] small x
- [X] capital X
- [ ] empty
- [x]x without gap
- [ ]empty without gap
- [x]
x on new line
- [ ]
empty on new line
- [ ] tabs instead of spaces
Description`)
req := NewRequest(t, "GET", issueURL)
resp := session.MakeRequest(t, req, http.StatusOK)
issueContent := NewHTMLParser(t, resp.Body).doc.Find(".comment .render-content").First()
isCheckBox := func(i int, s *goquery.Selection) bool {
typeVal, typeExists := s.Attr("type")
return typeExists && typeVal == "checkbox"
}
isChecked := func(i int, s *goquery.Selection) bool {
_, checkedExists := s.Attr("checked")
return checkedExists
}
checkBoxes := issueContent.Find("input").FilterFunction(isCheckBox)
assert.Equal(t, 8, checkBoxes.Length())
assert.Equal(t, 4, checkBoxes.FilterFunction(isChecked).Length())
// Issues list should show the correct numbers of checked and total checkboxes
repo, err := repo_model.GetRepositoryByOwnerAndName(db.DefaultContext, "user2", "repo1")
assert.NoError(t, err)
req = NewRequestf(t, "GET", "%s/issues", repo.Link())
resp = MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
issuesSelection := htmlDoc.Find("#issue-list .flex-item")
assert.Equal(t, "4 / 8", strings.TrimSpace(issuesSelection.Find(".checklist").Text()))
value, _ := issuesSelection.Find("progress").Attr("value")
vmax, _ := issuesSelection.Find("progress").Attr("max")
assert.Equal(t, "4", value)
assert.Equal(t, "8", vmax)
}
func TestIssueDependencies(t *testing.T) {
defer tests.PrepareTestEnv(t)()

View file

@ -32,11 +32,11 @@ export function initMarkupTasklist() {
const buffer = encoder.encode(oldContent);
// Indexes may fall off the ends and return undefined.
if (buffer[position - 1] !== '['.codePointAt(0) ||
buffer[position] !== ' '.codePointAt(0) && buffer[position] !== 'x'.codePointAt(0) ||
buffer[position] !== ' '.codePointAt(0) && buffer[position] !== 'x'.codePointAt(0) && buffer[position] !== 'X'.codePointAt(0) ||
buffer[position + 1] !== ']'.codePointAt(0)) {
// Position is probably wrong. Revert and don't allow change.
checkbox.checked = !checkbox.checked;
throw new Error(`Expected position to be space or x and surrounded by brackets, but it's not: position=${position}`);
throw new Error(`Expected position to be space, x or X and surrounded by brackets, but it's not: position=${position}`);
}
buffer.set(encoder.encode(checkboxCharacter), position);
const newContent = new TextDecoder().decode(buffer);