From a20d2f8c7ba66314855de9a1b3a8d93ff44d1632 Mon Sep 17 00:00:00 2001 From: Mark Amery Date: Mon, 8 Jan 2024 13:05:06 +0000 Subject: [PATCH 1/3] Add failing test showing maxEditLength: 0 is handled wrongly --- test/diff/line.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/diff/line.js b/test/diff/line.js index d38095f4..7b8b7fe7 100644 --- a/test/diff/line.js +++ b/test/diff/line.js @@ -64,6 +64,26 @@ describe('diff/line', function() { 'line\nnew value\nline', { callback, maxEditLength: 1 }); }); }); + + describe('given options.maxEditLength === 0', function() { + it('returns normally if the strings are identical', function() { + const diffResult = diffLines( + 'foo\nbar\nbaz\nqux\n', + 'foo\nbar\nbaz\nqux\n', + { maxEditLength: 0 } + ); + expect(convertChangesToXML(diffResult)).to.equal('foo\nbar\nbaz\nqux\n'); + }); + + it('terminates early if there is even a single change', function() { + const diffResult = diffLines( + 'foo\nbar\nbaz\nqux\n', + 'fox\nbar\nbaz\nqux\n', + { maxEditLength: 0 } + ); + expect(diffResult).to.be.undefined; + }); + }); }); // Trimmed Line Diff From c93f3bfca230a03af4c776f9631d9a3008663bce Mon Sep 17 00:00:00 2001 From: Mark Amery Date: Mon, 8 Jan 2024 13:05:50 +0000 Subject: [PATCH 2/3] Handle maxEditLength === 0 correctly (test now passes) --- src/diff/base.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/diff/base.js b/src/diff/base.js index 99f93d7a..8e7b4be7 100644 --- a/src/diff/base.js +++ b/src/diff/base.js @@ -30,7 +30,7 @@ Diff.prototype = { let newLen = newString.length, oldLen = oldString.length; let editLength = 1; let maxEditLength = newLen + oldLen; - if(options.maxEditLength) { + if(options.maxEditLength != null) { maxEditLength = Math.min(maxEditLength, options.maxEditLength); } From c706f774fa19488a3fe1f8f69e1357956e9bb21d Mon Sep 17 00:00:00 2001 From: Mark Amery Date: Mon, 8 Jan 2024 13:11:06 +0000 Subject: [PATCH 3/3] Document change --- release-notes.md | 1 + 1 file changed, 1 insertion(+) diff --git a/release-notes.md b/release-notes.md index 8c358580..e22311ce 100644 --- a/release-notes.md +++ b/release-notes.md @@ -7,6 +7,7 @@ - [#435](https://github.com/kpdecker/jsdiff/pull/435) Fix `parsePatch` handling of control characters. `parsePatch` used to interpret various unusual control characters - namely vertical tabs, form feeds, lone carriage returns without a line feed, and EBCDIC NELs - as line breaks when parsing a patch file. This was inconsistent with the behavior of both JsDiff's own `diffLines` method and also the Unix `diff` and `patch` utils, which all simply treat those control characters as ordinary characters. The result of this discrepancy was that some well-formed patches - produced either by `diff` or by JsDiff itself and handled properly by the `patch` util - would be wrongly parsed by `parsePatch`, with the effect that it would disregard the remainder of a hunk after encountering one of these control characters. - [#439](https://github.com/kpdecker/jsdiff/pull/439) Prefer diffs that order deletions before insertions. When faced with a choice between two diffs with an equal total edit distance, the Myers diff algorithm generally prefers one that does deletions before insertions rather than insertions before deletions. For instance, when diffing `abcd` against `acbd`, it will prefer a diff that says to delete the `b` and then insert a new `b` after the `c`, over a diff that says to insert a `c` before the `b` and then delete the existing `c`. JsDiff deviated from the published Myers algorithm in a way that led to it having the opposite preference in many cases, including that example. This is now fixed, meaning diffs output by JsDiff will more accurately reflect what the published Myers diff algorithm would output. - [#455](https://github.com/kpdecker/jsdiff/pull/455) The `added` and `removed` properties of change objects are now guaranteed to be set to a boolean value. (Previously, they would be set to `undefined` or omitted entirely instead of setting them to false.) +- [#464](https://github.com/kpdecker/jsdiff/pull/464) Specifying `{maxEditLength: 0}` now sets a max edit length of 0 instead of no maximum. ## Development