Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[web] Use 'Uri' instead of 'dart:html' to extract pathname #127983

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions packages/flutter_web_plugins/lib/src/navigation/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:html';

final AnchorElement _urlParsingNode = AnchorElement();

/// Extracts the pathname part of a full [url].
///
/// Example: for the url `http://example.com/foo`, the extracted pathname will
/// be `/foo`.
String extractPathname(String url) {
_urlParsingNode.href = url; // ignore: unsafe_html, node is never exposed to the user
final String pathname = _urlParsingNode.pathname ?? '';
return (pathname.isEmpty || pathname[0] == '/') ? pathname : '/$pathname';
return ensureLeadingSlash(Uri.parse(url).path);
}

// The <base> element in the document.
final Element? _baseElement = document.querySelector('base');

/// Returns the `href` attribute of the <base> element in the document.
///
/// Returns null if the element isn't found.
String? getBaseElementHrefFromDom() => _baseElement?.getAttribute('href');

/// Checks that [baseHref] is set.
///
/// Throws an exception otherwise.
Expand Down
3 changes: 1 addition & 2 deletions packages/flutter_web_plugins/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ dependencies:
flutter:
sdk: flutter

js: 0.6.7

characters: 1.3.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
collection: 1.17.2 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
js: 0.6.7 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
material_color_utilities: 0.5.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
meta: 1.9.1 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
vector_math: 2.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_web_plugins/test/navigation/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ void main() {
expect(extractPathname('https://example.com/foo'), '/foo');
expect(extractPathname('https://example.com/foo#bar'), '/foo');
expect(extractPathname('https://example.com/foo/#bar'), '/foo/');

// URL encoding.
expect(extractPathname('/foo bar'), '/foo%20bar');
expect(extractPathname('https://example.com/foo bar'), '/foo%20bar');
});
}