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 5 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
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
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', () {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

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;
}
}
23 changes: 17 additions & 6 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,14 +30,26 @@ class SharePlusPlugin extends SharePlatform {
await _navigator.share({'title': subject, 'text': text});
} catch (e) {
//Navigator is not available or the webPage is not served on https
final uri = Uri.encodeFull('mailto:?subject=$subject&body=$text');
await launch(uri);
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
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
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', () {
final mock = MockUrlLauncherPlatform();
UrlLauncherPlatform.instance = mock;

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;
}
}