Skip to content

Commit

Permalink
Support to run native unlink directly
Browse files Browse the repository at this point in the history
  • Loading branch information
easylogic committed Jan 14, 2020
1 parent 702fcb6 commit 0f5e10a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/js/base/module/Editor.js
Expand Up @@ -807,17 +807,9 @@ export default class Editor {
* @type command
*/
unlink() {
let rng = this.getLastRange();
if (rng.isOnAnchor()) {
const anchor = dom.ancestor(rng.sc, dom.isAnchor);
rng = range.createFromNode(anchor);
rng.select();
this.setLastRange();

this.beforeCommand();
document.execCommand('unlink');
this.afterCommand();
}
this.beforeCommand();
document.execCommand('unlink');
this.afterCommand();
}

/**
Expand Down
74 changes: 74 additions & 0 deletions test/base/module/Editor.spec.js
Expand Up @@ -411,6 +411,80 @@ describe('Editor', () => {
});
});


describe('editor.unlink', () => {
it('should remove all link on selection', () => {
var codes = [
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
'<p><a href="http://summernote.org">hello world</a></p>',
];

context.invoke('code', codes.join(''));
$editable.appendTo('body');

range.createFromNode($editable[0]).normalize().select();

// check creation normal link
editor.unlink();

expect($editable.find('a').length).to.equal(0);
});

it('should remove a link on anchor', () => {
var codes = [
'<p><a href="http://summernote.org">hello world</a></p>',
];

context.invoke('code', codes.join(''));
$editable.appendTo('body');

range.createFromNode($editable.find('a')[0]).normalize().select();

// check creation normal link
editor.unlink();

expect($editable.find('a').length).to.equal(0);
});

it('should remove a link on part anchor', () => {
var codes = [
'<p><a href="http://summernote.org">hello world</a></p>',
];

context.invoke('code', codes.join(''));
$editable.appendTo('body');

var anchorTextNode = $editable.find('a')[0].childNodes[0];

range.create(anchorTextNode, 1, anchorTextNode, 5).select();

// check creation normal link
editor.unlink();

expectContents(context, '<p><a href="http://summernote.org">h</a>ello<a href="http://summernote.org"> world</a></p>');
});

it('should remove links on multipart anchor', () => {
var codes = [
'<p><a href="http://summernote.org">hello world</a> middle text <a href="http://summernote.org">2hello world</a></p>',
];

context.invoke('code', codes.join(''));
$editable.appendTo('body');

var anchor1TextNode = $editable.find('a')[0].childNodes[0];
var anchor2TextNode = $editable.find('a')[1].childNodes[0];

range.create(anchor1TextNode, 1, anchor2TextNode, 2).select();

// check creation normal link
editor.unlink();

expectContents(context, '<p><a href="http://summernote.org">h</a>ello world middle text 2h<a href="http://summernote.org">ello world</a></p>');
});
});

describe('createLink', () => {
it('should create normal link', (done) => {
var text = 'hello';
Expand Down

0 comments on commit 0f5e10a

Please sign in to comment.