From 0c7e06a10c7196c24210afda2e43aa3f26d6dcbc Mon Sep 17 00:00:00 2001 From: "jeremy@jermolene.com" Date: Fri, 3 Mar 2023 18:22:35 +0000 Subject: [PATCH] Fix crash with invalid patches See https://github.com/Jermolene/TiddlyWiki5/pull/7290#issuecomment-1453155311 Thanks @yaisog --- core/modules/filters/strings.js | 10 ++++++++- .../tests/data/filters/DiffMergePatch3.tid | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 editions/test/tiddlers/tests/data/filters/DiffMergePatch3.tid diff --git a/core/modules/filters/strings.js b/core/modules/filters/strings.js index 30881880c..1e015ff46 100644 --- a/core/modules/filters/strings.js +++ b/core/modules/filters/strings.js @@ -95,8 +95,16 @@ exports.makepatches = makeStringBinaryOperator( exports.applypatches = makeStringBinaryOperator( function(a,b) { var dmpObject = new dmp.diff_match_patch(), + patches; + try { patches = dmpObject.patch_fromText(b); - return [dmpObject.patch_apply(patches,a)[0]]; + } catch(e) { + } + if(patches) { + return [dmpObject.patch_apply(patches,a)[0]]; + } else { + return [a]; + } } ); diff --git a/editions/test/tiddlers/tests/data/filters/DiffMergePatch3.tid b/editions/test/tiddlers/tests/data/filters/DiffMergePatch3.tid new file mode 100644 index 000000000..87d98e890 --- /dev/null +++ b/editions/test/tiddlers/tests/data/filters/DiffMergePatch3.tid @@ -0,0 +1,22 @@ +title: Filters/DiffMergePatch3 +description: Tests for diff-merge-patch derived operators +type: text/vnd.tiddlywiki-multiple +tags: [[$:/tags/wiki-test-spec]] + +title: Output + +\whitespace trim +\define text1() +the cat sat on the mat +\end + +\define patches() +**NOT A VALID PATCH** +\end + +<$text text={{{ [applypatches] }}}/> + ++ +title: ExpectedResult + +the cat sat on the mat \ No newline at end of file