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

Editor#toggleMarkup accepts attributes #569

Merged
merged 1 commit into from
Aug 10, 2017
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
5 changes: 3 additions & 2 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,11 +855,12 @@ class Editor {
* (aka a non-collapsed range), the selections' markup will be toggled.
* If the editor is not focused and has no active range, nothing happens.
* @param {String} markup E.g. "b", "em", "a"
* @param {Object} [attributes={}] E.g. {href: "http://bustle.com"}
* @public
* @see PostEditor#toggleMarkup
*/
toggleMarkup(markup) {
markup = this.builder.createMarkup(markup);
toggleMarkup(markup, attributes={}) {
markup = this.builder.createMarkup(markup, attributes);
let { range } = this;
if (range.isCollapsed) {
this._editState.toggleMarkupState(markup);
Expand Down
7 changes: 2 additions & 5 deletions src/js/editor/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ export function toggleLink(editor, showPrompt=defaultShowPrompt) {
let hasLink = editor.detectMarkupInRange(range, 'a');

if (hasLink) {
editor.run(postEditor => postEditor.toggleMarkup('a'));
editor.toggleMarkup('a');
} else {
showPrompt('Enter a URL', defaultUrl, url => {
if (!url) { return; }

editor.run(postEditor => {
let markup = postEditor.builder.createMarkup('a', {href: url});
postEditor.toggleMarkup(markup);
});
editor.toggleMarkup('a', {href: url});
});
}
}
2 changes: 1 addition & 1 deletion tests/helpers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function selectRange(startNode, startOffset, endNode, endOffset) {

function selectText(editor,
startText,
startContainingElement,
startContainingElement=editor.element,
endText=startText,
endContainingElement=startContainingElement) {

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/editor/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,33 @@ test('#insertCard focuses the cursor at the end of the card', (assert) => {
assert.positionIsEqual(range.tail, insertedCard.tailPosition(), 'range tail on card tail');
assert.ok(document.activeElement === editorElement, 'editor element retains focus');
});

test('#toggleMarkup removes A tag when no attributes given', function(assert) {
editor = Helpers.mobiledoc.renderInto(editorElement,
({post, markupSection, marker, markup}) => {
return post([markupSection('p', [
marker('^'), marker('link', [markup('a', {href: 'google.com'})]), marker('$')
])]);
});
Helpers.dom.selectText(editor, 'link');
editor.toggleMarkup('a');

assert.selectedText('link', 'text "link" still selected');
assert.ok(editor.hasCursor(), 'editor has cursor');
assert.hasElement('#editor p:contains(^link$)');
assert.hasNoElement('#editor a', 'a tag is removed');
});

test('#toggleMarkup adds A tag with attributes', function(assert) {
editor = Helpers.mobiledoc.renderInto(editorElement,
({post, markupSection, marker, markup}) => {
return post([markupSection('p', [marker('^link$')])]);
});
Helpers.dom.selectText(editor, 'link');
editor.toggleMarkup('a', {href: 'google.com'});

assert.selectedText('link', 'text "link" still selected');
assert.ok(editor.hasCursor(), 'editor has cursor');
assert.hasElement('#editor a:contains(link)');
assert.hasElement('#editor a[href="google.com"]:contains(link)');
});