From a97707d422bc154f36d0d4bc5fef7667ed07c2c7 Mon Sep 17 00:00:00 2001 From: Olivier Terral Date: Mon, 8 Jul 2024 12:00:55 +0200 Subject: [PATCH] fix: better management of textArray and whitespaces in kml placemark name --- src/utils/KML.js | 22 ++++++++++----- src/utils/KML.test.js | 63 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/utils/KML.js b/src/utils/KML.js index 2a078df9..9f2b20a1 100644 --- a/src/utils/KML.js +++ b/src/utils/KML.js @@ -469,9 +469,8 @@ const writeFeatures = (layer, featureProjection, mapResolution) => { clone.set("zIndex", newStyle.zIndex); } - // If we see spaces at the beginning or at the end we add a empty - // white space at the beginning and at the end. const text = newStyle.text?.getText(); + if (text) { let kmlText = text; @@ -480,13 +479,22 @@ const writeFeatures = (layer, featureProjection, mapResolution) => { clone.set("textArray", JSON.stringify(text)); // in the KML we just add the text without the bold or italic information - kmlText = text.map((t, idx) => { - return idx % 2 === 0 ? t : ""; - }); + kmlText = text + .map((t, idx) => { + return idx % 2 === 0 ? t : ""; + }) + .join(""); } - if (/^\s|\s$/g.test(kmlText)) { - newStyle.text.setText(`\u200B${kmlText}\u200B`); + // We add the current text as features's name so it will be added as Placemark's name in the kml + if (kmlText) { + // If we see spaces at the beginning or at the end we add a empty + // white space at the beginning and at the end. + if (/^\s|\s$/g.test(kmlText)) { + clone.set("name", `\u200B${kmlText}\u200B`); + } else { + clone.set("name", kmlText); + } } } diff --git a/src/utils/KML.test.js b/src/utils/KML.test.js index 9a51ccb7..cde2b862 100644 --- a/src/utils/KML.test.js +++ b/src/utils/KML.test.js @@ -150,7 +150,7 @@ describe("KML", () => { lala - bar + bar + + + bold 16px arial + + + + 0,0,0 + + + + + `; + const feats = KML.readFeatures(str); + const style = feats[0].getStyleFunction()(feats[0], 1); + expect(feats.length).toBe(1); + expect(style instanceof Style).toBe(true); + + // Text + const styleText = style.getText(); + expect(styleText.getText()).toBe(" bar "); // Avoid trim spaces using unicode \u200B + expectWriteResult(feats, str); + }); + test("should read/write TextStyle with textArray as ExtendedData.", () => { const str = ` lala - bar + \u200B\n foo \n\u200B - ["foo","bar","\\n",""] + ["\\n",""," ","","foo","bold 16px arial"," ","","\\n",""] bold 16px arial @@ -269,7 +308,21 @@ describe("KML", () => { // Text const styleText = style.getText(); - expect(styleText.getText()).toEqual(["foo", "bar", "\n", ""]); + + // Make sure it is an array, the toEqual on nextline is not working properly because the array is converted to a string for comparaison. + expect(Array.isArray(styleText.getText())).toBe(true); + expect(styleText.getText()).toEqual([ + "\n", + "", + " ", + "", + "foo", + "bold 16px arial", + " ", + "", + "\n", + "", + ]); expect(styleText.getFont()).toEqual("bold 16px arial"); expectWriteResult(feats, str); });