Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #190 from bliker/createLink-bugs
Browse files Browse the repository at this point in the history
Tests for createlink bugs
  • Loading branch information
Oliver Joseph Ash committed Jun 13, 2014
2 parents 517b22a + 47d2b14 commit 7f2fcd8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions BROWSERINCONSISTENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Playground: http://jsbin.com/iwEWUXo/2/edit?js,console,output
- Chrome converts BRs to Ps: http://jsbin.com/zeti/2/edit?js,output
- Firefox does not perform transformation upon Ps containing BRs:
http://jsbin.com/yiyaq/1/edit?js,output
* "`createLink`"
- Firefox does not create A if selection is empty:
http://jsbin.com/tutufi/2/edit?js,output
* "`delete`":
- Given a selection across multiple P elements, Firefox will place the caret
outside of any P element: http://jsbin.com/xopivama/3/edit?js,output
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/core/patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ define([
'./patches/commands/insert-html',
'./patches/commands/insert-list',
'./patches/commands/outdent',
'./patches/commands/create-link',
'./patches/events'
], function (
boldCommand,
indentCommand,
insertHTMLCommand,
insertListCommands,
outdentCommand,
createLinkCommand,
events
) {

Expand All @@ -28,7 +30,8 @@ define([
indent: indentCommand,
insertHTML: insertHTMLCommand,
insertList: insertListCommands,
outdent: outdentCommand
outdent: outdentCommand,
createLink: createLinkCommand,
},
events: events
};
Expand Down
38 changes: 38 additions & 0 deletions src/plugins/core/patches/commands/create-link.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
define(function () {

'use strict';

return function () {
return function (scribe) {
var createLinkCommand = new scribe.api.CommandPatch('createLink');
scribe.commandPatches.createLink = createLinkCommand;

createLinkCommand.execute = function (value) {
var selection = new scribe.api.Selection();

/**
* Firefox does not create a link when selection is collapsed
* so we create is manually. http://jsbin.com/tutufi/2/edit?js,output
*/
if (selection.selection.isCollapsed) {
var aElement = document.createElement('a');
aElement.setAttribute('href', value);
aElement.textContent = value;

selection.range.insertNode(aElement);

// Select the created link
var newRange = document.createRange();
newRange.setStartBefore(aElement);
newRange.setEndAfter(aElement);

selection.selection.removeAllRanges();
selection.selection.addRange(newRange);
} else {
scribe.api.CommandPatch.prototype.execute.call(this, value);
}
};
};
};

});
1 change: 1 addition & 0 deletions src/scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ define([
this.use(patches.commands.insertHTML());
this.use(patches.commands.insertList());
this.use(patches.commands.outdent());
this.use(patches.commands.createLink());
this.use(patches.events());

this.use(commands.indent());
Expand Down
31 changes: 31 additions & 0 deletions test/commands.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,35 @@ describe('commands', function () {
});
});
});

describe('createLink', function () {
given('an empty editor', function () {
when('the command is executed', function () {
beforeEach(function () {
scribeNode.click();
executeCommand('createLink', '#');
});

it('should insert A with specified URL and content', function () {
return scribeNode.getInnerHTML().then(function (innerHTML) {
expect(innerHTML).to.have.html('<p><a href="#">#</a><bogus-br></p>');
});
});
});
});

givenContentOf('<p>|1|</p>', function () {
when('the command is executed', function () {
beforeEach(function () {
executeCommand('createLink', '#');
});

it('should wrap selection with A', function () {
return scribeNode.getInnerHTML().then(function (innerHTML) {
expect(innerHTML).to.have.html('<p><a href="#">1</a></p>');
});
});
});
});
});
});

0 comments on commit 7f2fcd8

Please sign in to comment.