test-changed-range.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import ist from "ist"
  2. import {schema, doc, p} from "prosemirror-test-builder"
  3. import {Transform} from "prosemirror-transform"
  4. import {Node} from "prosemirror-model"
  5. import {ChangeSet} from "prosemirror-changeset"
  6. function mk(doc: Node, change: (tr: Transform) => Transform): {doc0: Node, tr: Transform, data: any[], set0: ChangeSet, set: ChangeSet} {
  7. let tr = change(new Transform(doc))
  8. let data = new Array(tr.steps.length).fill("a")
  9. let set0 = ChangeSet.create(doc)
  10. return {doc0: doc, tr, data, set0,
  11. set: set0.addSteps(tr.doc, tr.mapping.maps, data)}
  12. }
  13. function same(a: any, b: any) {
  14. ist(JSON.stringify(a), JSON.stringify(b))
  15. }
  16. describe("ChangeSet.changedRange", () => {
  17. it("returns null for identical sets", () => {
  18. let {set, doc0, tr, data} = mk(doc(p("foo")), tr => tr
  19. .replaceWith(2, 3, schema.text("aaaa"))
  20. .replaceWith(1, 1, schema.text("xx"))
  21. .delete(5, 7))
  22. ist(set.changedRange(set), null)
  23. ist(set.changedRange(ChangeSet.create(doc0).addSteps(tr.doc, tr.mapping.maps, data)), null)
  24. })
  25. it("returns only the changed range in simple cases", () => {
  26. let {set0, set, tr} = mk(doc(p("abcd")), tr => tr.replaceWith(2, 4, schema.text("u")))
  27. same(set0.changedRange(set, tr.mapping.maps), {from: 2, to: 3})
  28. })
  29. it("expands to cover updated spans", () => {
  30. let {doc0, set0, set, tr} = mk(doc(p("abcd")), tr => tr
  31. .replaceWith(2, 2, schema.text("c"))
  32. .delete(3, 5))
  33. let set1 = ChangeSet.create(doc0).addSteps(tr.docs[1], [tr.mapping.maps[0]], ["a"])
  34. same(set0.changedRange(set1, [tr.mapping.maps[0]]), {from: 2, to: 3})
  35. same(set1.changedRange(set, [tr.mapping.maps[1]]), {from: 2, to: 3})
  36. })
  37. it("detects changes in deletions", () => {
  38. let {set} = mk(doc(p("abc")), tr => tr.delete(2, 3))
  39. same(set.changedRange(set.map(() => "b")), {from: 2, to: 2})
  40. })
  41. })