diff --git a/src/io/files.js b/src/io/files.js index 8dce5b6839..b161541e47 100644 --- a/src/io/files.js +++ b/src/io/files.js @@ -1668,7 +1668,7 @@ function files(p5, fn) { fn.saveJSON(args[0], args[1], args[2]); return; case 'txt': - fn.saveStrings(args[0], args[1], args[2]); + fn.saveStrings(args[0], args[1], args[2], args[3]); // Takes the third argument return; // ================================================= // OPTION 3: decide based on object... diff --git a/test/unit/io/files.js b/test/unit/io/files.js index 1c30776b8c..35481ab7b2 100644 --- a/test/unit/io/files.js +++ b/test/unit/io/files.js @@ -5,7 +5,7 @@ import { vi } from 'vitest'; const mockAnchorElement = vi.mockObject({ href: null, download: null, - click: () => {} + click: () => { } }); const originalCreateElement = document.createElement; vi.spyOn(document, 'createElement').mockImplementation((...args) => { @@ -191,6 +191,17 @@ suite('Files', function () { assert.equal(mockAnchorElement.download, 'filename.txt'); }); + test('should preserve CRLF when saving a text file', async () => { + const myStrings = ['aaa', 'bbb']; + mockP5Prototype.save(myStrings, 'filename', 'txt', true); + + const saveData = new Blob([myStrings.join('\r\n')]); + expect(document.createElement).toHaveBeenCalledTimes(1); + expect(mockAnchorElement.click).toHaveBeenCalledTimes(1); + expect(URL.createObjectURL).toHaveBeenCalledWith(saveData); + assert.equal(mockAnchorElement.download, 'filename.txt'); + }); + test('should download a json file', async () => { const myObj = { hi: 'hello' }; mockP5Prototype.save(myObj, 'filename.json');