Allow to control popup links target

This commit is contained in:
Yohan Boniface 2023-06-21 12:19:01 +02:00
parent 2a04af162a
commit 3f8db1191f
3 changed files with 16 additions and 7 deletions

View file

@ -73,8 +73,9 @@ L.Util.escapeHTML = (s) => {
}) })
return s return s
} }
L.Util.toHTML = (r) => { L.Util.toHTML = (r, options) => {
if (!r) return '' if (!r) return ''
const target = options && options.target || 'blank'
let ii let ii
// detect newline format // 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') // 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')
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(/(=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, '<a target="_blank" href="$1">$1</a>') r = r.replace(/(https?:[^ \<)\n]*)/g, `<a target="_${target}" href="$1">$1</a>`)
r = r.replace(/\[\[(h_t_t_ps?:[^\]|]*?)\]\]/g, '<a target="_blank" href="$1">$1</a>') r = r.replace(/\[\[(h_t_t_ps?:[^\]|]*?)\]\]/g, `<a target="_${target}" href="$1">$1</a>`)
r = r.replace( r = r.replace(
/\[\[(h_t_t_ps?:[^|]*?)\|(.*?)\]\]/g, /\[\[(h_t_t_ps?:[^|]*?)\|(.*?)\]\]/g,
'<a target="_blank" href="$1">$2</a>' `<a target="_${target}" href="$1">$2</a>`
) )
r = r.replace(/\[\[([^\]|]*?)\]\]/g, '<a href="$1">$1</a>') r = r.replace(/\[\[([^\]|]*?)\]\]/g, `<a target="_${target}" href="$1">$1</a>`)
r = r.replace(/\[\[([^|]*?)\|(.*?)\]\]/g, '<a href="$1">$2</a>') r = r.replace(/\[\[([^|]*?)\|(.*?)\]\]/g, `<a target="_${target}" href="$1">$2</a>`)
// iframe // iframe
r = r.replace( r = r.replace(

View file

@ -98,6 +98,7 @@ L.U.PopupTemplate.Default = L.Class.extend({
renderBody: function () { renderBody: function () {
const template = this.feature.getOption('popupContentTemplate') const template = this.feature.getOption('popupContentTemplate')
const target = this.feature.getOption('outlinkTarget')
const container = L.DomUtil.create('div', 'umap-popup-container') const container = L.DomUtil.create('div', 'umap-popup-container')
let content = '' let content = ''
let properties let properties
@ -109,7 +110,7 @@ L.U.PopupTemplate.Default = L.Class.extend({
properties properties
) )
content = L.Util.greedyTemplate(template, properties) content = L.Util.greedyTemplate(template, properties)
content = L.Util.toHTML(content) content = L.Util.toHTML(content, {target: target})
container.innerHTML = content container.innerHTML = content
return container return container
}, },

View file

@ -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 <a href="http://osm.org" target="_self">http://osm.org</a> link'
)
})
it('should handle image', function () { it('should handle image', function () {
assert.equal( assert.equal(
L.Util.toHTML('A simple image: {{http://osm.org/pouet.png}}'), L.Util.toHTML('A simple image: {{http://osm.org/pouet.png}}'),