Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/features/route/RoutePopup.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ export function RoutePopup({ end, inline = false, ...props }) {
}

function DownloadRouteGPX({ route }) {
const escapeXml = (value = '') =>
String(value).replace(
/[<>&'"]/g,
(c) =>
({
'<': '&lt;',
'>': '&gt;',
'&': '&amp;',
"'": '&apos;',
'"': '&quot;',
})[c],
)

const sanitizeFilename = (name = '') =>
String(name)
.replace(/[\/:*?"<>|]/g, '')
.slice(0, 200) || 'route'

const GPXContent = React.useMemo(() => {
if (!route.waypoints.length) {
return null
Expand All @@ -396,8 +414,8 @@ function DownloadRouteGPX({ route }) {
return `<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="ReactMap" xmlns="http://www.topografix.com/GPX/1/1">
<rte>
<name>${route.name}</name>
<desc>${route.description}</desc>
<name>${escapeXml(route.name)}</name>
<desc>${escapeXml(route.description)}</desc>
${route.waypoints
.map(
(waypoint) =>
Expand All @@ -417,7 +435,7 @@ function DownloadRouteGPX({ route }) {
href={`data:application/gpx;charset=utf-8,${encodeURIComponent(
GPXContent,
)}`}
download={`${route.name}.gpx`}
download={`${sanitizeFilename(route.name)}.gpx`}
size="small"
style={{ color: 'inherit' }}
>
Expand Down
Loading