Skip to content

Commit 1511e56

Browse files
balazsskjbalsas
authored andcommitted
Adding on page bookmark support to links
Fixes #785
1 parent e56044e commit 1511e56

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/core/link.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(function() {
22
'use strict';
33

4+
var REGEX_BOOKMARK_SCHEME = /^#.*/i;
45
var REGEX_EMAIL_SCHEME = /^[a-z0-9\u0430-\u044F\._-]+@/i;
56
var REGEX_URI_SCHEME = /^(?:[a-z][a-z0-9+\-.]*)\:|^\//i;
67

@@ -214,8 +215,9 @@
214215
},
215216

216217
/**
217-
* Checks if the URI has an '@' symbol. If it does and the URI looks like an email
218-
* and doesn't have 'mailto:', 'mailto:' is added to the URI.
218+
* Checks if the URI begins with a '#' symbol to determine if it's an on page bookmark.
219+
* If it doesn't, it then checks if the URI has an '@' symbol. If it does and the URI
220+
* looks like an email and doesn't have 'mailto:', 'mailto:' is added to the URI.
219221
* If it doesn't and the URI doesn't have a scheme, the default 'http' scheme with
220222
* hierarchical path '//' is added to the URI.
221223
*
@@ -227,7 +229,9 @@
227229
* @return {String} The URI updated with the protocol.
228230
*/
229231
_getCompleteURI: function(URI) {
230-
if (REGEX_EMAIL_SCHEME.test(URI)) {
232+
if (REGEX_BOOKMARK_SCHEME.test(URI)) {
233+
return URI;
234+
} else if (REGEX_EMAIL_SCHEME.test(URI)) {
231235
URI = 'mailto:' + URI;
232236
} else if (!REGEX_URI_SCHEME.test(URI)) {
233237
URI = this.appendProtocol ? 'http://' + URI : URI;

test/core/test/link.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,23 @@
8282
assert.strictEqual(data, '<p>set a <a href="//test.com" target="_blank">selection</a> and then convert it to a link.</p>');
8383
});
8484

85+
it('should not add default protocol if link is an on page bookmark', function() {
86+
var link = new CKEDITOR.Link(this.nativeEditor);
87+
88+
bender.tools.selection.setWithHtml(this.nativeEditor, 'set a {selection} and then convert it to a link.');
89+
90+
link.create('#bookmark', {
91+
target: '_blank'
92+
});
93+
94+
var data = bender.tools.getData(this.nativeEditor, {
95+
fixHtml: true,
96+
compatHtml: true
97+
});
98+
99+
assert.strictEqual(data, '<p>set a <a href="#bookmark" target="_blank">selection</a> and then convert it to a link.</p>');
100+
});
101+
85102
it('should add mailto: when creating an email link', function() {
86103
var link = new CKEDITOR.Link(this.nativeEditor);
87104

0 commit comments

Comments
 (0)