Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
[url_launcher] Fixed missing # in links href when opening in new tab …
Browse files Browse the repository at this point in the history
…on the web (#4221)
  • Loading branch information
TheOneWithTheBraid authored Feb 25, 2022
1 parent 045e651 commit 06922c0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
Daniel Roek <[email protected]>
TheOneWithTheBraid <[email protected]>
1 change: 1 addition & 0 deletions packages/url_launcher/url_launcher_web/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ Aleksandr Yurkovskiy <[email protected]>
Anton Borries <[email protected]>
Alex Li <[email protected]>
Rahul Raj <[email protected]>
TheOneWithTheBraid <[email protected]>
6 changes: 5 additions & 1 deletion packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.9

- Fixes invalid routes when opening a `Link` in a new tab

## 2.0.8

* Updates the minimum Flutter version to 2.10, which is required by the change
Expand All @@ -17,7 +21,7 @@

## 2.0.4

* Add `implements` to pubspec.
- Add `implements` to pubspec.

## 2.0.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:js_util';

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:integration_test/integration_test.dart';
import 'package:url_launcher_platform_interface/link.dart';
import 'package:url_launcher_web/src/link.dart';
Expand Down Expand Up @@ -51,6 +52,25 @@ void main() {
// Check that the same anchor has been updated.
expect(anchor.getAttribute('href'), uri2.toString());
expect(anchor.getAttribute('target'), '_self');

final Uri uri3 = Uri.parse('/foobar');
await tester.pumpWidget(Directionality(
textDirection: TextDirection.ltr,
child: WebLinkDelegate(TestLinkInfo(
uri: uri3,
target: LinkTarget.self,
builder: (BuildContext context, FollowLink? followLink) {
return Container(width: 100, height: 100);
},
)),
));
await tester.pumpAndSettle();

// Check that internal route properly prepares using the default
// [UrlStrategy]
expect(anchor.getAttribute('href'),
urlStrategy?.prepareExternalUrl(uri3.toString()));
expect(anchor.getAttribute('target'), '_self');
});

testWidgets('sizes itself correctly', (WidgetTester tester) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Mocks generated by Mockito 5.0.16 from annotations
// Mocks generated by Mockito 5.0.17 from annotations
// in regular_integration_tests/integration_test/url_launcher_web_test.dart.
// Do not manually edit this file.

Expand Down Expand Up @@ -643,8 +643,6 @@ class MockWindow extends _i1.Mock implements _i2.Window {
bool dispatchEvent(_i2.Event? event) =>
(super.noSuchMethod(Invocation.method(#dispatchEvent, [event]),
returnValue: false) as bool);
@override
String toString() => super.toString();
}

/// A class which mocks [Navigator].
Expand Down Expand Up @@ -749,6 +747,4 @@ class MockNavigator extends _i1.Mock implements _i2.Navigator {
_i3.Future<dynamic> share([Map<dynamic, dynamic>? data]) =>
(super.noSuchMethod(Invocation.method(#share, [data]),
returnValue: Future<dynamic>.value()) as _i3.Future<dynamic>);
@override
String toString() => super.toString();
}
11 changes: 9 additions & 2 deletions packages/url_launcher/url_launcher_web/lib/src/link.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';

import 'package:flutter_web_plugins/flutter_web_plugins.dart' show urlStrategy;
import 'package:url_launcher_platform_interface/link.dart';

/// The unique identifier for the view type to be used for link platform views.
Expand Down Expand Up @@ -163,6 +163,7 @@ class LinkViewController extends PlatformViewController {
final BuildContext context;

late html.Element _element;

bool get _isInitialized => _element != null;

Future<void> _initialize() async {
Expand Down Expand Up @@ -221,7 +222,13 @@ class LinkViewController extends PlatformViewController {
if (uri == null) {
_element.removeAttribute('href');
} else {
_element.setAttribute('href', uri.toString());
String href = uri.toString();
// in case an internal uri is given, the url mus be properly encoded
// using the currently used [UrlStrategy]
if (!uri.hasScheme) {
href = urlStrategy?.prepareExternalUrl(href) ?? href;
}
_element.setAttribute('href', href);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_web
description: Web platform implementation of url_launcher
repository: https://github.com/flutter/plugins/tree/main/packages/url_launcher/url_launcher_web
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+url_launcher%22
version: 2.0.8
version: 2.0.9

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 06922c0

Please sign in to comment.