[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)
This commit is contained in:
Mouad Debbar
2023-06-05 23:46:02 -04:00
committed by GitHub
parent 9753223932
commit d87656559c
3 changed files with 6 additions and 17 deletions

View File

@@ -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.