[gnome-maps/wip/mlundblad/maps-uri: 8/11] uris: Add function to parse maps: URIs
- From: Marcus Lundblad <mlundblad src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/mlundblad/maps-uri: 8/11] uris: Add function to parse maps: URIs
- Date: Sun, 7 Nov 2021 21:30:27 +0000 (UTC)
commit 967dfef2760e7c22e7299bba896c2053ad8db583
Author: Marcus Lundblad <ml update uu se>
Date: Mon Nov 1 22:42:04 2021 +0100
uris: Add function to parse maps: URIs
Adds a function to parse a search query
from a maps: URI.
See https://www.iana.org/assignments/uri-schemes/prov/maps
src/uris.js | 22 ++++++++++++++++++++++
tests/urisTest.js | 13 +++++++++++++
2 files changed, 35 insertions(+)
---
diff --git a/src/uris.js b/src/uris.js
index 96b698f2..97ed4c42 100644
--- a/src/uris.js
+++ b/src/uris.js
@@ -22,6 +22,9 @@
const _ = imports.gettext.gettext;
const GLib = imports.gi.GLib;
+const Soup = imports.gi.Soup;
+
+const Utils = imports.utils;
// Matches URLs for OpenStreetMap (for addressing objects or coordinates)
const OSM_URL_REGEX = new RegExp(/https?:\/\/(www\.)?openstreetmap\.org./);
@@ -102,3 +105,22 @@ function parseAsObjectURL(url) {
return [];
}
+
+/**
+ * For maps: URIs, return the search query string if a valid URI
+ * otherwise null.
+ */
+function parseMapsURI(uri) {
+ let path = uri.substring('maps:'.length);
+ let [param, value] = Utils.splitAtFirst(path, '=');
+
+ if (param === 'q') {
+ try {
+ return Soup.uri_decode(value);
+ } catch (error) {
+ return null;
+ }
+ } else {
+ return null;
+ }
+}
diff --git a/tests/urisTest.js b/tests/urisTest.js
index 60848ec7..f7991ec8 100644
--- a/tests/urisTest.js
+++ b/tests/urisTest.js
@@ -19,6 +19,10 @@
* Author: Marcus Lundblad <ml update uu se>
*/
+pkg.require({ 'Gdk': '3.0',
+ 'Gtk': '3.0',
+ 'Soup': '2.4' });
+
const JsUnit = imports.jsUnit;
const URIS = imports.uris;
@@ -29,6 +33,7 @@ const OSM_COORD_URL1 =
function main() {
parseAsObjectURLTest();
parseAsCoordinateURLTest();
+ parseMapsURITest();
}
function parseAsObjectURLTest() {
@@ -59,6 +64,14 @@ function parseAsCoordinateURLTest() {
URIS.parseAsCoordinateURL('https://www.openstreetmap.org/?#map=12/59.40538/17.34894'));
}
+function parseMapsURITest() {
+ JsUnit.assertEquals('Query', URIS.parseMapsURI('maps:q=Query'));
+ JsUnit.assertEquals('Search query', URIS.parseMapsURI('maps:q=Search%20query'));
+ JsUnit.assertNull(URIS.parseMapsURI('maps:No%20query'));
+ JsUnit.assertNull(URIS.parseMapsURI('not_a_valid_uri'));
+ JsUnit.assertNull(URIS.parseMapsURI('maps:q=Foo%bar'));
+}
+
function _assertArrayEquals(arr1, arr2) {
JsUnit.assertEquals(arr1.length, arr2.length);
for (let i = 0; i < arr1.length; i++) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]