Skip to content

Commit d876565

Browse files
authored
[web] Use 'Uri' instead of 'dart:html' to extract pathname (flutter#127983)
- Use `Uri.parse()` to extract pathname. - Remove unused code from `utils.dart`. - Add test for URL encoding. (need to wait for flutter#126851 to make it into Google3)
1 parent 9753223 commit d876565

File tree

3 files changed

+6
-17
lines changed

3 files changed

+6
-17
lines changed

packages/flutter_web_plugins/lib/src/navigation/utils.dart

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,14 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:html';
6-
7-
final AnchorElement _urlParsingNode = AnchorElement();
8-
95
/// Extracts the pathname part of a full [url].
106
///
117
/// Example: for the url `http://example.com/foo`, the extracted pathname will
128
/// be `/foo`.
139
String extractPathname(String url) {
14-
_urlParsingNode.href = url; // ignore: unsafe_html, node is never exposed to the user
15-
final String pathname = _urlParsingNode.pathname ?? '';
16-
return (pathname.isEmpty || pathname[0] == '/') ? pathname : '/$pathname';
10+
return ensureLeadingSlash(Uri.parse(url).path);
1711
}
1812

19-
// The <base> element in the document.
20-
final Element? _baseElement = document.querySelector('base');
21-
22-
/// Returns the `href` attribute of the <base> element in the document.
23-
///
24-
/// Returns null if the element isn't found.
25-
String? getBaseElementHrefFromDom() => _baseElement?.getAttribute('href');
26-
2713
/// Checks that [baseHref] is set.
2814
///
2915
/// Throws an exception otherwise.

packages/flutter_web_plugins/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ dependencies:
99
flutter:
1010
sdk: flutter
1111

12-
js: 0.6.7
13-
1412
characters: 1.3.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
1513
collection: 1.17.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
14+
js: 0.6.7 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
1615
material_color_utilities: 0.5.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
1716
meta: 1.9.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
1817
vector_math: 2.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"

packages/flutter_web_plugins/test/navigation/utils_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,9 @@ void main() {
3333
expect(extractPathname('https://example.com/foo'), '/foo');
3434
expect(extractPathname('https://example.com/foo#bar'), '/foo');
3535
expect(extractPathname('https://example.com/foo/#bar'), '/foo/');
36+
37+
// URL encoding.
38+
expect(extractPathname('/foo bar'), '/foo%20bar');
39+
expect(extractPathname('https://example.com/foo bar'), '/foo%20bar');
3640
});
3741
}

0 commit comments

Comments
 (0)