Skip to content

fix(share_plus): Fix url encoding for web, Linux and Windows #236

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

Merged
merged 10 commits into from
May 14, 2021
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
4 changes: 4 additions & 0 deletions packages/share_plus_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

- Fixes: share URL is constructed incorrectly #235

## 2.0.1

- Improve documentation
Expand Down
19 changes: 16 additions & 3 deletions packages/share_plus_linux/lib/share_plus_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ class ShareLinux extends SharePlatform {
String text, {
String? subject,
Rect? sharePositionOrigin,
}) {
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
return launch(uri);
}) async {
final queryParameters = {
if (subject != null) 'subject': subject,
'body': text,
};

final uri = Uri(
scheme: 'mailto',
queryParameters: queryParameters,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice!!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is actually wrong due to a Dart bug (which is why I didn't suggest this format when I filed the issue). See dart-lang/sdk#43838, especially dart-lang/sdk#43838 (comment).

This will fix the issue I filed, but may cause spaces to be converted to + depending on the handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's unfortunate 😅 Are there more gotchas which I should be aware of before attempting another fix?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None that I know of, no. I would suggest including spaces in the values in your unit tests on the next round, so that tests will prevent someone accidentally falling into this trap again in the future when maintaining these plugins (although hopefully the issue will be fixed in Dart itself at some point).

);

if (await canLaunch(uri.toString())) {
await launch(uri.toString());
} else {
throw Exception('Unable to share on linux');
}
}

/// Share files.
Expand Down
3 changes: 2 additions & 1 deletion packages/share_plus_linux/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: share_plus_linux
description: Linux implementation of the share_plus plugin
version: 2.0.1
version: 2.0.2
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

Expand All @@ -20,3 +20,4 @@ dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.10.0
url_launcher_platform_interface: ^2.0.2
51 changes: 51 additions & 0 deletions packages/share_plus_linux/test/share_plus_linux_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:share_plus_linux/share_plus_linux.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_platform_interface/link.dart';

void main() {
test('url encoding is correct', () async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareLinux().share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
});

test('throws when url_launcher can\'t launch uri', () async {
final mock = MockUrlLauncherPlatform();
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(() async => await ShareLinux().share('foo bar'), throwsException);
});
}

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
String? url;
bool canLaunchMockValue = true;

@override
LinkDelegate? get linkDelegate => throw UnimplementedError();

@override
Future<bool> canLaunch(String url) async {
return canLaunchMockValue;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
this.url = url;
return true;
}
}
4 changes: 4 additions & 0 deletions packages/share_plus_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.3

- Fix #235: share URL is constructed incorrectly

## 2.0.2

- Fix #201: avoid call to "tomail:" when canceling "Share"
Expand Down
25 changes: 17 additions & 8 deletions packages/share_plus_web/lib/share_plus_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class SharePlusPlugin extends SharePlatform {
SharePlusPlugin({@visibleForTesting html.Navigator? debugNavigator})
: _navigator = debugNavigator ?? html.window.navigator;

@override

/// Share text
@override
Future<void> share(
String text, {
String? subject,
Expand All @@ -31,16 +30,26 @@ class SharePlusPlugin extends SharePlatform {
await _navigator.share({'title': subject, 'text': text});
} on NoSuchMethodError catch (_) {
//Navigator is not available or the webPage is not served on https
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
await launch(uri);
} catch (_) {
//Navigator share cancel
final queryParameters = {
if (subject != null) 'subject': subject,
'body': text,
};

final uri = Uri(
scheme: 'mailto',
queryParameters: queryParameters,
);

if (await canLaunch(uri.toString())) {
await launch(uri.toString());
} else {
throw Exception('Unable to share on web');
}
}
}

@override

/// Share files
@override
Future<void> shareFiles(
List<String> paths, {
List<String>? mimeTypes,
Expand Down
2 changes: 1 addition & 1 deletion packages/share_plus_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: share_plus_web
description: Web platform implementation of share_plus
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
version: 2.0.2
version: 2.0.3

flutter:
plugin:
Expand Down
4 changes: 4 additions & 0 deletions packages/share_plus_windows/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.2

- Fixes: share URL is constructed incorrectly #235

## 2.0.1

- Improve documentation
Expand Down
19 changes: 16 additions & 3 deletions packages/share_plus_windows/lib/share_plus_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ class ShareWindows extends SharePlatform {
String text, {
String? subject,
Rect? sharePositionOrigin,
}) {
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
return launch(uri);
}) async {
final queryParameters = {
if (subject != null) 'subject': subject,
'body': text,
};

final uri = Uri(
scheme: 'mailto',
queryParameters: queryParameters,
);

if (await canLaunch(uri.toString())) {
await launch(uri.toString());
} else {
throw Exception('Unable to share on windows');
}
}

/// Share files.
Expand Down
3 changes: 2 additions & 1 deletion packages/share_plus_windows/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: share_plus_windows
description: Windows implementation of the share_plus plugin
version: 2.0.1
version: 2.0.2
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

Expand All @@ -19,3 +19,4 @@ dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.10.0
url_launcher_platform_interface: ^2.0.2
51 changes: 51 additions & 0 deletions packages/share_plus_windows/test/share_plus_windows_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:share_plus_windows/share_plus_windows.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';
import 'package:url_launcher_platform_interface/link.dart';

void main() {
test('url encoding is correct', () async {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

await ShareWindows().share('foo&bar', subject: 'bar&foo');

expect(mock.url, 'mailto:?subject=bar%26foo&body=foo%26bar');
});

test('throws when url_launcher can\'t launch uri', () async {
final mock = MockUrlLauncherPlatform();
mock.canLaunchMockValue = false;
UrlLauncherPlatform.instance = mock;

expect(() async => await ShareWindows().share('foo bar'), throwsException);
});
}

class MockUrlLauncherPlatform extends UrlLauncherPlatform {
String? url;
bool canLaunchMockValue = true;

@override
LinkDelegate? get linkDelegate => throw UnimplementedError();

@override
Future<bool> canLaunch(String url) async {
return canLaunchMockValue;
}

@override
Future<bool> launch(
String url, {
required bool useSafariVC,
required bool useWebView,
required bool enableJavaScript,
required bool enableDomStorage,
required bool universalLinksOnly,
required Map<String, String> headers,
String? webOnlyWindowName,
}) async {
this.url = url;
return true;
}
}