package docx_test import ( "archive/zip" "bytes" "io" "strings" "testing" "github.com/carlosframework/docs/internal/docx" "github.com/carlosframework/docs/internal/export" ) func part(t *testing.T, data []byte, name string) string { t.Helper() z, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) if err != nil { t.Fatal(err) } for _, f := range z.File { if f.Name == name { r, _ := f.Open() b, _ := io.ReadAll(r) return string(b) } } t.Fatalf("no %s in the .docx", name) return "" } // Paul's default 5: Carlito goes to Word as Calibri and Caladea as // Cambria, so the document breaks its lines in Word where it does here, // and both come back as themselves. Carlito is the default, so it is // the document's own default (w:docDefaults) and its runs carry no // mark either way. func TestCarlitoAndCaladeaRoundTripAsCalibriAndCambria(t *testing.T) { doc, err := export.Parse("", `{"type":"doc","content":[ {"type":"heading","attrs":{"level":2},"content":[{"type":"text","text":"Minutes"}]}, {"type":"paragraph","content":[ {"type":"text","text":"Body text, "}, {"type":"text","text":"bold","marks":[{"type":"strong"}]}, {"type":"text","text":" and a Cambria twin","marks":[{"type":"font_caladea"},{"type":"em"}]}, {"type":"text","text":" and Inter at 14","marks":[{"type":"font_inter"},{"type":"size_14"}]}, {"type":"text","text":" and Arial's twin","marks":[{"type":"font_sans"}]} ]}]}`) if err != nil { t.Fatal(err) } data, err := docx.Write(doc) if err != nil { t.Fatal(err) } styles := part(t, data, "word/styles.xml") if !strings.Contains(styles, ``) { t.Errorf("fontTable.xml does not name %s", name) } } if !strings.Contains(table, ``) { t.Error("Inter does not tell Word what to fall back to") } res, err := docx.Import(data) if err != nil { t.Fatal(err) } back := res.Document if len(res.Report.Items) > 0 { t.Errorf("a document Docs wrote imports with losses: %+v", res.Report.Items) } runs := back.Root.Content[1].Content want := []struct { text string marks string }{ {"Body text, ", ""}, {"bold", "strong"}, {" and a Cambria twin", "font_caladea em"}, {" and Inter at 14", "font_inter size_14"}, {" and Arial's twin", "font_sans"}, } if back.Root.Content[0].Type != "heading" || back.Root.Content[0].Attrs.Level != 2 { t.Errorf("the heading came back as %+v", back.Root.Content[0]) } if len(runs) != len(want) { t.Fatalf("got %d runs back, want %d: %+v", len(runs), len(want), runs) } for i, w := range want { var marks []string for _, m := range runs[i].Marks { marks = append(marks, m.Type) } if runs[i].Text != w.text || strings.Join(marks, " ") != w.marks { t.Errorf("run %d came back %q %v, want %q [%s]", i, runs[i].Text, marks, w.text, w.marks) } } } // A Word document whose own default is Calibri, written by Word, is a // Carlito document here; one whose default is Cambria is Caladea // throughout; and a face Docs does not have is reported for the import // report rather than silently replaced. func TestImportingWordsDefaults(t *testing.T) { for _, c := range []struct { defaultFace, runFace string marks string unknown bool }{ {"Calibri", "", "", false}, {"Cambria", "", "font_caladea", false}, {"Calibri", "Cambria", "font_caladea", false}, {"Cambria", "Calibri", "", false}, {"Calibri", "Wingdings", "", true}, } { data := wordFile(t, c.defaultFace, c.runFace) res, err := docx.Import(data) if err != nil { t.Fatal(err) } doc, unknown := res.Document, substituted(res.Report) var marks []string for _, m := range doc.Root.Content[0].Content[0].Marks { marks = append(marks, m.Type) } if strings.Join(marks, " ") != c.marks { t.Errorf("default %s, run %q: marks %v, want [%s]", c.defaultFace, c.runFace, marks, c.marks) } if unknown != c.unknown { t.Errorf("default %s, run %q: substitution reported %v, want %v", c.defaultFace, c.runFace, unknown, c.unknown) } } } // wordFile is a minimal .docx the way Word writes one: the default in // styles.xml, an optional run override. func wordFile(t *testing.T, defaultFace, runFace string) []byte { t.Helper() var buf bytes.Buffer z := zip.NewWriter(&buf) w, _ := z.Create("word/styles.xml") io.WriteString(w, ``) rpr := "" if runFace != "" { rpr = `` } w, _ = z.Create("word/document.xml") io.WriteString(w, ``+rpr+`Hello`) z.Close() return buf.Bytes() } // substituted is whether the report says a face was substituted. func substituted(r docx.Report) bool { for _, it := range r.Items { if it.Code == docx.CodeFontSubstituted { return true } } return false }