Unverified Commit d8765655 authored by Mouad Debbar's avatar Mouad Debbar Committed by GitHub

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

- Use `Uri.parse()` to extract pathname.
- Remove unused code from `utils.dart`.
- Add test for URL encoding.

(need to wait for https://github.com/flutter/flutter/pull/126851 to make it into Google3)
parent 97532239
...@@ -2,28 +2,14 @@ ...@@ -2,28 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:html';
final AnchorElement _urlParsingNode = AnchorElement();
/// Extracts the pathname part of a full [url]. /// Extracts the pathname part of a full [url].
/// ///
/// Example: for the url `http://example.com/foo`, the extracted pathname will /// Example: for the url `http://example.com/foo`, the extracted pathname will
/// be `/foo`. /// be `/foo`.
String extractPathname(String url) { String extractPathname(String url) {
_urlParsingNode.href = url; // ignore: unsafe_html, node is never exposed to the user return ensureLeadingSlash(Uri.parse(url).path);
final String pathname = _urlParsingNode.pathname ?? '';
return (pathname.isEmpty || pathname[0] == '/') ? pathname : '/$pathname';
} }
// 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. /// Checks that [baseHref] is set.
/// ///
/// Throws an exception otherwise. /// Throws an exception otherwise.
......
...@@ -9,10 +9,9 @@ dependencies: ...@@ -9,10 +9,9 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
js: 0.6.7
characters: 1.3.0 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade" 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" 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" 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" 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" vector_math: 2.1.4 # THIS LINE IS AUTOGENERATED - TO UPDATE USE "flutter update-packages --force-upgrade"
......
...@@ -33,5 +33,9 @@ void main() { ...@@ -33,5 +33,9 @@ void main() {
expect(extractPathname('https://example.com/foo'), '/foo'); expect(extractPathname('https://example.com/foo'), '/foo');
expect(extractPathname('https://example.com/foo#bar'), '/foo'); expect(extractPathname('https://example.com/foo#bar'), '/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');
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment