fixes gpx import/export errors on Safari

This commit is contained in:
Christian Beutel
2025-01-10 16:17:19 +01:00
parent 3d953aa0e9
commit 5274d25199
3 changed files with 15 additions and 9 deletions

View File

@@ -119,11 +119,13 @@ export default class GPX {
}
static parse(gpxString: string): Promise<GPX | Error> {
return new Promise<GPX | Error>((resolve, reject) => xml2js.parseString(gpxString, {
const sanitizedGPX = gpxString.replace(/\sxmlns=""/g, '');
return new Promise<GPX | Error>((resolve, reject) => xml2js.parseString(sanitizedGPX, {
explicitArray: false,
attrValueProcessors: [(str: string | number) => {
if (!isNaN(Number(str))) {
str = Number.isInteger(Number(str)) ? parseInt(String(str), 10) : parseFloat(String(str));
attrValueProcessors: [(str: string) => {
if (str.length && !isNaN(Number(str))) {
return Number.isInteger(Number(str)) ? parseInt(String(str), 10) : parseFloat(String(str));
}
return str;
}
@@ -157,7 +159,10 @@ export default class GPX {
if (!xmlString.includes(`xmlns="${defaultAttributes["xmlns"]}"`)) {
xmlString = xmlString.replace('<gpx', `<gpx xmlns="${defaultAttributes["xmlns"]}"`);
}
// Safari for some ungodly reason adds empty xmlns attributes to every tag...
xmlString = xmlString.replace(/ xmlns=""/g, '');
return xmlString;
}
}