未验证 提交 b0b67ef9 编写于 作者: M Mouad Debbar 提交者: GitHub

[web] Fix window.defaultRouteName (#17580)

上级 3ddd1ef4
......@@ -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, '/');
});
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册