Skip to content

Commit

Permalink
Merge pull request #1293 from yabwe/integration-1216
Browse files Browse the repository at this point in the history
Fix #1216 from previous PR #1217
  • Loading branch information
j0k3r committed Feb 8, 2017
2 parents fdf48fc + a6c5c71 commit ba69260
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
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

0 comments on commit ba69260

Please sign in to comment.