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

Add options for minimal links and auto-prefixing http protocol #727

Merged
merged 14 commits into from
Apr 9, 2018
Merged
85 changes: 66 additions & 19 deletions src/trumbowyg.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {

// imgDblClickHandler: default is defined in constructor

plugins: {}
plugins: {},
urlProtocol: false,
minimalLinks: false
},
writable: false,
enumerable: true,
Expand Down Expand Up @@ -398,6 +400,8 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
t.o.imgDblClickHandler = t.getDefaultImgDblClickHandler();
}

t.urlPrefix = t.setupUrlPrefix();

t.disabled = t.o.disabled || (editorElem.nodeName === 'TEXTAREA' && editorElem.disabled);

if (options.btns) {
Expand Down Expand Up @@ -470,6 +474,23 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
this.btnsDef[btnName] = btnDef;
},

setupUrlPrefix: function() {
var t = this,
protocol = t.o.urlProtocol;

if(!protocol) { return; }

if(typeof(protocol) === 'boolean') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (typeof protocol !== 'string') {

return 'https://'
} else if(typeof(protocol) === 'string') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need else as we always manage all others cases with the previous if

if(protocol.match(/^https?:\/\/$/)) { return protocol; }

if(protocol.match(/^https?$/)) {
return protocol + '://'
}
}
}

buildEditor: function () {
var t = this,
prefix = t.o.prefix,
Expand Down Expand Up @@ -1189,8 +1210,10 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
var $a = $(node);
text = $a.text();
url = $a.attr('href');
title = $a.attr('title');
target = $a.attr('target');
if (!t.o.minimalLinks) {
title = $a.attr('title');
target = $a.attr('target');
}
var range = t.doc.createRange();
range.selectNode(node);
documentSelection.removeAllRanges();
Expand All @@ -1199,31 +1222,43 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {

t.saveRange();

t.openModalInsert(t.lang.createLink, {
var options = {
url: {
label: 'URL',
required: true,
value: url
},
title: {
label: t.lang.title,
value: title
},
text: {
label: t.lang.text,
value: text
},
target: {
label: t.lang.target,
value: target
value: new XMLSerializer().serializeToString(documentSelection.getRangeAt(0).cloneContents())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is out of date and should stay text

}
}, function (v) { // v is value
};
if (!t.o.minimalLinks) {
Object.assign(options, {
title: {
label: t.lang.title,
value: title
},
target: {
label: t.lang.target,
value: target
}
});
}

t.openModalInsert(t.lang.createLink, options, function (v) { // v is value
var url = t.prependUrlPrefix(v.url);
if(!url.length) { return false; }

var link = $(['<a href="', v.url, '">', v.text || v.url, '</a>'].join(''));
if (v.title.length > 0) {
link.attr('title', v.title);
}
if (v.target.length > 0) {
link.attr('target', v.target);

if (!t.o.minimalLinks) {
if (v.title.length > 0) {
link.attr('title', v.title);
}
if (v.target.length > 0) {
link.attr('target', v.target);
}
}
t.range.deleteContents();
t.range.insertNode(link[0]);
Expand All @@ -1232,6 +1267,18 @@ Object.defineProperty(jQuery.trumbowyg, 'defaultOptions', {
return true;
});
},
prependUrlPrefix: function(url) {
var t = this;
if(!t.urlPrefix) { return url; }

const VALID_LINK_PREFIX = /^([a-z][-+.a-z0-9]*:|\/|#)/i;
if(VALID_LINK_PREFIX.test(url)) { return url; }

const SIMPLE_EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if(SIMPLE_EMAIL_REGEX.test(url)) { return 'mailto:' + url; }

return t.urlPrefix + url;
},
unlink: function () {
var t = this,
documentSelection = t.doc.getSelection(),
Expand Down