diff --git a/lib/src/reader/sheet_stream.dart b/lib/src/reader/sheet_stream.dart index e8d5408..5f35514 100644 --- a/lib/src/reader/sheet_stream.dart +++ b/lib/src/reader/sheet_stream.dart @@ -137,21 +137,12 @@ extension SheetStream on Excel { if (file == null) return; file.decompress(); - final xmlStr = utf8.decode(file.content); - final start = xmlStr.indexOf('', start); - if (openEnd == -1) return; - // carries no rows at all. - if (xmlStr[openEnd - 1] == '/') return; - final end = xmlStr.indexOf('', openEnd); - if (end == -1) return; - - yield* _streamSheetData(xmlStr.substring(openEnd + 1, end)); + yield* _streamWorksheet(utf8.decode(file.content)); } - /// SAX-walks the inner content of `` and yields one list per row. - Iterable> _streamSheetData(String inner) sync* { + /// Lazily walks XML events inside `sheetData`, without building a tree or + /// matching tag-like text in comments. Stops at the end of the cell data. + Iterable> _streamWorksheet(String xml) sync* { var row = []; var col = -1; String? cellRef; @@ -161,8 +152,21 @@ extension SheetStream on Excel { final valueBuf = StringBuffer(); StringBuffer? formulaBuf; var inRow = false; + var inSheetData = false; - for (final event in parseEvents('$inner')) { + for (final event in parseEvents(xml)) { + if (!inSheetData) { + if (event is XmlStartElementEvent && + _localName(event.name) == 'sheetData') { + if (event.isSelfClosing) return; + inSheetData = true; + } + continue; + } + if (event is XmlEndElementEvent && + _localName(event.name) == 'sheetData') { + return; + } if (event is XmlStartElementEvent) { switch (_localName(event.name)) { case 'row': diff --git a/test/sheet_stream_test.dart b/test/sheet_stream_test.dart index cd68bab..e502685 100644 --- a/test/sheet_stream_test.dart +++ b/test/sheet_stream_test.dart @@ -22,6 +22,62 @@ List> _streamedRows(Excel excel, String sheet) => excel.streamRows(sheet).toList(); void main() { + group('Streaming Namespace Parity', () { + test('accepts whitespace in a prefixed closing tag', () { + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '' + '42' + '', + }, + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + + test('ignores a prefixed sheetData tag inside a comment', () { + final bytes = buildXlsx( + '42', + beforeSheetData: '', + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + + test('reads a worksheet with every element namespace prefixed', () { + const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '' + '42' + '', + }, + ); + final excel = Excel.decodeBytes(bytes); + expect(excel.streamRows('Sheet1').toList(), [ + [const IntCellValue(42)], + ]); + }); + + test('yields no rows for a prefixed worksheet with empty sheetData', () { + const ns = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; + final bytes = buildXlsx( + '', + extraParts: { + 'xl/worksheets/sheet1.xml': + '', + }, + ); + expect(Excel.decodeBytes(bytes).streamRows('Sheet1').toList(), isEmpty); + }); + }); + group('Streaming Row Read', () { test('agrees with the eager reader on a generated workbook', () { final excel = Excel.createExcel();