diff --git a/src/replace_step.ts b/src/replace_step.ts index ed5c704..c94992c 100644 --- a/src/replace_step.ts +++ b/src/replace_step.ts @@ -134,14 +134,8 @@ export class ReplaceAroundStep extends Step { map(mapping: Mappable) { let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1) - let gapFrom: number, gapTo: number - if (this.gapFrom == this.from && this.gapTo == this.to) { - gapFrom = mapping.map(this.gapFrom, 1) - gapTo = mapping.map(this.gapTo, -1) - } else { - gapFrom = mapping.map(this.gapFrom, -1) - gapTo = mapping.map(this.gapTo, 1) - } + let gapFrom = this.from == this.gapFrom ? from.pos : mapping.map(this.gapFrom, -1) + let gapTo = this.to == this.gapTo ? to.pos : mapping.map(this.gapTo, 1) if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos) return null return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure) } diff --git a/test/test-replace_step.ts b/test/test-replace_step.ts index 7b0f2a7..85e25b2 100644 --- a/test/test-replace_step.ts +++ b/test/test-replace_step.ts @@ -1,28 +1,26 @@ -import ist from "ist"; -import { Fragment, Slice } from "prosemirror-model"; -import { blockquote, p } from "prosemirror-test-builder"; -import { ReplaceAroundStep, StepMap } from "prosemirror-transform"; +import ist from "ist" +import {Node} from "prosemirror-model" +import {doc, blockquote, p, schema, eq} from "prosemirror-test-builder" +import {Transform} from "prosemirror-transform" -describe("ReplaceAroundStep", () => { - it("can map if its from is positive", () => { - let slice = new Slice(Fragment.from(blockquote()), 0, 0); - // Wrap the content between 10 and 20 in a blockquote - let step = new ReplaceAroundStep(10, 20, 10, 20, slice, 1, true); - let mappedStep = step.map(StepMap.offset(100)); - ist(mappedStep?.from, 110); - ist(mappedStep?.to, 120); - ist(mappedStep?.gapFrom, 110); - ist(mappedStep?.gapTo, 120); - }); +describe("ReplaceAroundStep.map", () => { + function test(doc: Node, change: (tr: Transform) => void, otherChange: (tr: Transform) => void, expected: Node) { + let trA = new Transform(doc), trB = new Transform(doc) + change(trA) + otherChange(trB) + let result = new Transform(trB.doc).step(trA.steps[0].map(trB.mapping)!).doc + ist(result, expected, eq) + } - it("can map if its from is 0", () => { - let slice = new Slice(Fragment.from(blockquote()), 0, 0); - // Wrap the content between 0 and 20 in a blockquote - let step = new ReplaceAroundStep(0, 20, 0, 20, slice, 1, true); - let mappedStep = step.map(StepMap.offset(100)); - ist(mappedStep?.from, 100); - ist(mappedStep?.to, 120); - ist(mappedStep?.gapFrom, 100); - ist(mappedStep?.gapTo, 120); - }); -}); + it("doesn't break wrap steps on insertions", () => + test(doc(p("a")), + tr => tr.wrap(tr.doc.resolve(1).blockRange()!, [{type: schema.nodes.blockquote}]), + tr => tr.insert(0, p("b")), + doc(p("b"), blockquote(p("a"))))) + + it("doesn't overwrite content inserted at start of unwrap step", () => + test(doc(blockquote(p("a"))), + tr => tr.lift(tr.doc.resolve(2).blockRange()!, 0), + tr => tr.insert(2, schema.text("x")), + doc(p("xa")))) +})