fix: update isGoogleMapsUrl to also detect Amap URLs

- Add amap.com and uri.amap.com to URL detection
- Keep legacy Google Maps URL patterns for backward compatibility
- Update test expectations
This commit is contained in:
jubnl 2026-07-11 18:50:30 +08:00
parent 0e6fe5bcab
commit 9a34f1983b
2 changed files with 10 additions and 4 deletions

View File

@ -2,6 +2,12 @@ import { describe, it, expect } from 'vitest'
import { isGoogleMapsUrl } from './PlaceFormModal.helpers'
describe('isGoogleMapsUrl', () => {
it('accepts Amap URLs', () => {
expect(isGoogleMapsUrl('https://uri.amap.com/marker?position=1,2')).toBe(true)
expect(isGoogleMapsUrl('https://ditu.amap.com/search?query=test')).toBe(true)
expect(isGoogleMapsUrl('https://www.amap.com/dir')).toBe(true)
})
it('accepts the short share hosts', () => {
expect(isGoogleMapsUrl('https://maps.app.goo.gl/abc123')).toBe(true)
expect(isGoogleMapsUrl('https://goo.gl/maps/xyz')).toBe(true)

View File

@ -21,13 +21,13 @@ export function isGoogleMapsUrl(input: string): boolean {
try {
const { hostname, pathname } = new URL(input.trim())
const h = hostname.toLowerCase()
// maps.app.goo.gl, goo.gl/maps
// AMap / Gaode URLs
if (h === 'uri.amap.com') return true
if (h.endsWith('.amap.com')) return true
// Google Maps URL patterns (legacy)
if (h === 'maps.app.goo.gl') return true
if (h === 'goo.gl' && pathname.startsWith('/maps')) return true
// maps.google.* (e.g. maps.google.com, maps.google.co.uk)
// Must be maps.google.<tld> or maps.google.<sld>.<tld> — reject maps.google.evil.com
if (/^maps\.google\.[a-z]{2,3}(\.[a-z]{2})?$/.test(h)) return true
// google.*/maps (e.g. google.com/maps, www.google.co.uk/maps)
const bare = h.startsWith('www.') ? h.slice(4) : h
if (/^google\.[a-z]{2,3}(\.[a-z]{2})?$/.test(bare) && pathname.startsWith('/maps')) return true
return false