Skip to content

Add document.write() fallback #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ The `mountTarget` props is a css selector (#target/.target) that specifies where
</Frame>
```

###### dangerouslyUseDocWrite
`dangerouslyUseDocWrite: PropTypes.bool`

Defaults to `false`

The frame's initial content, as defined by the `initialContent` prop, is populated via the frame's `srcdoc` attribute by default. However, this can cause issues with some libraries such as Recaptcha and Google Maps that depend on the frame's location/origin. In these cases, setting this flag will cause `Frame` to use `document.write()` to populate the initial content. This is **unperformant and unrecommended**, but allows these libraries to be used inside a `Frame` instance.

###### contentDidMount and contentDidUpdate
`contentDidMount: PropTypes.func`
`contentDidUpdate: PropTypes.func`
Expand Down
15 changes: 14 additions & 1 deletion src/Frame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class Frame extends Component {
head: PropTypes.node,
initialContent: PropTypes.string,
mountTarget: PropTypes.string,
dangerouslyUseDocWrite: PropTypes.bool,
contentDidMount: PropTypes.func,
contentDidUpdate: PropTypes.func,
children: PropTypes.oneOfType([
Expand All @@ -27,6 +28,7 @@ export class Frame extends Component {
head: null,
children: undefined,
mountTarget: undefined,
dangerouslyUseDocWrite: false,
contentDidMount: () => {},
contentDidUpdate: () => {},
initialContent:
Expand Down Expand Up @@ -126,6 +128,12 @@ export class Frame extends Component {
</Content>
);

if (this.props.dangerouslyUseDocWrite && doc.body.children.length < 1) {
doc.open('text/html', 'replace');
doc.write(this.props.initialContent);
doc.close();
}

const mountTarget = this.getMountTarget();

if (!mountTarget) {
Expand All @@ -141,12 +149,17 @@ export class Frame extends Component {
render() {
const props = {
...this.props,
srcDoc: this.props.initialContent,
children: undefined // The iframe isn't ready so we drop children from props here. #12, #17
};

if (!this.props.dangerouslyUseDocWrite) {
props.srcDoc = this.props.initialContent;
}

delete props.head;
delete props.initialContent;
delete props.mountTarget;
delete props.dangerouslyUseDocWrite;
delete props.contentDidMount;
delete props.contentDidUpdate;
delete props.forwardedRef;
Expand Down
20 changes: 20 additions & 0 deletions test/Frame.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ describe('The Frame Component', () => {
);
});

it('should allow setting initialContent via document.write() when required', () => {
div = document.body.appendChild(document.createElement('div'));

const initialContent =
'<!DOCTYPE html><html><head><script>console.log("foo");</script></head><body><div></div></body></html>';
const renderedContent =
'<html><head><script>console.log("foo");</script></head><body><div><div class="frame-content"></div></div></body></html>';
const frame = ReactDOM.render(
<Frame
dangerouslyUseDocWrite
initialContent={initialContent}
contentDidMount={() => {
const doc = ReactDOM.findDOMNode(frame).contentDocument;
expect(doc.documentElement.outerHTML).to.equal(renderedContent);
}}
/>,
div
);
});

it('should allow setting mountTarget', done => {
div = document.body.appendChild(document.createElement('div'));

Expand Down