Skip to content

Commit

Permalink
font fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gomutex committed Feb 11, 2024
1 parent 9c53771 commit 3b0c3f5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
23 changes: 10 additions & 13 deletions oxml/elements/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,19 @@ func NewFont(name, charset, family string, pitch types.FontPitchType) *Font {

func (f *Font) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "w:font"
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "name"}, Value: f.Name})
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "w:name"}, Value: f.Name})

err := e.EncodeToken(start)
if err != nil {
return err
}

err = e.EncodeElement(f.Charset, xml.StartElement{Name: xml.Name{Local: "w:charset"}})
if err != nil {
if err := e.EncodeElement(f.Family, xml.StartElement{Name: xml.Name{Local: "w:family"}}); err != nil {
return err
}

err = e.EncodeElement(f.Family, xml.StartElement{Name: xml.Name{Local: "w:family"}})
if err != nil {
return err
}
return e.EncodeToken(start.End())

err = e.EncodeToken(start.End())
if err != nil {
return err
}

return nil
}

// UnmarshalXML unmarshals XML to Font.
Expand All @@ -58,6 +48,13 @@ func (f *Font) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err
}

for _, attr := range start.Attr {
switch attr.Name.Local {
case "name":
f.Name = attr.Value
}
}

switch t := token.(type) {
case xml.StartElement:
switch t.Name {
Expand Down
35 changes: 35 additions & 0 deletions oxml/elements/font_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package elements

import (
"encoding/xml"
"testing"
)

func TestFont_UnmarshalXML(t *testing.T) {
xmlString := `<w:font w:name="Arial" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:charset w:val="00" />
<w:family w:val="swiss" />
<w:pitch w:val="variable" />
</w:font>`

var parsedFont Font
err := xml.Unmarshal([]byte(xmlString), &parsedFont)
if err != nil {
t.Fatalf("Error unmarshaling XML to Font: %v", err)
}

expectedFont := Font{
Name: "Arial",
Charset: Charset{Value: "00"},
Family: Family{Value: "swiss"},
Pitch: Pitch{Value: "variable"},
}

// Compare the parsedFont with the expectedFont
if parsedFont.Name != expectedFont.Name ||
parsedFont.Charset.Value != expectedFont.Charset.Value ||
parsedFont.Family.Value != expectedFont.Family.Value ||
parsedFont.Pitch.Value != expectedFont.Pitch.Value {
t.Errorf("Expected Font %v, got %v", expectedFont, parsedFont)
}
}

0 comments on commit 3b0c3f5

Please sign in to comment.