-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Patch WordPress] Ensure block editor iframe is controlled by a servi…
…ce worker Fixes #646 Patches the block editor to use a special ControlledIframe component instead of a regular HTML "iframe" element. The goal is to make the iframe use a plain HTTP URL instead of srcDoc, blob URL and other variations. Why? In Playground, the editor iframe needs to be controlled by Playground's service worker so it can serve CSS and other static assets. Otherwise all the requests originating in that iframe will yield 404s. However, different WordPress versions use a variety of iframe techniques that result in a non-controlled iframe: * 6.3 uses a binary blob URL and the frame isn't controlled by a service worker * <= 6.2 uses srcdoc had a null origin and the frame isn't controlled by a service worker * Other dynamic techniques, such as using a data URL, also fail to produce a controlled iframe HTTP URL src like src="/doc.html" seems to be the only way to create a controlled iframe. And so, this commit ensures that the block editor iframe uses a plain HTTP URL regardless of the WordPress version. Related: WordPress/gutenberg#55152
- Loading branch information
Showing
23 changed files
with
561 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/playground/compile-wordpress/build-assets/controlled-iframe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* A synchronous function to read a blob URL as text. | ||
* | ||
* @param {string} url | ||
* @returns {string} | ||
*/ | ||
const __playground_readBlobAsText = function (url) { | ||
try { | ||
let xhr = new XMLHttpRequest(); | ||
xhr.open('GET', url, false); | ||
xhr.overrideMimeType('text/plain;charset=utf-8'); | ||
xhr.send(); | ||
return xhr.responseText; | ||
} catch(e) { | ||
return ''; | ||
} finally { | ||
URL.revokeObjectURL(url); | ||
} | ||
} | ||
|
||
window.__playground_ControlledIframe = window.wp.element.forwardRef(function (props, ref) { | ||
const source = window.wp.element.useMemo(function () { | ||
if (props.srcDoc) { | ||
// WordPress <= 6.2 uses a srcDoc that only contains a doctype. | ||
return '/wp-includes/empty.html'; | ||
} else if (props.src && props.src.startsWith('blob:')) { | ||
// WordPress 6.3 uses a blob URL with doctype and a list of static assets. | ||
// Let's pass the document content to empty.html and render it there. | ||
return '/wp-includes/empty.html#' + encodeURIComponent(__playground_readBlobAsText(props.src)); | ||
} else { | ||
// WordPress >= 6.4 uses a plain HTTPS URL that needs no correction. | ||
return props.src; | ||
} | ||
}, [props.src]); | ||
return ( | ||
window.wp.element.createElement('iframe', { | ||
...props, | ||
ref: ref, | ||
src: source, | ||
// Make sure there's no srcDoc, as it would interfere with the src. | ||
srcDoc: undefined | ||
}) | ||
) | ||
}); |
2 changes: 1 addition & 1 deletion
2
packages/playground/remote/public/wp-5.9/wp-includes/empty.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html> | ||
<!doctype html><script>const hash = window.location.hash.substring(1); if ( hash ) document.write(decodeURIComponent(hash))</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
packages/playground/remote/public/wp-5.9/wp-includes/js/dist/block-editor.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/playground/remote/public/wp-6.0/wp-includes/empty.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html> | ||
<!doctype html><script>const hash = window.location.hash.substring(1); if ( hash ) document.write(decodeURIComponent(hash))</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
packages/playground/remote/public/wp-6.0/wp-includes/js/dist/block-editor.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/playground/remote/public/wp-6.1/wp-includes/empty.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html> | ||
<!doctype html><script>const hash = window.location.hash.substring(1); if ( hash ) document.write(decodeURIComponent(hash))</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
packages/playground/remote/public/wp-6.1/wp-includes/js/dist/block-editor.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/playground/remote/public/wp-6.2/wp-includes/empty.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html> | ||
<!doctype html><script>const hash = window.location.hash.substring(1); if ( hash ) document.write(decodeURIComponent(hash))</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
packages/playground/remote/public/wp-6.2/wp-includes/js/dist/block-editor.min.js
Large diffs are not rendered by default.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
packages/playground/remote/public/wp-6.3/wp-includes/empty.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!doctype html> | ||
<!doctype html><script>const hash = window.location.hash.substring(1); if ( hash ) document.write(decodeURIComponent(hash))</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.