Skip to content

Commit 0d64779

Browse files
authored
Add support for data-src attribute in IFRAME elements with src priority (#1497)
1 parent 71cca5f commit 0d64779

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

packages/fwfh_webview/lib/src/internal.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const kTagIframe = 'iframe';
2+
const kAttributeIframeDataSrc = 'data-src';
23
const kAttributeIframeHeight = 'height';
34
const kAttributeIframeSandbox = 'sandbox';
45
const kAttributeIframeSandboxAllowScripts = 'allow-scripts';

packages/fwfh_webview/lib/src/web_view_factory.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ mixin WebViewFactory on WidgetFactory {
132132
}
133133

134134
final a = meta.element.attributes;
135-
final src = urlFull(a[kAttributeIframeSrc] ?? '');
135+
final dataSrc = a[kAttributeIframeDataSrc];
136+
final srcAttr = a[kAttributeIframeSrc];
137+
final src = urlFull(
138+
(srcAttr?.isNotEmpty == true) ? srcAttr! : (dataSrc ?? '')
139+
);
136140
if (src == null) {
137141
return widgets;
138142
}

packages/fwfh_webview/test/web_view_factory_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ void main() {
318318
});
319319
});
320320

321+
group('data-src attribute', () {
322+
testWidgets('renders with data-src', (tester) async {
323+
const html = '<iframe data-src="$src"></iframe>';
324+
final explained = await explain(tester, html);
325+
expect(explained, contains('url=$src,'));
326+
});
327+
328+
testWidgets('src takes priority over data-src', (tester) async {
329+
const html = '<iframe data-src="$src/1" src="$src/2"></iframe>';
330+
final explained = await explain(tester, html);
331+
expect(explained, contains('url=$src/2,'));
332+
});
333+
334+
testWidgets('falls back to data-src when src is empty', (tester) async {
335+
const html = '<iframe data-src="$src" src=""></iframe>';
336+
final explained = await explain(tester, html);
337+
expect(explained, contains('url=$src,'));
338+
});
339+
340+
testWidgets('uses src when data-src is missing', (tester) async {
341+
const html = '<iframe src="$src"></iframe>';
342+
final explained = await explain(tester, html);
343+
expect(explained, contains('url=$src,'));
344+
});
345+
});
346+
321347
group('errors', () {
322348
testWidgets('no src', (tester) async {
323349
const html = '<iframe></iframe>';
@@ -330,6 +356,12 @@ void main() {
330356
final explained = await explain(tester, html);
331357
expect(explained, equals('[widget0]'));
332358
});
359+
360+
testWidgets('bad data-src (cannot build full url)', (tester) async {
361+
const html = '<iframe data-src="bad"></iframe>';
362+
final explained = await explain(tester, html);
363+
expect(explained, equals('[widget0]'));
364+
});
333365
});
334366
}
335367

0 commit comments

Comments
 (0)