From 917a761bfa13a08fedeb338324c654cd7ebaeff4 Mon Sep 17 00:00:00 2001 From: Ella van Durpe Date: Wed, 18 Mar 2020 11:39:18 +0100 Subject: [PATCH] Paste: replace iframes with url --- .../src/components/rich-text/index.js | 9 +----- packages/block-library/src/html/index.js | 2 -- packages/block-library/src/html/transforms.js | 29 ------------------- packages/blocks/README.md | 1 - .../src/api/raw-handling/iframe-remover.js | 8 ++--- .../src/api/raw-handling/paste-handler.js | 22 +++++--------- test/integration/blocks-raw-handling.test.js | 6 +--- .../fixtures/iframe-embed-out.html | 8 +++-- 8 files changed, 17 insertions(+), 68 deletions(-) delete mode 100644 packages/block-library/src/html/transforms.js diff --git a/packages/block-editor/src/components/rich-text/index.js b/packages/block-editor/src/components/rich-text/index.js index 6cfbbcc51ac99..7fc7f32291b45 100644 --- a/packages/block-editor/src/components/rich-text/index.js +++ b/packages/block-editor/src/components/rich-text/index.js @@ -141,10 +141,7 @@ function RichTextWrapper( const selectionStart = getSelectionStart(); const selectionEnd = getSelectionEnd(); - const { - __experimentalCanUserUseUnfilteredHTML, - __experimentalUndo: undo, - } = getSettings(); + const { __experimentalUndo: undo } = getSettings(); let isSelected; @@ -171,7 +168,6 @@ function RichTextWrapper( } return { - canUserUseUnfilteredHTML: __experimentalCanUserUseUnfilteredHTML, isCaretWithinFormattedText: isCaretWithinFormattedText(), selectionStart: isSelected ? selectionStart.offset : undefined, selectionEnd: isSelected ? selectionEnd.offset : undefined, @@ -186,7 +182,6 @@ function RichTextWrapper( // retreived from the store on merge. // To do: fix this somehow. const { - canUserUseUnfilteredHTML, isCaretWithinFormattedText, selectionStart, selectionEnd, @@ -394,7 +389,6 @@ function RichTextWrapper( plainText, mode, tagName, - canUserUseUnfilteredHTML, } ); if ( typeof content === 'string' ) { @@ -437,7 +431,6 @@ function RichTextWrapper( onSplit, splitValue, __unstableEmbedURLOnPaste, - canUserUseUnfilteredHTML, multiline, ] ); diff --git a/packages/block-library/src/html/index.js b/packages/block-library/src/html/index.js index f3e592da15318..78abd06156f10 100644 --- a/packages/block-library/src/html/index.js +++ b/packages/block-library/src/html/index.js @@ -10,7 +10,6 @@ import { html as icon } from '@wordpress/icons'; import edit from './edit'; import metadata from './block.json'; import save from './save'; -import transforms from './transforms'; const { name } = metadata; @@ -34,7 +33,6 @@ export const settings = { className: false, html: false, }, - transforms, edit, save, }; diff --git a/packages/block-library/src/html/transforms.js b/packages/block-library/src/html/transforms.js deleted file mode 100644 index 7b6c054f1c208..0000000000000 --- a/packages/block-library/src/html/transforms.js +++ /dev/null @@ -1,29 +0,0 @@ -const transforms = { - from: [ - { - type: 'raw', - isMatch: ( node ) => - node.nodeName === 'FIGURE' && !! node.querySelector( 'iframe' ), - schema: ( { phrasingContentSchema } ) => ( { - figure: { - require: [ 'iframe' ], - children: { - iframe: { - attributes: [ - 'src', - 'allowfullscreen', - 'height', - 'width', - ], - }, - figcaption: { - children: phrasingContentSchema, - }, - }, - }, - } ), - }, - ], -}; - -export default transforms; diff --git a/packages/blocks/README.md b/packages/blocks/README.md index 2f78d4d7c2e29..d6cf2684deaea 100644 --- a/packages/blocks/README.md +++ b/packages/blocks/README.md @@ -608,7 +608,6 @@ _Parameters_ - _options.plainText_ `[string]`: Plain text version. - _options.mode_ `[string]`: Handle content as blocks or inline content. _ 'AUTO': Decide based on the content passed. _ 'INLINE': Always handle as inline content, and return string. \* 'BLOCKS': Always handle as blocks, and return array of blocks. - _options.tagName_ `[Array]`: The tag into which content will be inserted. -- _options.canUserUseUnfilteredHTML_ `[boolean]`: Whether or not the user can use unfiltered HTML. _Returns_ diff --git a/packages/blocks/src/api/raw-handling/iframe-remover.js b/packages/blocks/src/api/raw-handling/iframe-remover.js index 35adb29cdf808..e7c951507cf83 100644 --- a/packages/blocks/src/api/raw-handling/iframe-remover.js +++ b/packages/blocks/src/api/raw-handling/iframe-remover.js @@ -1,8 +1,3 @@ -/** - * WordPress dependencies - */ -import { remove } from '@wordpress/dom'; - /** * Removes iframes. * @@ -12,6 +7,7 @@ import { remove } from '@wordpress/dom'; */ export default function( node ) { if ( node.nodeName === 'IFRAME' ) { - remove( node ); + const text = node.ownerDocument.createTextNode( node.src ); + node.parentNode.replaceChild( text, node ); } } diff --git a/packages/blocks/src/api/raw-handling/paste-handler.js b/packages/blocks/src/api/raw-handling/paste-handler.js index 50738ae0358ce..cb94bbf770e5c 100644 --- a/packages/blocks/src/api/raw-handling/paste-handler.js +++ b/packages/blocks/src/api/raw-handling/paste-handler.js @@ -126,14 +126,13 @@ function htmlToBlocks( { html, rawTransforms } ) { * Converts an HTML string to known blocks. Strips everything else. * * @param {Object} options - * @param {string} [options.HTML] The HTML to convert. - * @param {string} [options.plainText] Plain text version. - * @param {string} [options.mode] Handle content as blocks or inline content. - * * 'AUTO': Decide based on the content passed. - * * 'INLINE': Always handle as inline content, and return string. - * * 'BLOCKS': Always handle as blocks, and return array of blocks. - * @param {Array} [options.tagName] The tag into which content will be inserted. - * @param {boolean} [options.canUserUseUnfilteredHTML] Whether or not the user can use unfiltered HTML. + * @param {string} [options.HTML] The HTML to convert. + * @param {string} [options.plainText] Plain text version. + * @param {string} [options.mode] Handle content as blocks or inline content. + * * 'AUTO': Decide based on the content passed. + * * 'INLINE': Always handle as inline content, and return string. + * * 'BLOCKS': Always handle as blocks, and return array of blocks. + * @param {Array} [options.tagName] The tag into which content will be inserted. * * @return {Array|string} A list of blocks or a string, depending on `handlerMode`. */ @@ -142,7 +141,6 @@ export function pasteHandler( { plainText = '', mode = 'AUTO', tagName, - canUserUseUnfilteredHTML = false, } ) { // First of all, strip any meta tags. HTML = HTML.replace( /]+>/g, '' ); @@ -243,15 +241,11 @@ export function pasteHandler( { phrasingContentReducer, specialCommentConverter, commentRemover, + iframeRemover, figureContentReducer, blockquoteNormaliser, ]; - if ( ! canUserUseUnfilteredHTML ) { - // Should run before `figureContentReducer`. - filters.unshift( iframeRemover ); - } - const schema = { ...blockContentSchema, // Keep top-level phrasing content, normalised by `normaliseBlocks`. diff --git a/test/integration/blocks-raw-handling.test.js b/test/integration/blocks-raw-handling.test.js index 7abe9fb4bb3d6..60a0722e3837f 100644 --- a/test/integration/blocks-raw-handling.test.js +++ b/test/integration/blocks-raw-handling.test.js @@ -398,11 +398,7 @@ describe( 'Blocks raw handling', () => { throw new Error( `Expected fixtures for type ${ type }` ); } - const converted = pasteHandler( { - HTML, - plainText, - canUserUseUnfilteredHTML: true, - } ); + const converted = pasteHandler( { HTML, plainText } ); const serialized = typeof converted === 'string' ? converted diff --git a/test/integration/fixtures/iframe-embed-out.html b/test/integration/fixtures/iframe-embed-out.html index 6eeb487da82b5..262eb52abe71c 100644 --- a/test/integration/fixtures/iframe-embed-out.html +++ b/test/integration/fixtures/iframe-embed-out.html @@ -1,3 +1,5 @@ - -
- + +
+https://www.google.com/maps/embed +
+ \ No newline at end of file