diff --git a/packages/app-cli/tests/models_BaseItem.js b/packages/app-cli/tests/models_BaseItem.js index 040a92c6a..b9e8c0b0b 100644 --- a/packages/app-cli/tests/models_BaseItem.js +++ b/packages/app-cli/tests/models_BaseItem.js @@ -105,7 +105,11 @@ describe('models_BaseItem', function() { })); it('should serialize and unserialize properties that contain new lines', asyncTest(async () => { - const note = await Note.save({ title: 'note', source_url: '\nhttps://joplinapp.org/\n' }); + const sourceUrl = ` +https://joplinapp.org/ \\n +`; + + const note = await Note.save({ title: 'note', source_url: sourceUrl }); const noteBefore = await Note.load(note.id); const serialized = await Note.serialize(noteBefore); @@ -113,4 +117,18 @@ describe('models_BaseItem', function() { expect(noteAfter).toEqual(noteBefore); })); + + it('should not serialize the note title and body', asyncTest(async () => { + const note = await Note.save({ title: 'my note', body: `one line +two line +three line \\n no escape` }); + + const noteBefore = await Note.load(note.id); + const serialized = await Note.serialize(noteBefore); + expect(serialized.indexOf(`my note + +one line +two line +three line \\n no escape`)).toBe(0); + })); }); diff --git a/packages/lib/models/BaseItem.js b/packages/lib/models/BaseItem.js index e88798d28..3125dee99 100644 --- a/packages/lib/models/BaseItem.js +++ b/packages/lib/models/BaseItem.js @@ -260,7 +260,13 @@ class BaseItem extends BaseModel { propValue = `${propValue}`; } - return propValue.replace(/\n/g, '\\n').replace(/\r/g, '\\r'); + if (propName === 'body') return propValue; + + return propValue + .replace(/\\n/g, '\\\\n') + .replace(/\\r/g, '\\\\r') + .replace(/\n/g, '\\n') + .replace(/\r/g, '\\r'); } static unserialize_format(type, propName, propValue) { @@ -281,7 +287,14 @@ class BaseItem extends BaseModel { propValue = Database.formatValue(ItemClass.fieldType(propName), propValue); } - return typeof propValue === 'string' ? propValue.replace(/\\n/g, '\n').replace(/\\r/g, '\r') : propValue; + if (propName === 'body') return propValue; + + return typeof propValue === 'string' ? propValue + .replace(/\\n/g, '\n') + .replace(/\\r/g, '\r') + .replace(/\\\n/g, '\\n') + .replace(/\\\r/g, '\\r') + : propValue; } static async serialize(item, shownKeys = null) {