Skip to content

Commit

Permalink
This closes #2015, fix a v2.9.0 regression bug introduced by commit 7…
Browse files Browse the repository at this point in the history
…715c14

- Fix corrupted workbook generated by open the workbook generated by stream writer
- Update unit tests
  • Loading branch information
xuri committed Oct 25, 2024
1 parent af190c7 commit 0d5d1c5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
9 changes: 7 additions & 2 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,16 @@ func getRootElement(d *xml.Decoder) []xml.Attr {
case xml.StartElement:
tokenIdx++
if tokenIdx == 1 {
var ns bool
for i := 0; i < len(startElement.Attr); i++ {
if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value {
startElement.Attr[i] = NameSpaceSpreadSheet
if startElement.Attr[i].Value == NameSpaceSpreadSheet.Value &&
startElement.Attr[i].Name == NameSpaceSpreadSheet.Name {
ns = true
}
}
if !ns {
startElement.Attr = append(startElement.Attr, NameSpaceSpreadSheet)
}
return startElement.Attr
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ func TestBytesReplace(t *testing.T) {

func TestGetRootElement(t *testing.T) {
assert.Len(t, getRootElement(xml.NewDecoder(strings.NewReader(""))), 0)
// Test get workbook root element which all workbook XML namespace has prefix
f := NewFile()
d := f.xmlNewDecoder(bytes.NewReader([]byte(`<x:workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"></x:workbook>`)))
assert.Len(t, getRootElement(d), 3)
}

func TestSetIgnorableNameSpace(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pivotTable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestDeleteWorkbookPivotCache(t *testing.T) {
f := NewFile()
// Test delete workbook pivot table cache with unsupported workbook charset
f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset)
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.deleteWorkbookPivotCache(PivotTableOptions{pivotCacheXML: "pivotCache/pivotCacheDefinition1.xml"}), "XML syntax error on line 1: invalid UTF-8")

// Test delete workbook pivot table cache with unsupported workbook relationships charset
Expand Down
2 changes: 1 addition & 1 deletion sheet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ func TestMoveSheet(t *testing.T) {

// Test move sheet with unsupported workbook charset
f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset)
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.MoveSheet("Sheet2", "Sheet1"), "XML syntax error on line 1: invalid UTF-8")
}

Expand Down
2 changes: 1 addition & 1 deletion slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func TestAddWorkbookSlicerCache(t *testing.T) {
// Test add a workbook slicer cache with unsupported charset workbook
f := NewFile()
f.WorkBook = nil
f.Pkg.Store("xl/workbook.xml", MacintoshCyrillicCharset)
f.Pkg.Store(defaultXMLPathWorkbook, MacintoshCyrillicCharset)
assert.EqualError(t, f.addWorkbookSlicerCache(1, ExtURISlicerCachesX15), "XML syntax error on line 1: invalid UTF-8")
assert.NoError(t, f.Close())
}
Expand Down

0 comments on commit 0d5d1c5

Please sign in to comment.