prosemirror-transform 1.10.3 → 1.10.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 1.10.5 (2025-11-11)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix a bug in `liftTarget` that caused it to fail to properly check content constraints when a lift would split a node.
6
+
7
+ ## 1.10.4 (2025-04-22)
8
+
9
+ ### Bug fixes
10
+
11
+ Fix a bug that caused mapping `ReplaceStep`s to reset their `structure` flag.
12
+
13
+ Align `removeNodeMark`'s behavior to that of `removeMark`. When passing in a node type, it now removes all occurrences of that type.
14
+
1
15
  ## 1.10.3 (2025-03-04)
2
16
 
3
17
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -613,7 +613,7 @@ var ReplaceStep = function (_Step5) {
613
613
  var from = mapping.mapResult(this.from, 1),
614
614
  to = mapping.mapResult(this.to, -1);
615
615
  if (from.deletedAcross && to.deletedAcross) return null;
616
- return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
616
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
617
617
  }
618
618
  }, {
619
619
  key: "merge",
@@ -853,12 +853,14 @@ function canCut(node, start, end) {
853
853
  function liftTarget(range) {
854
854
  var parent = range.parent;
855
855
  var content = parent.content.cutByIndex(range.startIndex, range.endIndex);
856
- for (var depth = range.depth;; --depth) {
856
+ for (var depth = range.depth, contentBefore = 0, contentAfter = 0;; --depth) {
857
857
  var node = range.$from.node(depth);
858
- var index = range.$from.index(depth),
859
- endIndex = range.$to.indexAfter(depth);
858
+ var index = range.$from.index(depth) + contentBefore,
859
+ endIndex = range.$to.indexAfter(depth) - contentAfter;
860
860
  if (depth < range.depth && node.canReplace(index, endIndex, content)) return depth;
861
861
  if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex)) break;
862
+ if (index) contentBefore = 1;
863
+ if (endIndex < node.childCount) contentAfter = 1;
862
864
  }
863
865
  return null;
864
866
  }
@@ -1434,7 +1436,7 @@ function _replaceRange(tr, from, to, slice) {
1434
1436
  var $from = tr.doc.resolve(from),
1435
1437
  $to = tr.doc.resolve(to);
1436
1438
  if (fitsTrivially($from, $to, slice)) return tr.step(new ReplaceStep(from, to, slice));
1437
- var targetDepths = coveredDepths($from, tr.doc.resolve(to));
1439
+ var targetDepths = coveredDepths($from, $to);
1438
1440
  if (targetDepths[targetDepths.length - 1] == 0) targetDepths.pop();
1439
1441
  var preferredTarget = -($from.depth + 1);
1440
1442
  targetDepths.unshift(preferredTarget);
@@ -1796,13 +1798,20 @@ var Transform = function () {
1796
1798
  }, {
1797
1799
  key: "removeNodeMark",
1798
1800
  value: function removeNodeMark(pos, mark) {
1799
- if (!(mark instanceof prosemirrorModel.Mark)) {
1800
- var node = this.doc.nodeAt(pos);
1801
- if (!node) throw new RangeError("No node at position " + pos);
1802
- mark = mark.isInSet(node.marks);
1803
- if (!mark) return this;
1801
+ var node = this.doc.nodeAt(pos);
1802
+ if (!node) throw new RangeError("No node at position " + pos);
1803
+ if (mark instanceof prosemirrorModel.Mark) {
1804
+ if (mark.isInSet(node.marks)) this.step(new RemoveNodeMarkStep(pos, mark));
1805
+ } else {
1806
+ var set = node.marks,
1807
+ found,
1808
+ steps = [];
1809
+ while (found = mark.isInSet(set)) {
1810
+ steps.push(new RemoveNodeMarkStep(pos, found));
1811
+ set = found.removeFromSet(set);
1812
+ }
1813
+ for (var i = steps.length - 1; i >= 0; i--) this.step(steps[i]);
1804
1814
  }
1805
- this.step(new RemoveNodeMarkStep(pos, mark));
1806
1815
  return this;
1807
1816
  }
1808
1817
  }, {
package/dist/index.d.cts CHANGED
@@ -425,7 +425,7 @@ declare class Transform {
425
425
  */
426
426
  addNodeMark(pos: number, mark: Mark): this;
427
427
  /**
428
- Remove a mark (or a mark of the given type) from the node at
428
+ Remove a mark (or all marks of the given type) from the node at
429
429
  position `pos`.
430
430
  */
431
431
  removeNodeMark(pos: number, mark: Mark | MarkType): this;
package/dist/index.d.ts CHANGED
@@ -425,7 +425,7 @@ declare class Transform {
425
425
  */
426
426
  addNodeMark(pos: number, mark: Mark): this;
427
427
  /**
428
- Remove a mark (or a mark of the given type) from the node at
428
+ Remove a mark (or all marks of the given type) from the node at
429
429
  position `pos`.
430
430
  */
431
431
  removeNodeMark(pos: number, mark: Mark | MarkType): this;
package/dist/index.js CHANGED
@@ -714,7 +714,7 @@ class ReplaceStep extends Step {
714
714
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
715
715
  if (from.deletedAcross && to.deletedAcross)
716
716
  return null;
717
- return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
717
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure);
718
718
  }
719
719
  merge(other) {
720
720
  if (!(other instanceof ReplaceStep) || other.structure || this.structure)
@@ -982,13 +982,17 @@ can be lifted. Will not go across
982
982
  function liftTarget(range) {
983
983
  let parent = range.parent;
984
984
  let content = parent.content.cutByIndex(range.startIndex, range.endIndex);
985
- for (let depth = range.depth;; --depth) {
985
+ for (let depth = range.depth, contentBefore = 0, contentAfter = 0;; --depth) {
986
986
  let node = range.$from.node(depth);
987
- let index = range.$from.index(depth), endIndex = range.$to.indexAfter(depth);
987
+ let index = range.$from.index(depth) + contentBefore, endIndex = range.$to.indexAfter(depth) - contentAfter;
988
988
  if (depth < range.depth && node.canReplace(index, endIndex, content))
989
989
  return depth;
990
990
  if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex))
991
991
  break;
992
+ if (index)
993
+ contentBefore = 1;
994
+ if (endIndex < node.childCount)
995
+ contentAfter = 1;
992
996
  }
993
997
  return null;
994
998
  }
@@ -1639,7 +1643,7 @@ function replaceRange(tr, from, to, slice) {
1639
1643
  let $from = tr.doc.resolve(from), $to = tr.doc.resolve(to);
1640
1644
  if (fitsTrivially($from, $to, slice))
1641
1645
  return tr.step(new ReplaceStep(from, to, slice));
1642
- let targetDepths = coveredDepths($from, tr.doc.resolve(to));
1646
+ let targetDepths = coveredDepths($from, $to);
1643
1647
  // Can't replace the whole document, so remove 0 if it's present
1644
1648
  if (targetDepths[targetDepths.length - 1] == 0)
1645
1649
  targetDepths.pop();
@@ -2091,19 +2095,26 @@ class Transform {
2091
2095
  return this;
2092
2096
  }
2093
2097
  /**
2094
- Remove a mark (or a mark of the given type) from the node at
2098
+ Remove a mark (or all marks of the given type) from the node at
2095
2099
  position `pos`.
2096
2100
  */
2097
2101
  removeNodeMark(pos, mark) {
2098
- if (!(mark instanceof Mark)) {
2099
- let node = this.doc.nodeAt(pos);
2100
- if (!node)
2101
- throw new RangeError("No node at position " + pos);
2102
- mark = mark.isInSet(node.marks);
2103
- if (!mark)
2104
- return this;
2102
+ let node = this.doc.nodeAt(pos);
2103
+ if (!node)
2104
+ throw new RangeError("No node at position " + pos);
2105
+ if (mark instanceof Mark) {
2106
+ if (mark.isInSet(node.marks))
2107
+ this.step(new RemoveNodeMarkStep(pos, mark));
2108
+ }
2109
+ else {
2110
+ let set = node.marks, found, steps = [];
2111
+ while (found = mark.isInSet(set)) {
2112
+ steps.push(new RemoveNodeMarkStep(pos, found));
2113
+ set = found.removeFromSet(set);
2114
+ }
2115
+ for (let i = steps.length - 1; i >= 0; i--)
2116
+ this.step(steps[i]);
2105
2117
  }
2106
- this.step(new RemoveNodeMarkStep(pos, mark));
2107
2118
  return this;
2108
2119
  }
2109
2120
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-transform",
3
- "version": "1.10.3",
3
+ "version": "1.10.5",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/replace.ts CHANGED
@@ -338,7 +338,7 @@ export function replaceRange(tr: Transform, from: number, to: number, slice: Sli
338
338
  if (fitsTrivially($from, $to, slice))
339
339
  return tr.step(new ReplaceStep(from, to, slice))
340
340
 
341
- let targetDepths = coveredDepths($from, tr.doc.resolve(to))
341
+ let targetDepths = coveredDepths($from, $to)
342
342
  // Can't replace the whole document, so remove 0 if it's present
343
343
  if (targetDepths[targetDepths.length - 1] == 0) targetDepths.pop()
344
344
  // Negative numbers represent not expansion over the whole node at
@@ -42,7 +42,7 @@ export class ReplaceStep extends Step {
42
42
  map(mapping: Mappable) {
43
43
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
44
44
  if (from.deletedAcross && to.deletedAcross) return null
45
- return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice)
45
+ return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice, this.structure)
46
46
  }
47
47
 
48
48
  merge(other: Step) {
package/src/structure.ts CHANGED
@@ -15,12 +15,14 @@ function canCut(node: Node, start: number, end: number) {
15
15
  export function liftTarget(range: NodeRange): number | null {
16
16
  let parent = range.parent
17
17
  let content = parent.content.cutByIndex(range.startIndex, range.endIndex)
18
- for (let depth = range.depth;; --depth) {
18
+ for (let depth = range.depth, contentBefore = 0, contentAfter = 0;; --depth) {
19
19
  let node = range.$from.node(depth)
20
- let index = range.$from.index(depth), endIndex = range.$to.indexAfter(depth)
20
+ let index = range.$from.index(depth) + contentBefore, endIndex = range.$to.indexAfter(depth) - contentAfter
21
21
  if (depth < range.depth && node.canReplace(index, endIndex, content))
22
22
  return depth
23
23
  if (depth == 0 || node.type.spec.isolating || !canCut(node, index, endIndex)) break
24
+ if (index) contentBefore = 1
25
+ if (endIndex < node.childCount) contentAfter = 1
24
26
  }
25
27
  return null
26
28
  }
package/src/transform.ts CHANGED
@@ -198,16 +198,21 @@ export class Transform {
198
198
  return this
199
199
  }
200
200
 
201
- /// Remove a mark (or a mark of the given type) from the node at
201
+ /// Remove a mark (or all marks of the given type) from the node at
202
202
  /// position `pos`.
203
203
  removeNodeMark(pos: number, mark: Mark | MarkType): this {
204
- if (!(mark instanceof Mark)) {
205
- let node = this.doc.nodeAt(pos)
206
- if (!node) throw new RangeError("No node at position " + pos)
207
- mark = mark.isInSet(node.marks)!
208
- if (!mark) return this
204
+ let node = this.doc.nodeAt(pos)
205
+ if (!node) throw new RangeError("No node at position " + pos)
206
+ if (mark instanceof Mark) {
207
+ if (mark.isInSet(node.marks)) this.step(new RemoveNodeMarkStep(pos, mark))
208
+ } else {
209
+ let set = node.marks, found, steps: Step[] = []
210
+ while (found = mark.isInSet(set)) {
211
+ steps.push(new RemoveNodeMarkStep(pos, found))
212
+ set = found.removeFromSet(set)
213
+ }
214
+ for (let i = steps.length - 1; i >= 0; i--) this.step(steps[i])
209
215
  }
210
- this.step(new RemoveNodeMarkStep(pos, mark))
211
216
  return this
212
217
  }
213
218