@codemirror/state 6.3.2 → 6.3.3

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,9 @@
1
+ ## 6.3.3 (2023-12-06)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix an issue where `Text.slice` and `Text.replace` could return objects with incorrect `length` when the given `from`/`to` values were out of range for the text.
6
+
1
7
  ## 6.3.2 (2023-11-27)
2
8
 
3
9
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -24,6 +24,7 @@ class Text {
24
24
  Replace a range of the text with the given content.
25
25
  */
26
26
  replace(from, to, text) {
27
+ [from, to] = clip(this, from, to);
27
28
  let parts = [];
28
29
  this.decompose(0, from, parts, 2 /* Open.To */);
29
30
  if (text.length)
@@ -41,6 +42,7 @@ class Text {
41
42
  Retrieve the text between the given points.
42
43
  */
43
44
  slice(from, to = this.length) {
45
+ [from, to] = clip(this, from, to);
44
46
  let parts = [];
45
47
  this.decompose(from, to, parts, 0);
46
48
  return TextNode.from(parts, to - from);
@@ -167,6 +169,7 @@ class TextLeaf extends Text {
167
169
  replace(from, to, text) {
168
170
  if (!(text instanceof TextLeaf))
169
171
  return super.replace(from, to, text);
172
+ [from, to] = clip(this, from, to);
170
173
  let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
171
174
  let newLen = this.length + text.length - (to - from);
172
175
  if (lines.length <= 32 /* Tree.Branch */)
@@ -174,6 +177,7 @@ class TextLeaf extends Text {
174
177
  return TextNode.from(TextLeaf.split(lines, []), newLen);
175
178
  }
176
179
  sliceString(from, to = this.length, lineSep = "\n") {
180
+ [from, to] = clip(this, from, to);
177
181
  let result = "";
178
182
  for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
179
183
  let line = this.text[i], end = pos + line.length;
@@ -242,6 +246,7 @@ class TextNode extends Text {
242
246
  }
243
247
  }
244
248
  replace(from, to, text) {
249
+ [from, to] = clip(this, from, to);
245
250
  if (text.lines < this.lines)
246
251
  for (let i = 0, pos = 0; i < this.children.length; i++) {
247
252
  let child = this.children[i], end = pos + child.length;
@@ -264,6 +269,7 @@ class TextNode extends Text {
264
269
  return super.replace(from, to, text);
265
270
  }
266
271
  sliceString(from, to = this.length, lineSep = "\n") {
272
+ [from, to] = clip(this, from, to);
267
273
  let result = "";
268
274
  for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
269
275
  let child = this.children[i], end = pos + child.length;
@@ -551,6 +557,10 @@ class Line {
551
557
  */
552
558
  get length() { return this.to - this.from; }
553
559
  }
560
+ function clip(text, from, to) {
561
+ from = Math.max(0, Math.min(text.length, from));
562
+ return [from, Math.max(from, Math.min(text.length, to))];
563
+ }
554
564
 
555
565
  // Compressed representation of the Grapheme_Cluster_Break=Extend
556
566
  // information from
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ class Text {
22
22
  Replace a range of the text with the given content.
23
23
  */
24
24
  replace(from, to, text) {
25
+ [from, to] = clip(this, from, to);
25
26
  let parts = [];
26
27
  this.decompose(0, from, parts, 2 /* Open.To */);
27
28
  if (text.length)
@@ -39,6 +40,7 @@ class Text {
39
40
  Retrieve the text between the given points.
40
41
  */
41
42
  slice(from, to = this.length) {
43
+ [from, to] = clip(this, from, to);
42
44
  let parts = [];
43
45
  this.decompose(from, to, parts, 0);
44
46
  return TextNode.from(parts, to - from);
@@ -165,6 +167,7 @@ class TextLeaf extends Text {
165
167
  replace(from, to, text) {
166
168
  if (!(text instanceof TextLeaf))
167
169
  return super.replace(from, to, text);
170
+ [from, to] = clip(this, from, to);
168
171
  let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
169
172
  let newLen = this.length + text.length - (to - from);
170
173
  if (lines.length <= 32 /* Tree.Branch */)
@@ -172,6 +175,7 @@ class TextLeaf extends Text {
172
175
  return TextNode.from(TextLeaf.split(lines, []), newLen);
173
176
  }
174
177
  sliceString(from, to = this.length, lineSep = "\n") {
178
+ [from, to] = clip(this, from, to);
175
179
  let result = "";
176
180
  for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
177
181
  let line = this.text[i], end = pos + line.length;
@@ -240,6 +244,7 @@ class TextNode extends Text {
240
244
  }
241
245
  }
242
246
  replace(from, to, text) {
247
+ [from, to] = clip(this, from, to);
243
248
  if (text.lines < this.lines)
244
249
  for (let i = 0, pos = 0; i < this.children.length; i++) {
245
250
  let child = this.children[i], end = pos + child.length;
@@ -262,6 +267,7 @@ class TextNode extends Text {
262
267
  return super.replace(from, to, text);
263
268
  }
264
269
  sliceString(from, to = this.length, lineSep = "\n") {
270
+ [from, to] = clip(this, from, to);
265
271
  let result = "";
266
272
  for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
267
273
  let child = this.children[i], end = pos + child.length;
@@ -549,6 +555,10 @@ class Line {
549
555
  */
550
556
  get length() { return this.to - this.from; }
551
557
  }
558
+ function clip(text, from, to) {
559
+ from = Math.max(0, Math.min(text.length, from));
560
+ return [from, Math.max(from, Math.min(text.length, to))];
561
+ }
552
562
 
553
563
  // Compressed representation of the Grapheme_Cluster_Break=Extend
554
564
  // information from
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/state",
3
- "version": "6.3.2",
3
+ "version": "6.3.3",
4
4
  "description": "Editor state data structures for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",