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

Commit 7c49361

Browse files
authored
[url_launcher, url_launcher_web]Added webOnlyWindowName parameter to launch() (#2979)
1 parent 161aed2 commit 7c49361

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

packages/url_launcher/url_launcher/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.5.1
2+
3+
* Added webOnlyWindowName parameter to launch()
4+
15
## 5.5.0
26

37
* Support Linux by default.

packages/url_launcher/url_launcher/lib/url_launcher.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ import 'package:url_launcher_platform_interface/url_launcher_platform_interface.
4444
/// [enableDomStorage] is an Android only setting. If true, WebView enable
4545
/// DOM storage.
4646
/// [headers] is an Android only setting that adds headers to the WebView.
47+
/// [webOnlyWindowName] is an Web only setting . _blank opens the new url in new tab ,
48+
/// _self opens the new url in current tab.
49+
/// Default behaviour is to open the url in new tab.
4750
///
4851
/// Note that if any of the above are set to true but the URL is not a web URL,
4952
/// this will throw a [PlatformException].
@@ -63,6 +66,7 @@ Future<bool> launch(
6366
bool universalLinksOnly,
6467
Map<String, String> headers,
6568
Brightness statusBarBrightness,
69+
String webOnlyWindowName,
6670
}) async {
6771
assert(urlString != null);
6872
final Uri url = Uri.parse(urlString.trimLeft());
@@ -93,6 +97,7 @@ Future<bool> launch(
9397
enableDomStorage: enableDomStorage ?? false,
9498
universalLinksOnly: universalLinksOnly ?? false,
9599
headers: headers ?? <String, String>{},
100+
webOnlyWindowName: webOnlyWindowName,
96101
);
97102
assert(previousAutomaticSystemUiAdjustment != null);
98103
if (statusBarBrightness != null) {

packages/url_launcher/url_launcher/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: url_launcher
22
description: Flutter plugin for launching a URL on Android and iOS. Supports
33
web, phone, SMS, and email schemes.
44
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
5-
version: 5.5.0
5+
version: 5.5.1
66

77
flutter:
88
plugin:

packages/url_launcher/url_launcher_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 0.1.3
2+
3+
- Added webOnlyWindowName parameter to launch()
4+
15
# 0.1.2+1
26

37
- Update docs

packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
3939
bool _isSafariTargetTopScheme(String url) =>
4040
_safariTargetTopSchemes.contains(_getUrlScheme(url));
4141

42-
/// Opens the given [url] in a new window.
42+
/// Opens the given [url] in the specified [webOnlyWindowName].
4343
///
4444
/// Returns the newly created window.
4545
@visibleForTesting
46-
html.WindowBase openNewWindow(String url) {
46+
html.WindowBase openNewWindow(String url, {String webOnlyWindowName}) {
4747
// We need to open mailto, tel and sms urls on the _top window context on safari browsers.
4848
// See https://github.com/flutter/flutter/issues/51461 for reference.
49-
final target =
50-
browser.isSafari && _isSafariTargetTopScheme(url) ? '_top' : '';
49+
final target = webOnlyWindowName ??
50+
((browser.isSafari && _isSafariTargetTopScheme(url)) ? '_top' : '');
5151
return _window.open(url, target);
5252
}
5353

@@ -65,7 +65,9 @@ class UrlLauncherPlugin extends UrlLauncherPlatform {
6565
@required bool enableDomStorage,
6666
@required bool universalLinksOnly,
6767
@required Map<String, String> headers,
68+
String webOnlyWindowName,
6869
}) {
69-
return Future<bool>.value(openNewWindow(url) != null);
70+
return Future<bool>.value(
71+
openNewWindow(url, webOnlyWindowName: webOnlyWindowName) != null);
7072
}
7173
}

packages/url_launcher/url_launcher_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/u
44
# 0.1.y+z is compatible with 1.0.0, if you land a breaking change bump
55
# the version to 2.0.0.
66
# See more details: https://github.com/flutter/flutter/wiki/Package-migration-to-1.0.0
7-
version: 0.1.2+1
7+
version: 0.1.3
88

99
flutter:
1010
plugin:

packages/url_launcher/url_launcher_web/test/url_launcher_web_test.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ void main() {
145145

146146
verify(mockWindow.open('sms:+19725551212?body=hello%20there', ''));
147147
});
148+
test('setting oOnlyLinkTarget as _self opens the url in the same tab',
149+
() {
150+
plugin.openNewWindow("https://www.google.com",
151+
webOnlyWindowName: "_self");
152+
verify(mockWindow.open('https://www.google.com', '_self'));
153+
});
154+
155+
test('setting webOnlyLinkTarget as _blank opens the url in a new tab',
156+
() {
157+
plugin.openNewWindow("https://www.google.com",
158+
webOnlyWindowName: "_blank");
159+
verify(mockWindow.open('https://www.google.com', '_blank'));
160+
});
148161

149162
group('Safari', () {
150163
setUp(() {
@@ -181,6 +194,13 @@ void main() {
181194
verify(
182195
mockWindow.open('sms:+19725551212?body=hello%20there', '_top'));
183196
});
197+
test(
198+
'mailto urls should use _blank if webOnlyWindowName is set as _blank',
199+
() {
200+
plugin.openNewWindow("mailto:name@mydomain.com",
201+
webOnlyWindowName: "_blank");
202+
verify(mockWindow.open("mailto:name@mydomain.com", "_blank"));
203+
});
184204
});
185205
});
186206
});

0 commit comments

Comments
 (0)