Skip to content

Commit

Permalink
Add srcdoc check when building IframeWidget
Browse files Browse the repository at this point in the history
This branch allows developers to, in addition to 'src', pass in html that contains a 'srcdoc' property. 'src' is great for passing in a URL, but if someone wants to pass in HTML, etc., inside an iframe, then we need to use 'srcdoc'. This branch is broken into three commits since there are three separate features being introduced.

Check is 'srcdoc' is sent in with the extensionContext attributes. If there is no 'srcdoc' we fallback to 'src'. This change doesn't break the current usage of the package and will not impact developers currently using it.
  • Loading branch information
swiftymf committed Aug 9, 2023
1 parent 79ec194 commit 403a644
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/flutter_html_iframe/lib/iframe_mobile.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -34,17 +36,26 @@ class IframeWidget extends StatelessWidget {
final givenHeight =
double.tryParse(extensionContext.attributes['height'] ?? "");

Uri? srcUri;

if (extensionContext.attributes['srcdoc'] != null) {
srcUri = Uri.dataFromString(
extensionContext.attributes['srcdoc'] ?? '',
mimeType: 'text/html',
encoding: Encoding.getByName('utf-8'),
);
} else {
srcUri = Uri.tryParse(extensionContext.attributes['src'] ?? "") ?? Uri();
}

return SizedBox(
width: givenWidth ?? (givenHeight ?? 150) * 2,
height: givenHeight ?? (givenWidth ?? 300) / 2,
child: CssBoxWidget(
style: extensionContext.styledElement!.style,
childIsReplaced: true,
child: WebViewWidget(
controller: controller
..loadRequest(
Uri.tryParse(extensionContext.attributes['src'] ?? "") ??
Uri()),
controller: controller..loadRequest(srcUri),
key: key,
gestureRecognizers: {Factory(() => VerticalDragGestureRecognizer())},
),
Expand Down

0 comments on commit 403a644

Please sign in to comment.