From b7c80d12aa6315cf0be2d65610a2b439a0a8d461 Mon Sep 17 00:00:00 2001 From: zombieFox Date: Sun, 14 Jun 2020 16:15:01 +0100 Subject: [PATCH] [refactor] improve link string handling --- src/js/helper.js | 11 ++++++++++- src/js/link.js | 28 +++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/js/helper.js b/src/js/helper.js index 8f06f2ba..ebca0ef5 100644 --- a/src/js/helper.js +++ b/src/js/helper.js @@ -786,6 +786,14 @@ var helper = (function() { }; }; + var removeSpaces = function(value) { + if (typeof value == "string") { + return value.trim().replace(/\s/g, ""); + } else { + return value; + }; + }; + // exposed methods return { e: e, @@ -813,7 +821,8 @@ var helper = (function() { isHexNumber: isHexNumber, convertColor: convertColor, checkIfValidString: checkIfValidString, - trimString: trimString + trimString: trimString, + removeSpaces: removeSpaces }; })(); diff --git a/src/js/link.js b/src/js/link.js index ed3cdbdc..147c96dc 100644 --- a/src/js/link.js +++ b/src/js/link.js @@ -950,14 +950,22 @@ var link = (function() { attr: [{ key: "class", value: "link-panel-front" - }, { - key: "href", - value: stagedLink.link.url }, { key: "tabindex", value: 1 }] }; + if (helper.checkIfValidString(stagedLink.link.url)) { + linkPanelFrontOptions.attr.push({ + key: "href", + value: helper.removeSpaces(stagedLink.link.url) + }); + } else { + linkPanelFrontOptions.attr.push({ + key: "href", + value: "#" + }); + }; if (state.get.current().link.item.newTab) { linkPanelFrontOptions.attr.push({ key: "target", @@ -1016,21 +1024,21 @@ var link = (function() { }; var linkUrl = helper.node("div|class:link-url"); - var url; + var urlDisplayString; if (helper.checkIfValidString(stagedLink.link.url)) { - url = helper.trimString(stagedLink.link.url.replace(/^https?\:\/\//i, "").replace(/\/+$/, "")); + urlDisplayString = helper.removeSpaces(stagedLink.link.url.replace(/^https?\:\/\//i, "").replace(/\/+$/, "")); } else { - url = ""; + urlDisplayString = ""; }; var linkUrlText = helper.makeNode({ tag: "p", - text: url, + text: urlDisplayString, attr: [{ key: "class", value: "link-url-text" }, { key: "title", - value: url + value: urlDisplayString }] }); var linkControl = helper.node("div|class:link-control"); @@ -1077,7 +1085,9 @@ var link = (function() { linkControl.appendChild(linkEdit); linkRemove.appendChild(linkRemoveIcon); linkControl.appendChild(linkRemove); - linkUrl.appendChild(linkUrlText); + if (helper.checkIfValidString(stagedLink.link.url)) { + linkUrl.appendChild(linkUrlText); + }; linkPanelBack.appendChild(linkUrl); linkPanelBack.appendChild(linkControl); linkItem.appendChild(linkPanelFront);