Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,8 +1042,8 @@ define(function (require, exports, module) {
* Returns the currently selected text, or "" if no selection. Includes \n if the
* selection spans multiple lines (does NOT reflect the Document's line-endings style). By
* default, returns only the contents of the primary selection, unless `allSelections` is true.
* @param {boolean=} allSelections Whether to return the contents of all selections instead
* of just the primary selection. Default false.
* @param {boolean=} allSelections Whether to return the contents of all selections (separated
* by newlines) instead of just the primary selection. Default false.
* @return {!string} The selected text.
*/
Editor.prototype.getSelectedText = function (allSelections) {
Expand Down
38 changes: 27 additions & 11 deletions test/spec/Editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,24 +608,40 @@ define(function (require, exports, module) {
});

it("should return the contents of a single selection", function () {
myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 12});
expect(myEditor.getSelectedText()).toEqual("line");
myEditor._codeMirror.setSelection({line: 0, ch: 8}, {line: 0, ch: 14});
expect(myEditor.getSelectedText()).toEqual("line 0");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all of these tests, why don't you just check both cases of getSelectedText() ?

    expect(myEditor.getSelectedText()).toEqual(...);
    expect(myEditor.getSelectedText(true)).toEqual(...);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh, yes, that's better. Will fix.

});

it("should return the contents of a multiple selection, separated by newlines", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 12}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 12}}
it("should return the primary selection by default", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
]);
expect(myEditor.getSelectedText()).toEqual("line\nline\nline");
expect(myEditor.getSelectedText()).toEqual("line 2");
});

it("should return a primary selection other than the last", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
], 1);
expect(myEditor.getSelectedText()).toEqual("line 1");
});

it("should return the contents of a multiple selection separated by newlines if allSelections is true", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 8}, head: {line: 0, ch: 14}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 8}, head: {line: 2, ch: 14}}
]);
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
});

it("should return the contents of a multiple selection when some selections are reversed", function () {
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 12}, head: {line: 0, ch: 8}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 12}},
{anchor: {line: 2, ch: 12}, head: {line: 2, ch: 8}}
myEditor._codeMirror.setSelections([{anchor: {line: 0, ch: 14}, head: {line: 0, ch: 8}},
{anchor: {line: 1, ch: 8}, head: {line: 1, ch: 14}},
{anchor: {line: 2, ch: 14}, head: {line: 2, ch: 8}}
]);
expect(myEditor.getSelectedText()).toEqual("line\nline\nline");
expect(myEditor.getSelectedText(true)).toEqual("line 0\nline 1\nline 2");
});
});

Expand Down