Skip to content

Commit 181123c

Browse files
Copilotdaohoangson
andcommitted
Add data-src attribute support for IFRAME elements
Co-authored-by: daohoangson <[email protected]>
1 parent d8c0fee commit 181123c

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mixin WebViewFactory on WidgetFactory {
132132
}
133133

134134
final a = meta.element.attributes;
135-
final src = urlFull(a[kAttributeIframeSrc] ?? '');
135+
final src = urlFull(a[kAttributeIframeDataSrc] ?? a[kAttributeIframeSrc] ?? '');
136136
if (src == null) {
137137
return widgets;
138138
}

packages/fwfh_webview/test/web_view_factory_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,61 @@ 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(
326+
explained,
327+
equals(
328+
'[CssSizing:$sizingConstraints,child='
329+
'[WebView:url=$src,aspectRatio=$defaultAspectRatio,js=true,gestureRecognizers={}]'
330+
']',
331+
),
332+
);
333+
});
334+
335+
testWidgets('data-src takes priority over src', (tester) async {
336+
const dataSrcUrl = 'http://data-src.com';
337+
const html = '<iframe data-src="$dataSrcUrl" src="$src"></iframe>';
338+
final explained = await explain(tester, html);
339+
expect(
340+
explained,
341+
equals(
342+
'[CssSizing:$sizingConstraints,child='
343+
'[WebView:url=$dataSrcUrl,aspectRatio=$defaultAspectRatio,js=true,gestureRecognizers={}]'
344+
']',
345+
),
346+
);
347+
});
348+
349+
testWidgets('falls back to src when data-src is empty', (tester) async {
350+
const html = '<iframe data-src="" src="$src"></iframe>';
351+
final explained = await explain(tester, html);
352+
expect(
353+
explained,
354+
equals(
355+
'[CssSizing:$sizingConstraints,child='
356+
'[WebView:url=$src,aspectRatio=$defaultAspectRatio,js=true,gestureRecognizers={}]'
357+
']',
358+
),
359+
);
360+
});
361+
362+
testWidgets('falls back to src when data-src is missing', (tester) async {
363+
const html = '<iframe src="$src"></iframe>';
364+
final explained = await explain(tester, html);
365+
expect(
366+
explained,
367+
equals(
368+
'[CssSizing:$sizingConstraints,child='
369+
'[WebView:url=$src,aspectRatio=$defaultAspectRatio,js=true,gestureRecognizers={}]'
370+
']',
371+
),
372+
);
373+
});
374+
});
375+
321376
group('errors', () {
322377
testWidgets('no src', (tester) async {
323378
const html = '<iframe></iframe>';
@@ -330,6 +385,12 @@ void main() {
330385
final explained = await explain(tester, html);
331386
expect(explained, equals('[widget0]'));
332387
});
388+
389+
testWidgets('bad data-src (cannot build full url)', (tester) async {
390+
const html = '<iframe data-src="bad"></iframe>';
391+
final explained = await explain(tester, html);
392+
expect(explained, equals('[widget0]'));
393+
});
333394
});
334395
}
335396

0 commit comments

Comments
 (0)