Skip to content

Commit

Permalink
fix for yabwe#939 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishi Jain committed Feb 11, 2016
1 parent 512f13f commit ded26d5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 4 additions & 4 deletions spec/anchor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('Anchor Button TestCase', function () {
});
expect(editor.createLink).toHaveBeenCalled();
// A trailing <br> may be added when insertHTML is used to add the link internally.
expect(this.el.innerHTML.indexOf('<a href="http://test.com">lorem ipsum</a>')).toBe(0);
expect(this.el.innerHTML.indexOf('<a href="http://test.com" target="_self">lorem ipsum</a>')).toBe(0);
});

it('should remove the extra white spaces in the link when user presses enter', function () {
Expand All @@ -87,7 +87,7 @@ describe('Anchor Button TestCase', function () {
});
expect(editor.createLink).toHaveBeenCalled();
// A trailing <br> may be added when insertHTML is used to add the link internally.
expect(this.el.innerHTML.indexOf('<a href="test">lorem ipsum</a>')).toBe(0);
expect(this.el.innerHTML.indexOf('<a href="test" target="_self">lorem ipsum</a>')).toBe(0);
});

it('should not set any href if all user passes is spaces in the link when user presses enter', function () {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe('Anchor Button TestCase', function () {
keyCode: MediumEditor.util.keyCode.ENTER
});
expect(editor.createLink).toHaveBeenCalled();
expect(this.el.innerHTML).toMatch(/^Hello world, <a href="http:\/\/test\.com\/?">this <strong>will become a link<\/strong><\/a><strong>, but this part won\'t\.<\/strong>(<br>|<strong><\/strong>)?$/);
expect(this.el.innerHTML).toMatch(/^Hello world, <a href="http:\/\/test\.com\/?" target="_self">this <strong>will become a link<\/strong><\/a><strong>, but this part won\'t\.<\/strong>(<br>|<strong><\/strong>)?$/);
});

it('should create a link when the user selects text within two paragraphs', function () {
Expand Down Expand Up @@ -582,7 +582,7 @@ describe('Anchor Button TestCase', function () {
// TODO: Find a better way to fix this issue if Edge 12 is going to matter
var edgeVersion = getEdgeVersion();
if (!edgeVersion || edgeVersion >= 13) {
expect(this.el.innerHTML).toContain('<a href="http://www.google.com"><img src="../demo/img/medium-editor.jpg"></a>');
expect(this.el.innerHTML).toContain('<a href="http://www.google.com" target="_self"><img src="../demo/img/medium-editor.jpg"></a>');
}
});
});
Expand Down
22 changes: 22 additions & 0 deletions spec/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ describe('MediumEditor.util', function () {
});
});

describe('settargetself', function () {
it('sets target self on a A element from a A element', function () {
var el = this.createElement('a', '', 'lorem ipsum');
el.attributes.href = 'http://0.0.0.0/bar.html';

MediumEditor.util.setTargetSelf(el, 'http://0.0.0.0/bar.html');

expect(el.target).toBe('_self');
});

it('sets target blank on a A element from a DIV element', function () {
var el = this.createElement('div', '', '<a href="http://1.1.1.1/foo.html">foo</a> <a href="http://0.0.0.0/bar.html">bar</a>');

MediumEditor.util.setTargetSelf(el, 'http://0.0.0.0/bar.html');

var nodes = el.getElementsByTagName('a');

expect(nodes[0].target).not.toBe('_self');
expect(nodes[1].target).toBe('_self');
});
});

describe('addClassToAnchors', function () {
it('add class to anchors on a A element from a A element', function () {
var el = this.createElement('a', '', 'lorem ipsum');
Expand Down
2 changes: 2 additions & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,8 @@

if (this.options.targetBlank || opts.target === '_blank') {
MediumEditor.util.setTargetBlank(MediumEditor.selection.getSelectionStart(this.options.ownerDocument), opts.url);
} else {
MediumEditor.util.setTargetSelf(MediumEditor.selection.getSelectionStart(this.options.ownerDocument), opts.url);
}

if (opts.buttonClass) {
Expand Down
19 changes: 19 additions & 0 deletions src/js/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,25 @@
}
},

/*
* this function is called to explicitly remove the target='_blank' and set it to '_self'
* as FF holds on to _blank value even after unchecking the checkbox on anchor form
*/
setTargetSelf: function (el, anchorUrl) {
var i;
if (el.nodeName.toLowerCase() === 'a') {
el.target = '_self';
} else {
el = el.getElementsByTagName('a');

for (i = 0; i < el.length; i += 1) {
if (anchorUrl === el[i].attributes.href.value) {
el[i].target = '_self';
}
}
}
},

addClassToAnchors: function (el, buttonClass) {
var classes = buttonClass.split(' '),
i,
Expand Down

0 comments on commit ded26d5

Please sign in to comment.