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

Commit de02c2b

Browse files
Merge branch 'main' into android-handle-quick-action-without-restart
2 parents 791fcfb + 3a2cd24 commit de02c2b

File tree

4 files changed

+77
-17
lines changed

4 files changed

+77
-17
lines changed

packages/webview_flutter/webview_flutter_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.0+4
2+
3+
* Fixes incorrect escaping of some characters when setting the HTML to the iframe element.
4+
15
## 0.1.0+3
26

37
* Minor fixes for new analysis options.

packages/webview_flutter/webview_flutter_web/lib/webview_flutter_web.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:convert';
67
import 'dart:html';
78
import 'package:flutter/foundation.dart';
89
import 'package:flutter/gestures.dart';
@@ -183,7 +184,11 @@ class WebWebViewPlatformController implements WebViewPlatformController {
183184
String? baseUrl,
184185
}) async {
185186
// ignore: unsafe_html
186-
_element.src = 'data:text/html,${Uri.encodeFull(html)}';
187+
_element.src = Uri.dataFromString(
188+
html,
189+
mimeType: 'text/html',
190+
encoding: utf8,
191+
).toString();
187192
}
188193

189194
@override
@@ -199,8 +204,11 @@ class WebWebViewPlatformController implements WebViewPlatformController {
199204
final String contentType =
200205
httpReq.getResponseHeader('content-type') ?? 'text/html';
201206
// ignore: unsafe_html
202-
_element.src =
203-
'data:$contentType,${Uri.encodeFull(httpReq.responseText ?? '')}';
207+
_element.src = Uri.dataFromString(
208+
httpReq.responseText ?? '',
209+
mimeType: contentType,
210+
encoding: utf8,
211+
).toString();
204212
}
205213

206214
@override

packages/webview_flutter/webview_flutter_web/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: webview_flutter_web
22
description: A Flutter plugin that provides a WebView widget on web.
33
repository: https://github.com/flutter/plugins/tree/main/packages/webview_flutter/webview_flutter_web
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
5-
version: 0.1.0+3
5+
version: 0.1.0+4
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

packages/webview_flutter/webview_flutter_web/test/webview_flutter_web_test.dart

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,33 @@ void main() {
5454
verify(mockElement.src = 'test url');
5555
});
5656

57-
test('loadHtmlString loads html into iframe', () {
58-
// Setup
59-
final MockIFrameElement mockElement = MockIFrameElement();
60-
final WebWebViewPlatformController controller =
61-
WebWebViewPlatformController(
62-
mockElement,
63-
);
64-
// Run
65-
controller.loadHtmlString('test html');
66-
// Verify
67-
verify(mockElement.src = 'data:text/html,${Uri.encodeFull('test html')}');
57+
group('loadHtmlString', () {
58+
test('loadHtmlString loads html into iframe', () {
59+
// Setup
60+
final MockIFrameElement mockElement = MockIFrameElement();
61+
final WebWebViewPlatformController controller =
62+
WebWebViewPlatformController(
63+
mockElement,
64+
);
65+
// Run
66+
controller.loadHtmlString('test html');
67+
// Verify
68+
verify(mockElement.src =
69+
'data:text/html;charset=utf-8,${Uri.encodeFull('test html')}');
70+
});
71+
72+
test('loadHtmlString escapes "#" correctly', () {
73+
// Setup
74+
final MockIFrameElement mockElement = MockIFrameElement();
75+
final WebWebViewPlatformController controller =
76+
WebWebViewPlatformController(
77+
mockElement,
78+
);
79+
// Run
80+
controller.loadHtmlString('#');
81+
// Verify
82+
verify(mockElement.src = argThat(contains('%23')));
83+
});
6884
});
6985

7086
group('loadRequest', () {
@@ -122,8 +138,40 @@ void main() {
122138
requestHeaders: <String, String>{'Foo': 'Bar'},
123139
sendData: Uint8List.fromList('test body'.codeUnits),
124140
));
125-
verify(
126-
mockElement.src = 'data:text/plain,${Uri.encodeFull('test data')}');
141+
verify(mockElement.src =
142+
'data:;charset=utf-8,${Uri.encodeFull('test data')}');
143+
});
144+
145+
test('loadRequest escapes "#" correctly', () async {
146+
// Setup
147+
final MockIFrameElement mockElement = MockIFrameElement();
148+
final WebWebViewPlatformController controller =
149+
WebWebViewPlatformController(
150+
mockElement,
151+
);
152+
final MockHttpRequest mockHttpRequest = MockHttpRequest();
153+
when(mockHttpRequest.getResponseHeader('content-type'))
154+
.thenReturn('text/html');
155+
when(mockHttpRequest.responseText).thenReturn('#');
156+
final MockHttpRequestFactory mockHttpRequestFactory =
157+
MockHttpRequestFactory();
158+
when(mockHttpRequestFactory.request(
159+
any,
160+
method: anyNamed('method'),
161+
requestHeaders: anyNamed('requestHeaders'),
162+
sendData: anyNamed('sendData'),
163+
)).thenAnswer((_) => Future<HttpRequest>.value(mockHttpRequest));
164+
controller.httpRequestFactory = mockHttpRequestFactory;
165+
// Run
166+
await controller.loadRequest(
167+
WebViewRequest(
168+
uri: Uri.parse('https://flutter.dev'),
169+
method: WebViewRequestMethod.post,
170+
body: Uint8List.fromList('test body'.codeUnits),
171+
headers: <String, String>{'Foo': 'Bar'}),
172+
);
173+
// Verify
174+
verify(mockElement.src = argThat(contains('%23')));
127175
});
128176
});
129177
});

0 commit comments

Comments
 (0)