From 01afd64bcc95ad0394211fd927bb045241da13b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?The=20one=20with=20the=20braid=20=28she/her=29=20=7C=20D?= =?UTF-8?q?=D1=84=D2=BF=20mit=20dem=20Zopf=20=28sie/ihr=29?= Date: Wed, 13 Oct 2021 19:48:05 +0200 Subject: [PATCH] Implemented getter to expose current url strategy for web plugins (#90708) --- AUTHORS | 1 + .../test_driver/url_strategy_integration.dart | 7 ++++++- .../lib/src/navigation/js_url_strategy.dart | 5 ++--- .../lib/src/navigation/url_strategy.dart | 21 +++++++++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index fe6502d2ae..c6f1a901e8 100644 --- a/AUTHORS +++ b/AUTHORS @@ -88,3 +88,4 @@ Casey Rogers Pradumna Saraf Kai Yu Denis Grafov +TheOneWithTheBraid diff --git a/dev/integration_tests/web_e2e_tests/test_driver/url_strategy_integration.dart b/dev/integration_tests/web_e2e_tests/test_driver/url_strategy_integration.dart index 967b3cd8f5..ef03789c86 100644 --- a/dev/integration_tests/web_e2e_tests/test_driver/url_strategy_integration.dart +++ b/dev/integration_tests/web_e2e_tests/test_driver/url_strategy_integration.dart @@ -5,6 +5,7 @@ import 'dart:async'; import 'dart:html' as html; import 'dart:ui' as ui; + import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_web_plugins/flutter_web_plugins.dart'; @@ -27,6 +28,9 @@ void main() { app.main(); await tester.pumpAndSettle(); + // checking whether the previously set strategy is properly preserved + expect(urlStrategy, strategy); + expect(strategy.getPath(), '/'); final NavigatorState navigator = app.navKey.currentState!; @@ -44,7 +48,8 @@ void main() { class TestUrlStrategy extends UrlStrategy { /// Creates a instance of [TestUrlStrategy] with an empty string as the /// path. - factory TestUrlStrategy() => TestUrlStrategy.fromEntry(const TestHistoryEntry(null, null, '')); + factory TestUrlStrategy() => + TestUrlStrategy.fromEntry(const TestHistoryEntry(null, null, '')); /// Creates an instance of [TestUrlStrategy] and populates it with a list /// that has [initialEntry] as the only item. diff --git a/packages/flutter_web_plugins/lib/src/navigation/js_url_strategy.dart b/packages/flutter_web_plugins/lib/src/navigation/js_url_strategy.dart index 616dad3102..30fba83602 100644 --- a/packages/flutter_web_plugins/lib/src/navigation/js_url_strategy.dart +++ b/packages/flutter_web_plugins/lib/src/navigation/js_url_strategy.dart @@ -23,7 +23,7 @@ typedef _JsSetUrlStrategy = void Function(JsUrlStrategy?); /// A JavaScript hook to customize the URL strategy of a Flutter app. // // Keep this in sync with the JS name in the web engine. Find it at: -// https://github.com/flutter/engine/blob/custom_location_strategy/lib/web_ui/lib/src/engine/navigation/js_url_strategy.dart +// https://github.com/flutter/engine/blob/master/lib/web_ui/lib/src/engine/navigation/js_url_strategy.dart // // TODO(mdebbar): Add integration test https://github.com/flutter/flutter/issues/66852 @JS('_flutter_web_set_location_strategy') @@ -37,8 +37,7 @@ typedef _AddPopStateListener = ui.VoidCallback Function(html.EventListener); typedef _StringToString = String Function(String); -typedef _StateOperation = void Function( - Object state, String title, String url); +typedef _StateOperation = void Function(Object state, String title, String url); typedef _HistoryMove = Future Function(int count); diff --git a/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart b/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart index 043c58e2ac..27ee4874ba 100644 --- a/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart +++ b/packages/flutter_web_plugins/lib/src/navigation/url_strategy.dart @@ -9,10 +9,30 @@ import 'dart:ui' as ui; import 'js_url_strategy.dart'; import 'utils.dart'; +/// Saves the current [UrlStrategy] to be accessed by [urlStrategy] or +/// [setUrlStrategy]. +/// +/// This is particularly required for web plugins relying on valid URL +/// encoding. +// +// Keep this in sync with the default url strategy in the web engine. +// Find it at: +// https://github.com/flutter/engine/blob/master/lib/web_ui/lib/src/engine/window.dart#L360 +// +UrlStrategy? _urlStrategy = const HashUrlStrategy(); + +/// Returns the present [UrlStrategy] for handling the browser URL. +/// +/// In case null is returned, the browser integration has been manually +/// disabled by [setUrlStrategy]. +UrlStrategy? get urlStrategy => _urlStrategy; + /// Change the strategy to use for handling browser URL. /// /// Setting this to null disables all integration with the browser history. void setUrlStrategy(UrlStrategy? strategy) { + _urlStrategy = strategy; + JsUrlStrategy? jsUrlStrategy; if (strategy != null) { jsUrlStrategy = convertToJsUrlStrategy(strategy); @@ -285,6 +305,7 @@ class BrowserPlatformLocation extends PlatformLocation { static const String _defaultSearch = ''; html.Location get _location => html.window.location; + html.History get _history => html.window.history; @override