From 31c1148c38527dae14405cfb8da7b816f3bd143e Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 30 Sep 2021 16:43:35 +0100 Subject: [PATCH] Decode HTML entities for markup: false dynmap marker labels (Fixes: #172) --- src/util.ts | 7 +++++++ src/util/dynmap.ts | 33 ++++++++++++++++++++------------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/util.ts b/src/util.ts index d03defe..f614ab4 100644 --- a/src/util.ts +++ b/src/util.ts @@ -203,3 +203,10 @@ export const focus = (selector: string) => { (element as HTMLElement).focus(); } } + +const decodeTextarea = document.createElement('textarea'); + +export const decodeHTMLEntities = (text: string) => { + decodeTextarea.innerHTML = text; + return decodeTextarea.textContent; +} diff --git a/src/util/dynmap.ts b/src/util/dynmap.ts index b102ce5..0a0a981 100644 --- a/src/util/dynmap.ts +++ b/src/util/dynmap.ts @@ -28,7 +28,7 @@ import { LiveAtlasWorldDefinition } from "@/index"; import {getPoints} from "@/util/areas"; -import {endWorldNameRegex, netherWorldNameRegex, titleColoursRegex} from "@/util"; +import {decodeHTMLEntities, endWorldNameRegex, netherWorldNameRegex, titleColoursRegex} from "@/util"; import {getLinePoints} from "@/util/lines"; import LiveAtlasMapDefinition from "@/model/LiveAtlasMapDefinition"; @@ -263,21 +263,28 @@ export function buildMarkers(data: any): Map { return markers; } -export function buildMarker(marker: any): LiveAtlasMarker { - return Object.seal({ - label: marker.label || '', - isLabelHTML: marker.markup || false, +export function buildMarker(data: any): LiveAtlasMarker { + const marker = Object.seal({ + label: data.label || '', + isLabelHTML: data.markup || false, location: { - x: marker.x || 0, - y: marker.y || 0, - z: marker.z || 0, + x: data.x || 0, + y: data.y || 0, + z: data.z || 0, }, - dimensions: marker.dim ? marker.dim.split('x') : [16, 16], - icon: marker.icon || "default", - minZoom: typeof marker.minzoom !== 'undefined' && marker.minzoom > -1 ? marker.minzoom : undefined, - maxZoom: typeof marker.maxzoom !== 'undefined' && marker.maxzoom > -1 ? marker.maxzoom : undefined, - popupContent: marker.desc || undefined, + dimensions: data.dim ? data.dim.split('x') : [16, 16], + icon: data.icon || "default", + minZoom: typeof data.minzoom !== 'undefined' && data.minzoom > -1 ? data.minzoom : undefined, + maxZoom: typeof data.maxzoom !== 'undefined' && data.maxzoom > -1 ? data.maxzoom : undefined, + popupContent: data.desc || undefined, }); + + //Fix double escaping on non-HTML labels + if(!marker.isLabelHTML) { + marker.label = decodeHTMLEntities(marker.label); + } + + return marker; } export function buildAreas(data: any): Map {