From 3e759217547707137cf0d3cfb5b936bc8fbf135c Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Fri, 9 Apr 2021 13:59:49 +0200 Subject: [PATCH] Better handling of image detection when running from ts-node; add some tests --- README.md | 9 +-------- UI/i18n/Translation.ts | 12 +++++++++++- test/ImageAttribution.spec.ts | 28 +++++++++++++++++++++++++--- test/TestHelper.ts | 5 +++++ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 50ce713..a4b29c9 100644 --- a/README.md +++ b/README.md @@ -214,16 +214,9 @@ Park icon via http://www.onlinewebfonts.com/icon/425974, CC BY 3.0 (@sterankofra Forest icon via https://www.onlinewebfonts.com/icon/498112, CC BY -Statistics icon via https://www.onlinewebfonts.com/icon/197818 - Shower icon (used in 'bike_cleaning.svg'): https://commons.wikimedia.org/wiki/File:Shower_symbol.svg Bench icons from StreetComplete: https://github.com/westnordost/StreetComplete/tree/v25.0-beta1/res/graphics/quest%20icons, GPLv3.0 - -Urinal icon: https://thenounproject.com/term/urinal/1307984/ - -24/7 icon: https://www.vecteezy.com/vector-art/1394992-24-7-service-and-support-icon-set - -Translation-icon: https://commons.wikimedia.org/wiki/File:OOjs_UI_icon_language-ltr.svg \ No newline at end of file +Urinal icon: https://thenounproject.com/term/urinal/1307984/ \ No newline at end of file diff --git a/UI/i18n/Translation.ts b/UI/i18n/Translation.ts index c8a8b5a..cf0ca8f 100644 --- a/UI/i18n/Translation.ts +++ b/UI/i18n/Translation.ts @@ -125,12 +125,22 @@ export class Translation extends UIElement { if (isIcon) { const icons = render.split(";").filter(part => part.match(/(\.svg|\.png|\.jpg)$/) != null) allIcons.push(...icons) - } else if(!Utils.runningFromConsole){ + } else if (!Utils.runningFromConsole) { // This might be a tagrendering containing some img as html const htmlElement = document.createElement("div") htmlElement.innerHTML = render const images = Array.from(htmlElement.getElementsByTagName("img")).map(img => img.src) allIcons.push(...images) + } else { + // We are running this in ts-node (~= nodejs), and can not access document + // So, we fallback to simple regex + const matches = render.match(/]+>/g) + if (matches != null) { + const sources = matches.map(img => img.match(/src=("[^"]+"|'[^']+'|[^/ ]+)/)) + .filter(match => match != null) + .map(match => match[1].trim().replace(/^['"]/, '').replace(/['"]$/, '')); + allIcons.push(...sources) + } } } return allIcons.filter(icon => icon != undefined) diff --git a/test/ImageAttribution.spec.ts b/test/ImageAttribution.spec.ts index f234579..1292c07 100644 --- a/test/ImageAttribution.spec.ts +++ b/test/ImageAttribution.spec.ts @@ -18,17 +18,39 @@ import {And} from "../Logic/Tags/And"; import {ImageSearcher} from "../Logic/Actors/ImageSearcher"; import {AllKnownLayouts} from "../Customizations/AllKnownLayouts"; import AllKnownLayers from "../Customizations/AllKnownLayers"; +import LayerConfig from "../Customizations/JSON/LayerConfig"; new T("ImageAttribution Tests", [ [ "Should find all the images", () => { - const pumps = AllKnownLayers.sharedLayers["bike_repair_station"] - const expected = "./assets/layers/bike_repair_station/pump_example_manual.jpg" + const pumps: LayerConfig = AllKnownLayers.sharedLayers["bike_repair_station"] const images = pumps.ExtractImages(); + const expectedValues = ['./assets/layers/bike_repair_station/repair_station.svg', + './assets/layers/bike_repair_station/repair_station_pump.svg', + './assets/layers/bike_repair_station/broken_pump_2.svg', + './assets/layers/bike_repair_station/pump.svg', + './assets/themes/cyclofix/fietsambassade_gent_logo_small.svg', + './assets/layers/bike_repair_station/pump_example_manual.jpg', + './assets/layers/bike_repair_station/pump_example.png', + './assets/layers/bike_repair_station/pump_example_round.jpg', + './assets/layers/bike_repair_station/repair_station_example.jpg'] + for (const expected of expectedValues) { + T.isTrue(images.has(expected), expected + " not found") + } + } + ], + [ + "Test image discovery regex", + () => { + const tr = new Translation({en: "XYZ XYZ XYZ "}) + const images = new Set(tr.ExtractImages(false)); + equal(3, images.size) + T.isTrue(images.has("a.svg"), "a.svg not found") + T.isTrue(images.has("b.svg"), "b.svg not found") + T.isTrue(images.has("some image.svg"), "some image.svg not found") - equal(images.length, 5, "The pump example was not found") } ] diff --git a/test/TestHelper.ts b/test/TestHelper.ts index 9587284..4232f2c 100644 --- a/test/TestHelper.ts +++ b/test/TestHelper.ts @@ -25,4 +25,9 @@ export default class T { } } + static isTrue(b: boolean, msg: string) { + if(!b){ + throw "Expected true, but got false: "+msg + } + } }