From 0d5d1c53b2bd33c68784f19fb8a4324ed68f372e Mon Sep 17 00:00:00 2001 From: xuri Date: Fri, 25 Oct 2024 08:52:59 +0800 Subject: [PATCH] This closes #2015, fix a v2.9.0 regression bug introduced by commit 7715c1462a917c657d4022a4fe5b57d41d77055a - Fix corrupted workbook generated by open the workbook generated by stream writer - Update unit tests --- lib.go | 9 +++++++-- lib_test.go | 4 ++++ pivotTable_test.go | 2 +- sheet_test.go | 2 +- slicer_test.go | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib.go b/lib.go index e01c305..08ce248 100644 --- a/lib.go +++ b/lib.go @@ -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 } } diff --git a/lib_test.go b/lib_test.go index 7500f49..4895528 100644 --- a/lib_test.go +++ b/lib_test.go @@ -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(``))) + assert.Len(t, getRootElement(d), 3) } func TestSetIgnorableNameSpace(t *testing.T) { diff --git a/pivotTable_test.go b/pivotTable_test.go index 20528cc..21c2a1d 100644 --- a/pivotTable_test.go +++ b/pivotTable_test.go @@ -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 diff --git a/sheet_test.go b/sheet_test.go index 47fcd97..89aa507 100644 --- a/sheet_test.go +++ b/sheet_test.go @@ -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") } diff --git a/slicer_test.go b/slicer_test.go index 5a79a80..df9e667 100644 --- a/slicer_test.go +++ b/slicer_test.go @@ -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()) }