Skip to content

Commit 68c22cc

Browse files
Gerardo Pachecopatil-vipul
Gerardo Pacheco
authored andcommitted
[Mobile] - Image corrector - Check the path extension is a valid one (WordPress#62190)
* Mobile - Image corrector - Check the path extension is a valid one * [Mobile] - Image corrector- Expand comment explaining the filtering for file: media paths * Integration Test helpers - Expand pasteIntoRichText to support passing files as a parameter * Mobile Editor Tests - Add new tests related to pasting HTML content with local image paths * Update snapshot
1 parent 3810fef commit 68c22cc

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

packages/blocks/src/api/raw-handling/image-corrector.native.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ export default function imageCorrector( node ) {
1010
return;
1111
}
1212

13-
if ( node.src.indexOf( 'file:' ) === 0 ) {
13+
// For local files makes sure the path doesn't end with an invalid extension.
14+
// This scenario often happens with content from MS Word and similar text apps.
15+
// We still need to support local files pasted from the users Media library.
16+
if ( node.src.startsWith( 'file:' ) && node.src.slice( -1 ) === '/' ) {
1417
node.setAttribute( 'src', '' );
1518
}
1619

packages/edit-post/src/test/__snapshots__/editor.native.js.snap

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Editor adds empty image block when pasting unsupported HTML local image path 1`] = `
4+
"<!-- wp:image -->
5+
<figure class="wp-block-image"><img src="" alt=""/></figure>
6+
<!-- /wp:image -->"
7+
`;
8+
9+
exports[`Editor adds image block when pasting HTML local image path 1`] = `
10+
"<!-- wp:image -->
11+
<figure class="wp-block-image"><img src="file:///path/to/file.png" alt=""/></figure>
12+
<!-- /wp:image -->"
13+
`;
14+
315
exports[`Editor appends media correctly for allowed types 1`] = `
416
"<!-- wp:image -->
517
<figure class="wp-block-image"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt=""/></figure>

packages/edit-post/src/test/editor.native.js

+34
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import {
99
getEditorHtml,
1010
getEditorTitle,
1111
initializeEditor,
12+
pasteIntoRichText,
1213
screen,
1314
setupCoreBlocks,
15+
within,
1416
} from 'test/helpers';
1517
import { BackHandler } from 'react-native';
1618

@@ -98,6 +100,38 @@ describe( 'Editor', () => {
98100
} );
99101
} );
100102

103+
it( 'adds empty image block when pasting unsupported HTML local image path', async () => {
104+
await initializeEditor();
105+
await addBlock( screen, 'Paragraph' );
106+
107+
const paragraphBlock = getBlock( screen, 'Paragraph' );
108+
fireEvent.press( paragraphBlock );
109+
const paragraphTextInput =
110+
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
111+
112+
pasteIntoRichText( paragraphTextInput, {
113+
text: '<div><img src="file:LOW-RES.png"></div>',
114+
} );
115+
116+
expect( getEditorHtml() ).toMatchSnapshot();
117+
} );
118+
119+
it( 'adds image block when pasting HTML local image path', async () => {
120+
await initializeEditor();
121+
await addBlock( screen, 'Paragraph' );
122+
123+
const paragraphBlock = getBlock( screen, 'Paragraph' );
124+
fireEvent.press( paragraphBlock );
125+
const paragraphTextInput =
126+
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );
127+
128+
pasteIntoRichText( paragraphTextInput, {
129+
files: [ 'file:///path/to/file.png' ],
130+
} );
131+
132+
expect( getEditorHtml() ).toMatchSnapshot();
133+
} );
134+
101135
it( 'appends media correctly for allowed types', async () => {
102136
// Arrange
103137
requestMediaImport

test/native/integration-test-helpers/rich-text-paste.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ import { fireEvent } from '@testing-library/react-native';
66
/**
77
* Paste content into a RichText component.
88
*
9-
* @param {import('react-test-renderer').ReactTestInstance} richText RichText test instance.
10-
* @param {Object} content Content to paste.
11-
* @param {string} content.text Text format of the content.
12-
* @param {string} [content.html] HTML format of the content. If not provided, text format will be used.
9+
* @param {import('react-test-renderer').ReactTestInstance} richText RichText test instance.
10+
* @param {Object} content Content to paste.
11+
* @param {string} content.text Text format of the content.
12+
* @param {string} [content.html] HTML format of the content. If not provided, text format will be used.
13+
* @param {string} [content.files] Files array to add to the editor.
1314
*/
14-
export const pasteIntoRichText = ( richText, { text, html } ) => {
15+
export const pasteIntoRichText = ( richText, { text, html, files = [] } ) => {
1516
fireEvent( richText, 'focus' );
1617
fireEvent( richText, 'paste', {
1718
preventDefault: jest.fn(),
1819
nativeEvent: {
1920
eventCount: 1,
2021
target: undefined,
21-
files: [],
22+
files,
2223
pastedHtml: html || text,
2324
pastedText: text,
2425
},

0 commit comments

Comments
 (0)