Permalink
Browse files

Add a substring function for the editor

  • Loading branch information...
1 parent 90ae502 commit a43ce7c9e1f34a38b71c5c335cd74547603e56dd @rhburrows committed Jul 7, 2011
Showing with 41 additions and 3 deletions.
  1. +22 −0 src/editor.js
  2. +2 −3 src/extras/selection.js
  3. +17 −0 test/editorTest.js
View
@@ -269,6 +269,28 @@
setFace : function(from, to, face) {
this.display.setFace(from, to, face);
+ },
+
+ slice : function(from, to) {
+ var start = "",
+ end = "";
+
+ if (from < this.presize) {
+ if (to < this.presize) {
+ start = this.buffer.slice(from, to).join('');
+ } else {
+ start = this.buffer.slice(from, this.presize).join('');
+ var postStart = this.size - this.postsize;
+ end = this.buffer.slice(postStart,
+ postStart + (to - this.presize)).join('');
+ }
+ } else {
+ var postStart = this.size - this.postsize;
+ end = this.buffer.slice(postStart + (from - this.presize),
+ postStart + (to - this.presize)).join('');
+ }
+
+ return start + end;
}
};
View
@@ -73,11 +73,10 @@
return null;
}
- var contents = this.contents();
if (this.selectionBegan < this.pointPosition()) {
- return contents.slice(this.selectionBegan, this.pointPosition());
+ return editor.slice(this.selectionBegan, this.pointPosition());
} else {
- return contents.slice(this.pointPosition(), this.selectionBegan);
+ return editor.slice(this.pointPosition(), this.selectionBegan);
}
};
View
@@ -438,4 +438,21 @@ test("lineCount", function(){
editor.insertString("ello\nThere\nFriend.");
equals(editor.lineCount(), 3, "It counts the number of lines with multiple");
+});
+
+test("slice", function(){
+ editor.insertString("Hello there");
+
+ equals(editor.slice(1,4), "ell",
+ "It works if the point is after the slice");
+
+ editor.movePointTo(0);
+
+ equals(editor.slice(1,4), "ell",
+ "It works if the point is before the slice");
+
+ editor.movePointTo(2);
+
+ equals(editor.slice(1,4), "ell",
+ "It works if the point is in the middle of the slice");
});

0 comments on commit a43ce7c

Please sign in to comment.