From b8e0f3662ab0be847c066c500c7afbf5fc9a3a9e Mon Sep 17 00:00:00 2001 From: Kuba Orlik Date: Thu, 8 Aug 2024 21:42:07 +0200 Subject: [PATCH] Decode html entities in map with pins --- .../map-with-pins/map-with-pins.stimulus.ts | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/back/jdd-components/map-with-pins/map-with-pins.stimulus.ts b/src/back/jdd-components/map-with-pins/map-with-pins.stimulus.ts index cce67a2..8bc642d 100644 --- a/src/back/jdd-components/map-with-pins/map-with-pins.stimulus.ts +++ b/src/back/jdd-components/map-with-pins/map-with-pins.stimulus.ts @@ -19,6 +19,26 @@ function parseCoords(s: string): [number, number] { return s.split(", ").map((x) => parseFloat(x)) as [number, number]; } +function decodeHTMLEntities(text) { + var entities = [ + ["amp", "&"], + ["apos", "'"], + ["#x27", "'"], + ["#x2F", "/"], + ["#39", "'"], + ["#47", "/"], + ["lt", "<"], + ["gt", ">"], + ["nbsp", " "], + ["quot", '"'], + ]; + + for (var i = 0, max = entities.length; i < max; ++i) + text = text.replace(new RegExp("&" + entities[i][0] + ";", "g"), entities[i][1]); + + return text; +} + export default class MapWithPins extends Controller { id: string; map: L.Map; @@ -70,7 +90,9 @@ export default class MapWithPins extends Controller { // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const pins = JSON.parse( // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument - this.element.attributes["data-map-with-pins-pins-value"].value + decodeHTMLEntities( + this.element.attributes["data-map-with-pins-pins-value"].value + ) ) as Pin[]; pins.forEach((pin) => this.addPin(pin)); this.initiated = true;