Skip to content
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

[Mobile] - Image corrector - Check the path extension is a valid one #62190

Merged
merged 5 commits into from
Jun 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export default function imageCorrector( node ) {
return;
}

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

Expand Down
12 changes: 12 additions & 0 deletions packages/edit-post/src/test/__snapshots__/editor.native.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Editor adds empty image block when pasting unsupported HTML local image path 1`] = `
"<!-- wp:image -->
<figure class="wp-block-image"><img src="" alt=""/></figure>
<!-- /wp:image -->"
`;

exports[`Editor adds image block when pasting HTML local image path 1`] = `
"<!-- wp:image -->
<figure class="wp-block-image"><img src="file:///path/to/file.png" alt=""/></figure>
<!-- /wp:image -->"
`;

exports[`Editor appends media correctly for allowed types 1`] = `
"<!-- wp:image -->
<figure class="wp-block-image"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt=""/></figure>
Expand Down
34 changes: 34 additions & 0 deletions packages/edit-post/src/test/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
getEditorHtml,
getEditorTitle,
initializeEditor,
pasteIntoRichText,
screen,
setupCoreBlocks,
within,
} from 'test/helpers';
import { BackHandler } from 'react-native';

Expand Down Expand Up @@ -98,6 +100,38 @@ describe( 'Editor', () => {
} );
} );

it( 'adds empty image block when pasting unsupported HTML local image path', async () => {
await initializeEditor();
await addBlock( screen, 'Paragraph' );

const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );
const paragraphTextInput =
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );

pasteIntoRichText( paragraphTextInput, {
text: '<div><img src="file:LOW-RES.png"></div>',
} );

expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'adds image block when pasting HTML local image path', async () => {
await initializeEditor();
await addBlock( screen, 'Paragraph' );

const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );
const paragraphTextInput =
within( paragraphBlock ).getByPlaceholderText( 'Start writing…' );

pasteIntoRichText( paragraphTextInput, {
files: [ 'file:///path/to/file.png' ],
} );

expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'appends media correctly for allowed types', async () => {
// Arrange
requestMediaImport
Expand Down
13 changes: 7 additions & 6 deletions test/native/integration-test-helpers/rich-text-paste.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import { fireEvent } from '@testing-library/react-native';
/**
* Paste content into a RichText component.
*
* @param {import('react-test-renderer').ReactTestInstance} richText RichText test instance.
* @param {Object} content Content to paste.
* @param {string} content.text Text format of the content.
* @param {string} [content.html] HTML format of the content. If not provided, text format will be used.
* @param {import('react-test-renderer').ReactTestInstance} richText RichText test instance.
* @param {Object} content Content to paste.
* @param {string} content.text Text format of the content.
* @param {string} [content.html] HTML format of the content. If not provided, text format will be used.
* @param {string} [content.files] Files array to add to the editor.
*/
export const pasteIntoRichText = ( richText, { text, html } ) => {
export const pasteIntoRichText = ( richText, { text, html, files = [] } ) => {
fireEvent( richText, 'focus' );
fireEvent( richText, 'paste', {
preventDefault: jest.fn(),
nativeEvent: {
eventCount: 1,
target: undefined,
files: [],
files,
pastedHtml: html || text,
pastedText: text,
},
Expand Down
Loading