Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 436a2ac

Browse files
committed
feat(iframe-plugin): support headers via provideEmbeddedHeaders prop
1 parent 5a5d8d2 commit 436a2ac

9 files changed

+98
-17
lines changed

example/CustomExample.js

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ export default function CustomExample({
183183
return (
184184
<RenderHTML
185185
key={`custom-${instance}`}
186+
provideEmbeddedHeaders={(uri, tagName) => null}
186187
source={{ html: table1 }}
187188
contentWidth={availableWidth}
188189
renderersProps={{

packages/iframe-plugin/README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,41 @@ equivalent to `resizeMode: 'contain'` for images. See example below with
127127

128128
![](/blob/master/images/scalesPageToFit.jpg)
129129

130+
## Providing headers
131+
132+
```jsx
133+
import IframeRenderer, { iframeModel } from '@native-html/iframe-plugin';
134+
import RenderHTML from 'react-native-render-html';
135+
import WebView from 'react-native-webview';
136+
137+
const renderers = {
138+
iframe: IframeRenderer
139+
}
140+
141+
const customHTMLElementModels = {
142+
iframe: iframeModel
143+
}
144+
145+
function provideEmbeddedHeaders(uri, tagName) {
146+
if (tagName === "iframe" && uri.startsWith("https://protected-domain.com")) {
147+
// Pass an authorization header to all iframes in protected-domain.com
148+
return {
149+
Authorization: "Bearer XXXXXXXX";
150+
}
151+
}
152+
}
153+
154+
// ...
155+
156+
<RenderHTML renderers={renderers}
157+
WebView={WebView}
158+
source={{ html: '<iframe width="400" height="200" src="https://protected-domain.com/user/cart?embedded"></iframe>' }}
159+
customHTMLElementModels={customHTMLElementModels}
160+
provideEmbeddedHeaders={provideEmbeddedHeaders}
161+
/>
162+
163+
```
164+
130165
## Customizing the renderer
131166

132167
You can customize the renderer logic thanks to `useHtmlIframeProps` hook, `iframeModel` and `HTMLIframe` exports:
@@ -137,7 +172,7 @@ import {useHtmlIframeProps, HTMLIframe, iframeModel} from '@native-html/iframe-p
137172
const IframeRenderer = function IframeRenderer(props) {
138173
const iframeProps = useHtmlIframeProps(props);
139174
// Do customize the props here; wrap with your own container...
140-
return iframeProps ? <HTMLIframe {...iframeProps} /> : null;
175+
return <HTMLIframe {...iframeProps} />;
141176
};
142177

143178
const renderers = {

packages/iframe-plugin/docs/iframe-plugin.htmliframe.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ A component to render iframes in react-native-render-html.
99
<b>Signature:</b>
1010

1111
```typescript
12-
export default function HTMLIframe({ WebView, webViewProps: userWebViewProps, source, style, onLinkPress, scaleFactor, injectedCSSStyles, removeBodySpacing, scalesPageToFit }: HTMLIframeProps): React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>;
12+
export default function HTMLIframe({ WebView, webViewProps: userWebViewProps, source, style, onLinkPress, scaleFactor, injectedCSSStyles, removeBodySpacing, scalesPageToFit }: HTMLIframeProps): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
1313
```
1414

1515
## Parameters
@@ -20,5 +20,5 @@ export default function HTMLIframe({ WebView, webViewProps: userWebViewProps, so
2020

2121
<b>Returns:</b>
2222

23-
React.ReactElement&lt;any, string \| ((props: any) =&gt; React.ReactElement&lt;any, string \| any \| (new (props: any) =&gt; React.Component&lt;any, any, any&gt;)&gt; \| null) \| (new (props: any) =&gt; React.Component&lt;any, any, any&gt;)&gt;
23+
React.ReactElement&lt;any, string \| React.JSXElementConstructor&lt;any&gt;&gt;
2424

packages/iframe-plugin/docs/iframe-plugin.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
| Function | Description |
1010
| --- | --- |
1111
| [HTMLIframe({ WebView, webViewProps: userWebViewProps, source, style, onLinkPress, scaleFactor, injectedCSSStyles, removeBodySpacing, scalesPageToFit })](./iframe-plugin.htmliframe.md) | A component to render iframes in react-native-render-html. |
12-
| [useHtmlIframeProps({ key, style, tnode }, iframeConfig)](./iframe-plugin.usehtmliframeprops.md) | Extract props for the HTMLIframe component from renderer function arguments. This function is especially usefull for custom iframe renderers. |
12+
| [useHtmlIframeProps({ style, tnode }, iframeConfig)](./iframe-plugin.usehtmliframeprops.md) | Extract props for the HTMLIframe component from renderer function arguments. This function is especially usefull for custom iframe renderers. |
1313

1414
## Interfaces
1515

packages/iframe-plugin/docs/iframe-plugin.usehtmliframeprops.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ Extract props for the HTMLIframe component from renderer function arguments. Thi
99
<b>Signature:</b>
1010

1111
```typescript
12-
export default function useHtmlIframeProps({ key, style, tnode }: CustomRendererProps<TBlock>, iframeConfig?: IframeConfig): (HTMLIframeProps & {
13-
key?: any;
14-
}) | null;
12+
export default function useHtmlIframeProps({ style, tnode }: CustomRendererProps<TBlock>, iframeConfig?: IframeConfig): HTMLIframeProps | null;
1513
```
1614

1715
## Parameters
1816

1917
| Parameter | Type | Description |
2018
| --- | --- | --- |
21-
| { key, style, tnode } | CustomRendererProps&lt;TBlock&gt; | |
19+
| { style, tnode } | CustomRendererProps&lt;TBlock&gt; | |
2220
| iframeConfig | [IframeConfig](./iframe-plugin.iframeconfig.md) | Override config options. |
2321

2422
<b>Returns:</b>
2523

26-
([HTMLIframeProps](./iframe-plugin.htmliframeprops.md) &amp; { key?: any; }) \| null
24+
[HTMLIframeProps](./iframe-plugin.htmliframeprops.md) \| null
2725

packages/iframe-plugin/etc/iframe-plugin.api.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { TBlock } from '@native-html/transient-render-engine';
1616
import { ViewStyle } from 'react-native';
1717

1818
// @public
19-
export function HTMLIframe({ WebView, webViewProps: userWebViewProps, source, style, onLinkPress, scaleFactor, injectedCSSStyles, removeBodySpacing, scalesPageToFit }: HTMLIframeProps): React_2.ReactElement<any, string | ((props: any) => React_2.ReactElement<any, string | any | (new (props: any) => React_2.Component<any, any, any>)> | null) | (new (props: any) => React_2.Component<any, any, any>)>;
19+
export function HTMLIframe({ WebView, webViewProps: userWebViewProps, source, style, onLinkPress, scaleFactor, injectedCSSStyles, removeBodySpacing, scalesPageToFit }: HTMLIframeProps): React_2.ReactElement<any, string | React_2.JSXElementConstructor<any>>;
2020

2121
// @public
2222
export interface HTMLIframeProps<WebViewProps = any> extends IframeConfig {
@@ -49,9 +49,7 @@ const IframeRenderer: CustomBlockRenderer;
4949
export default IframeRenderer;
5050

5151
// @public
52-
export function useHtmlIframeProps({ key, style, tnode }: CustomRendererProps<TBlock>, iframeConfig?: IframeConfig): (HTMLIframeProps & {
53-
key?: any;
54-
}) | null;
52+
export function useHtmlIframeProps({ style, tnode }: CustomRendererProps<TBlock>, iframeConfig?: IframeConfig): HTMLIframeProps | null;
5553

5654

5755
// (No @packageDocumentation comment for this package)

packages/iframe-plugin/src/__tests__/IframeRenderer.test.tsx

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import HTML from 'react-native-render-html';
2+
import HTML, { RenderHTMLProps } from 'react-native-render-html';
33
import renderer from 'react-test-renderer';
44
import WebView from 'react-native-webview';
55
import IframeRenderer from '../IframeRenderer';
@@ -21,4 +21,30 @@ describe('iframe renderer', () => {
2121
);
2222
}).not.toThrow();
2323
});
24+
it('should support provideEmbeddedHeaders prop', () => {
25+
const props: RenderHTMLProps = {
26+
source: {
27+
html: '<iframe width="300" height="300" src="https://google.com/" />'
28+
},
29+
provideEmbeddedHeaders: (uri, tagName, params) => {
30+
if (tagName === 'iframe') {
31+
params;
32+
return {
33+
'X-Frame-Options': 'ALLOW-FROM https://google.com'
34+
};
35+
}
36+
}
37+
};
38+
const rendered = renderer.create(
39+
<HTML
40+
WebView={WebView}
41+
renderers={{
42+
iframe: IframeRenderer
43+
}}
44+
contentWidth={10}
45+
{...props}
46+
/>
47+
);
48+
expect(rendered.toJSON()).toMatchSnapshot();
49+
});
2450
});

packages/iframe-plugin/src/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,17 @@ declare module 'react-native-render-html' {
1111
*/
1212
iframe?: IframeConfig;
1313
}
14+
15+
interface EmbeddedWithHeadersParamsMap {
16+
iframe: {
17+
/**
18+
* The print height of the iframe in DPI.
19+
*/
20+
printHeight: number;
21+
/**
22+
* The print width of the iframe in DPI.
23+
*/
24+
printWidth: number;
25+
};
26+
}
1427
}

packages/iframe-plugin/src/useHtmlIframeProps.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ export default function useHtmlIframeProps(
3232
{ style, tnode }: CustomRendererProps<TBlock>,
3333
iframeConfig?: IframeConfig
3434
): HTMLIframeProps | null {
35-
const { WebView, defaultWebViewProps, computeEmbeddedMaxWidth } =
36-
useSharedProps();
35+
const {
36+
WebView,
37+
defaultWebViewProps,
38+
computeEmbeddedMaxWidth,
39+
provideEmbeddedHeaders
40+
} = useSharedProps();
3741
const contentWidth = useContentWidth();
3842
const globalIframeConfig = useRendererProps('iframe');
3943
const { onPress: onLinkPress } = useRendererProps('a');
@@ -77,7 +81,13 @@ export default function useHtmlIframeProps(
7781

7882
const source = htmlAttribs.srcdoc
7983
? { html: htmlAttribs.srcdoc as string, baseUrl: documentBaseUrl }
80-
: { uri: normalizedUrl };
84+
: {
85+
uri: normalizedUrl,
86+
headers: provideEmbeddedHeaders?.(normalizedUrl, 'iframe', {
87+
printHeight,
88+
printWidth
89+
})
90+
};
8191

8292
if (__DEV__ && !WebView) {
8393
console.warn(

0 commit comments

Comments
 (0)