fix(docs): pair comment ids with the document order, not the spec order (#92)
Release / GoReleaser (push) Skipped
Tests / Secret scan (gitleaks) (push) Skipped
Tests / Test (Go 1.25) (push) Skipped
Tests / Test (Go stable) (push) Skipped
Tests / Secret scan (gitleaks) (pull_request) Successful in 5s
Tests / Test (Go 1.25) (pull_request) Successful in 22s
Tests / Test (Go stable) (pull_request) Successful in 37s
Release / GoReleaser (push) Skipped
Tests / Secret scan (gitleaks) (push) Skipped
Tests / Test (Go 1.25) (push) Skipped
Tests / Test (Go stable) (push) Skipped
Tests / Secret scan (gitleaks) (pull_request) Successful in 5s
Tests / Test (Go 1.25) (pull_request) Successful in 22s
Tests / Test (Go stable) (pull_request) Successful in 37s
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -124,6 +125,48 @@ func TestInjectDocxCommentsNoMatch(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestInjectDocxCommentsOrder guards the marker-id ↔ comments.xml pairing when
|
||||
// the spec order differs from the document order.
|
||||
func TestInjectDocxCommentsOrder(t *testing.T) {
|
||||
src := buildTestDocx(t)
|
||||
spec := []DocxComment{
|
||||
{Anchor: "Nothing relevant", Text: "comment B"},
|
||||
{Anchor: "The term is two years", Text: "comment A"},
|
||||
}
|
||||
out, applied, err := InjectDocxComments(src, "A", spec)
|
||||
if err != nil || applied != 2 {
|
||||
t.Fatalf("inject: applied=%d err=%v", applied, err)
|
||||
}
|
||||
doc := unzipPart(t, out, "word/document.xml")
|
||||
cmts := unzipPart(t, out, "word/comments.xml")
|
||||
|
||||
// id -> text from comments.xml
|
||||
byID := map[string]string{}
|
||||
for _, m := range regexp.MustCompile(`(?s)<w:comment w:id="(\d+)"[^>]*>(.*?)</w:comment>`).FindAllStringSubmatch(cmts, -1) {
|
||||
txt := strings.TrimSpace(stripTags(m[2]))
|
||||
byID[m[1]] = txt
|
||||
}
|
||||
// paragraph that starts with the term text must point at "comment A"
|
||||
for _, p := range regexp.MustCompile(`(?s)<w:p\b[^>]*>.*?</w:p>`).FindAllString(doc, -1) {
|
||||
if !strings.Contains(stripTags(p), "The term is two years") {
|
||||
continue
|
||||
}
|
||||
m := regexp.MustCompile(`<w:commentRangeStart w:id="(\d+)"/>`).FindStringSubmatch(p)
|
||||
if m == nil {
|
||||
t.Fatal("no commentRangeStart in the term paragraph")
|
||||
}
|
||||
if got := byID[m[1]]; got != "comment A" {
|
||||
t.Fatalf("term paragraph points at id %s = %q, want \"comment A\"", m[1], got)
|
||||
}
|
||||
return
|
||||
}
|
||||
t.Fatal("term paragraph not found")
|
||||
}
|
||||
|
||||
func stripTags(s string) string {
|
||||
return regexp.MustCompile(`<[^>]*>`).ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
func TestParseDocxComments(t *testing.T) {
|
||||
got, err := ParseDocxComments([]byte(`[{"anchor":"a","text":"b"}]`))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user