mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
[web] Fix window.defaultRouteName (flutter/engine#17580)
This commit is contained in:
parent
a03f69c53a
commit
f353d919aa
@ -9,27 +9,6 @@ part of engine;
|
||||
|
||||
// Some parts of this file were inspired/copied from the AngularDart router.
|
||||
|
||||
/// Ensures that `str` is prefixed with `leading`.
|
||||
///
|
||||
/// If `str` is already prefixed, it'll be returned unchanged. If it's not,
|
||||
/// this function will prefix it.
|
||||
///
|
||||
/// The `applyWhenEmpty` flag controls whether this function should prefix `str`
|
||||
/// or not when it's an empty string.
|
||||
///
|
||||
/// ```dart
|
||||
/// ensureLeading('/path', '/'); // "/path"
|
||||
/// ensureLeading('path', '/'); // "/path"
|
||||
/// ensureLeading('', '/'); // "/"
|
||||
/// ensureLeading('', '/', applyWhenEmpty: false); // ""
|
||||
/// ```
|
||||
String ensureLeading(String str, String leading, {bool applyWhenEmpty = true}) {
|
||||
if (str.isEmpty && !applyWhenEmpty) {
|
||||
return str;
|
||||
}
|
||||
return str.startsWith(leading) ? str : '$leading$str';
|
||||
}
|
||||
|
||||
/// [LocationStrategy] is responsible for representing and reading route state
|
||||
/// from the browser's URL.
|
||||
///
|
||||
@ -93,12 +72,11 @@ class HashLocationStrategy extends LocationStrategy {
|
||||
// the hash value is always prefixed with a `#`
|
||||
// and if it is empty then it will stay empty
|
||||
String path = _platformLocation.hash ?? '';
|
||||
assert(path.isEmpty || path.startsWith('#'));
|
||||
// Dart will complain if a call to substring is
|
||||
// executed with a position value that exceeds the
|
||||
// length of string.
|
||||
path = path.isEmpty ? path : path.substring(1);
|
||||
// The path, by convention, should always contain a leading '/'.
|
||||
return ensureLeading(path, '/');
|
||||
return path.isEmpty ? path : path.substring(1);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@ -37,7 +37,7 @@ class TestLocationStrategy extends LocationStrategy {
|
||||
history = <TestHistoryEntry>[initialEntry];
|
||||
|
||||
@override
|
||||
String get path => ensureLeading(currentEntry.url, '/');
|
||||
String get path => currentEntry.url;
|
||||
|
||||
int _currentEntryIndex;
|
||||
int get currentEntryIndex => _currentEntryIndex;
|
||||
|
||||
@ -441,6 +441,10 @@ class EngineWindow extends ui.Window {
|
||||
_replyToPlatformMessage(callback, codec.encodeSuccessEnvelope(true));
|
||||
break;
|
||||
}
|
||||
// As soon as Flutter starts taking control of the app navigation, we
|
||||
// should reset [_defaultRouteName] to "/" so it doesn't have any
|
||||
// further effect after this point.
|
||||
_defaultRouteName = '/';
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.6
|
||||
@TestOn('vm && linux')
|
||||
@TestOn('!safari')
|
||||
// TODO(nurhan): https://github.com/flutter/flutter/issues/51169
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
@ -3,9 +3,15 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
// @dart = 2.6
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:ui/src/engine.dart';
|
||||
|
||||
const MethodCodec codec = JSONMethodCodec();
|
||||
|
||||
void emptyCallback(ByteData date) {}
|
||||
|
||||
TestLocationStrategy _strategy;
|
||||
TestLocationStrategy get strategy => _strategy;
|
||||
set strategy(TestLocationStrategy newStrategy) {
|
||||
@ -17,7 +23,27 @@ void main() {
|
||||
strategy = TestLocationStrategy.fromEntry(TestHistoryEntry('initial state', null, '/initial'));
|
||||
expect(window.defaultRouteName, '/initial');
|
||||
|
||||
// Changing the URL in the address bar later shouldn't affect [window.defaultRouteName].
|
||||
strategy.replaceState(null, null, '/newpath');
|
||||
expect(window.defaultRouteName, '/initial');
|
||||
});
|
||||
|
||||
test('window.defaultRouteName should reset after navigation platform message', () {
|
||||
strategy = TestLocationStrategy.fromEntry(TestHistoryEntry('initial state', null, '/initial'));
|
||||
// Reading it multiple times should return the same value.
|
||||
expect(window.defaultRouteName, '/initial');
|
||||
expect(window.defaultRouteName, '/initial');
|
||||
|
||||
window.sendPlatformMessage(
|
||||
'flutter/navigation',
|
||||
JSONMethodCodec().encodeMethodCall(MethodCall(
|
||||
'routePushed',
|
||||
<String, dynamic>{'previousRouteName': '/foo', 'routeName': '/bar'},
|
||||
)),
|
||||
emptyCallback,
|
||||
);
|
||||
// After a navigation platform message, [window.defaultRouteName] should
|
||||
// reset to "/".
|
||||
expect(window.defaultRouteName, '/');
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user