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

Fix #1216 from previous PR #1217 #1293

Merged
merged 6 commits into from
Feb 8, 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
54 changes: 50 additions & 4 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,59 @@ describe('Anchor Button TestCase', function () {
expect(link.classList.contains('btn-default')).toBe(true);
});

it('should create a button when user selects this option, even if selected text carries tags ' +
'and the selection doesn\'t exactly match those tags', function () {
spyOn(MediumEditor.prototype, 'createLink').and.callThrough();
this.el.innerHTML = '<p>Hello <b>world</b>&nbsp;!</p>';

var editor = this.newMediumEditor('.editor', {
anchor: {
customClassOption: 'btn btn-default'
}
}),
p = this.el.querySelector('p'),
b = p.childNodes[1],
emptySpacePart = p.childNodes[2],
save,
input,
button,
link,
opts,
anchorExtension = editor.getExtensionByName('anchor'),
toolbar = editor.getExtensionByName('toolbar');

MediumEditor.selection.select(document, b, 0, emptySpacePart, 1); //select world including space
save = toolbar.getToolbarElement().querySelector('[data-action="createLink"]');

input = anchorExtension.getInput();
input.value = 'http://test.com';

button = anchorExtension.getForm().querySelector('input.medium-editor-toolbar-anchor-button');
button.setAttribute('type', 'checkbox');
button.checked = true;

fireEvent(anchorExtension.getForm().querySelector('a.medium-editor-toolbar-save'), 'click');

opts = {
value: 'http://test.com',
target: '_self',
buttonClass: 'btn btn-default'
};
expect(editor.createLink).toHaveBeenCalledWith(opts);

link = editor.elements[0].querySelector('a');
expect(link).not.toBeNull('Link does not exist');
expect(link.classList.contains('btn')).toBe(true, 'Link does not contain class btn');
expect(link.classList.contains('btn-default')).toBe(true, 'Link does not contain class btn-default');
});

it('should remove the target _blank from the anchor tag when the open in a new window checkbox,' +
' is unchecked and the form is saved', function () {
var editor = this.newMediumEditor('.editor', {
anchor: {
targetCheckbox: true
}
}),
anchor: {
targetCheckbox: true
}
}),
anchorExtension = editor.getExtensionByName('anchor'),
targetCheckbox,
link;
Expand Down
13 changes: 12 additions & 1 deletion src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@
}
},

/*
* this function adds one or several classes on an a element.
* if el parameter is not an a, it will look for a children of el.
* if no a children are found, it will look for the a parent.
*/
addClassToAnchors: function (el, buttonClass) {
var classes = buttonClass.split(' '),
i,
Expand All @@ -622,7 +627,13 @@
el.classList.add(classes[j]);
}
} else {
el = el.getElementsByTagName('a');
var aChildren = el.getElementsByTagName('a');
if (aChildren.length === 0) {
var parentAnchor = Util.getClosestTag(el, 'a');
el = parentAnchor ? [parentAnchor] : [];
} else {
el = aChildren;
}
for (i = 0; i < el.length; i += 1) {
for (j = 0; j < classes.length; j += 1) {
el[i].classList.add(classes[j]);
Expand Down