Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache range next nodes before modifying the range #179

Merged
merged 1 commit into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 8 additions & 4 deletions src/plugins/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,15 @@
// If user pressed `Enter`, get the next editable node at position 0,
// otherwise set the cursor at the next character of the link (the white space)
if (this._currentKeyCode === KEY_ENTER) {
range.setEnd(range.getNextEditableNode(), 0);
range.setStart(range.getNextEditableNode(), 0);
var nextEditableNode = range.getNextEditableNode();

range.setStart(nextEditableNode, 0);
range.setEnd(nextEditableNode, 0);
} else {
range.setEnd(range.getNextNode(), 1);
range.setStart(range.getNextNode(), 1);
var nextNode = range.getNextNode();

range.setStart(nextNode, 1);
range.setEnd(nextNode, 1);
}

range.select();
Expand Down
36 changes: 36 additions & 0 deletions src/ui/react/test/autolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@
});
});

it('should create a link at the end of the content when pressing SPACE', function() {
testLinkAtEnd.call(this, KEY_SPACE);
});

it('should create a link at the end of the content when pressing COMMA', function() {
testLinkAtEnd.call(this, KEY_COMMA);
});

it('should create a link at the end of the content when pressing SEMICOLON', function() {
testLinkAtEnd.call(this, KEY_SEMICOLON);
});

it('should create a link at the end of the content when pressing ENTER', function() {
testLinkAtEnd.call(this, KEY_ENTER);
});

function testLink(config) {
bender.tools.selection.setWithHtml(this.nativeEditor, config.html);

Expand All @@ -184,6 +200,26 @@
assert.equal(data, config.expected);
}

function testLinkAtEnd(keyCode) {
var linkText = 'www.liferay.com';

this.nativeEditor.setData(linkText);

var container = this.nativeEditor.element.getNextSourceNode().getNextSourceNode();

var range = this.nativeEditor.createRange();
range.setStart(container, linkText.length);
range.setEnd(container, linkText.length);

this.nativeEditor.getSelection().selectRanges([range]);

assert.doesNotThrow(
function() {
happen.keyup(this._editable, {keyCode: keyCode});
}.bind(this)
);
}

function getData() {
return bender.tools.getData(this.nativeEditor, {
compatHtml: true,
Expand Down