Skip to content

Commit

Permalink
Prepend HTTP to Buttons block links when missing protocol (#47311)
Browse files Browse the repository at this point in the history
* Prepend HTTP

* Add e2e test
  • Loading branch information
getdave authored Feb 9, 2023
1 parent b4a1cbf commit 0daf720
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { displayShortcut, isKeyboardEvent } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
import { useMergeRefs } from '@wordpress/compose';
import { prependHTTP } from '@wordpress/url';

const NEW_TAB_REL = 'noreferrer noopener';

Expand Down Expand Up @@ -240,7 +241,7 @@ function ButtonEdit( props ) {
url: newURL = '',
opensInNewTab: newOpensInNewTab,
} ) => {
setAttributes( { url: newURL } );
setAttributes( { url: prependHTTP( newURL ) } );

if ( opensInNewTab !== newOpensInNewTab ) {
onToggleOpenInNewTab( newOpensInNewTab );
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/specs/editor/blocks/buttons.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,29 @@ test.describe( 'Buttons', () => {
).toBeVisible();
} );

test( 'appends http protocol to links added which are missing a protocol', async ( {
editor,
page,
pageUtils,
} ) => {
// Regression: https://github.com/WordPress/gutenberg/issues/34307
await editor.insertBlock( { name: 'core/buttons' } );
await pageUtils.pressKeyWithModifier( 'primary', 'k' );

const urlInput = page.locator( 'role=combobox[name="URL"i]' );

await expect( urlInput ).toBeFocused();
await page.keyboard.type( 'example.com' );
await page.keyboard.press( 'Enter' );

// Move to "Edit" and switch UI back to edit mode
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Enter' );

// Check the value of the URL input has had http:// prepended.
await expect( urlInput ).toHaveValue( 'http://example.com' );
} );

test( 'can jump to the link editor using the keyboard shortcut', async ( {
editor,
page,
Expand Down

1 comment on commit 0daf720

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 0daf720.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4133478644
📝 Reported issues:

Please sign in to comment.