From 3f8db1191f5bba05a27d84d37c35c78a1ca49ad3 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Wed, 21 Jun 2023 12:19:01 +0200 Subject: [PATCH] Allow to control popup links target --- umap/static/umap/js/umap.core.js | 13 +++++++------ umap/static/umap/js/umap.popup.js | 3 ++- umap/static/umap/test/Util.js | 7 +++++++ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/umap/static/umap/js/umap.core.js b/umap/static/umap/js/umap.core.js index 84ef5f2e..4e5ad8bb 100644 --- a/umap/static/umap/js/umap.core.js +++ b/umap/static/umap/js/umap.core.js @@ -73,8 +73,9 @@ L.Util.escapeHTML = (s) => { }) return s } -L.Util.toHTML = (r) => { +L.Util.toHTML = (r, options) => { if (!r) return '' + const target = options && options.target || 'blank' let ii // detect newline format @@ -100,14 +101,14 @@ L.Util.toHTML = (r) => { r = r.replace(/(\[\[http)/g, '[[h_t_t_p') // Escape for avoiding clash between [[http://xxx]] and http://xxx r = r.replace(/({{http)/g, '{{h_t_t_p') r = r.replace(/(=http)/g, '=h_t_t_p') // http://xxx as query string, see https://github.com/umap-project/umap/issues/607 - r = r.replace(/(https?:[^ \<)\n]*)/g, '$1') - r = r.replace(/\[\[(h_t_t_ps?:[^\]|]*?)\]\]/g, '$1') + r = r.replace(/(https?:[^ \<)\n]*)/g, `$1`) + r = r.replace(/\[\[(h_t_t_ps?:[^\]|]*?)\]\]/g, `$1`) r = r.replace( /\[\[(h_t_t_ps?:[^|]*?)\|(.*?)\]\]/g, - '$2' + `$2` ) - r = r.replace(/\[\[([^\]|]*?)\]\]/g, '$1') - r = r.replace(/\[\[([^|]*?)\|(.*?)\]\]/g, '$2') + r = r.replace(/\[\[([^\]|]*?)\]\]/g, `$1`) + r = r.replace(/\[\[([^|]*?)\|(.*?)\]\]/g, `$2`) // iframe r = r.replace( diff --git a/umap/static/umap/js/umap.popup.js b/umap/static/umap/js/umap.popup.js index b65807f0..173cba39 100644 --- a/umap/static/umap/js/umap.popup.js +++ b/umap/static/umap/js/umap.popup.js @@ -98,6 +98,7 @@ L.U.PopupTemplate.Default = L.Class.extend({ renderBody: function () { const template = this.feature.getOption('popupContentTemplate') + const target = this.feature.getOption('outlinkTarget') const container = L.DomUtil.create('div', 'umap-popup-container') let content = '' let properties @@ -109,7 +110,7 @@ L.U.PopupTemplate.Default = L.Class.extend({ properties ) content = L.Util.greedyTemplate(template, properties) - content = L.Util.toHTML(content) + content = L.Util.toHTML(content, {target: target}) container.innerHTML = content return container }, diff --git a/umap/static/umap/test/Util.js b/umap/static/umap/test/Util.js index 311e25a5..8ed8e6d9 100644 --- a/umap/static/umap/test/Util.js +++ b/umap/static/umap/test/Util.js @@ -84,6 +84,13 @@ describe('L.Util', function () { ) }) + it('should handle target option', function () { + assert.equal( + L.Util.toHTML('A simple http://osm.org link', {target: 'self'}), + 'A simple http://osm.org link' + ) + }) + it('should handle image', function () { assert.equal( L.Util.toHTML('A simple image: {{http://osm.org/pouet.png}}'),