From 39c26cb1750ec77f2a413d9d91867f4c418cc3af Mon Sep 17 00:00:00 2001 From: Hiroshi Urabe Date: Thu, 24 Jun 2021 18:40:14 +0900 Subject: [PATCH] Fixed a problem with double encoding of URLs. (#32840) * Fixed a problem with double encoding of URLs. * To prevent double application of encodeURI, decodeURI is executed to revert to the original string. * Use safeDecodeURI instead of decodeURI. * add snapshot test * Update packages/block-library/src/navigation-link/edit.js Co-authored-by: Dave Smith * Update packages/e2e-tests/specs/experiments/blocks/navigation.test.js Co-authored-by: Dave Smith * Update packages/e2e-tests/specs/experiments/blocks/navigation.test.js Co-authored-by: Dave Smith * Update packages/e2e-tests/specs/experiments/blocks/navigation.test.js Co-authored-by: Dave Smith * Update packages/e2e-tests/specs/experiments/blocks/navigation.test.js Co-authored-by: Dave Smith Co-authored-by: Dave Smith --- .../block-library/src/navigation-link/edit.js | 5 +- .../__snapshots__/navigation.test.js.snap | 8 +++ .../experiments/blocks/navigation.test.js | 60 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/packages/block-library/src/navigation-link/edit.js b/packages/block-library/src/navigation-link/edit.js index 641d95ec5c2e06..b4efe383aa00c4 100644 --- a/packages/block-library/src/navigation-link/edit.js +++ b/packages/block-library/src/navigation-link/edit.js @@ -30,7 +30,7 @@ import { useBlockProps, store as blockEditorStore, } from '@wordpress/block-editor'; -import { isURL, prependHTTP } from '@wordpress/url'; +import { isURL, prependHTTP, safeDecodeURI } from '@wordpress/url'; import { Fragment, useState, @@ -200,7 +200,8 @@ export const updateNavigationLinkBlockAttributes = ( const kind = isCustomLink ? 'custom' : newKind; setAttributes( { - ...( url && { url: encodeURI( url ) } ), + // Passed `url` may already be encoded. To prevent double encoding, decodeURI is executed to revert to the original string. + ...( url && { url: encodeURI( safeDecodeURI( url ) ) } ), ...( label && { label } ), ...( undefined !== opensInNewTab && { opensInNewTab } ), ...( id && Number.isInteger( id ) && { id } ), diff --git a/packages/e2e-tests/specs/experiments/blocks/__snapshots__/navigation.test.js.snap b/packages/e2e-tests/specs/experiments/blocks/__snapshots__/navigation.test.js.snap index 25bae5dff448ea..915de5978854a4 100644 --- a/packages/e2e-tests/specs/experiments/blocks/__snapshots__/navigation.test.js.snap +++ b/packages/e2e-tests/specs/experiments/blocks/__snapshots__/navigation.test.js.snap @@ -57,3 +57,11 @@ exports[`Navigation allows pages to be created from the navigation block and the " `; + +exports[`Navigation encodes URL when create block if needed 1`] = ` +" + + + +" +`; diff --git a/packages/e2e-tests/specs/experiments/blocks/navigation.test.js b/packages/e2e-tests/specs/experiments/blocks/navigation.test.js index ed410616f9e07f..9212d6ee436e8e 100644 --- a/packages/e2e-tests/specs/experiments/blocks/navigation.test.js +++ b/packages/e2e-tests/specs/experiments/blocks/navigation.test.js @@ -465,6 +465,66 @@ describe( 'Navigation', () => { expect( await getEditedPostContent() ).toMatchSnapshot(); } ); + it( 'encodes URL when create block if needed', async () => { + // Add the navigation block. + await insertBlock( 'Navigation' ); + + // Create an empty nav block. + await page.waitForSelector( '.wp-block-navigation-placeholder' ); + + await createEmptyNavBlock(); + + await addLinkBlock(); + + // Add a link to the Link block. + await updateActiveNavigationLink( { + url: 'https://wordpress.org/шеллы', + type: 'url', + } ); + + await showBlockToolbar(); + + await addLinkBlock(); + + // Wait for URL input to be focused + await page.waitForSelector( + 'input.block-editor-url-input__input:focus' + ); + + // After adding a new block, search input should be shown immediately. + const isInURLInput = await page.evaluate( + () => + !! document.activeElement.matches( + 'input.block-editor-url-input__input' + ) + ); + expect( isInURLInput ).toBe( true ); + await page.keyboard.press( 'Escape' ); + + // Click the link placeholder + const placeholder = await page.waitForSelector( + '.wp-block-navigation-link__placeholder' + ); + await placeholder.click(); + + // Mocked response for internal page. + // We are encoding the slug/url in order + // that we can assert it is not double encoded by the block. + await mockSearchResponse( [ + { title: 'お問い合わせ', slug: encodeURI( 'お問い合わせ' ) }, + ] ); + + // Select the mocked internal page above. + await updateActiveNavigationLink( { + url: 'お問い合わせ', + type: 'entity', + } ); + + // Expect a Navigation Block with two Links in the snapshot. + // The 2nd link should not be double encoded. + expect( await getEditedPostContent() ).toMatchSnapshot(); + } ); + it( 'allows pages to be created from the navigation block and their links added to menu', async () => { // Mock request for creating pages and the page search response. // We mock the page search to return no results and we use a very long