Skip to content
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
154 changes: 154 additions & 0 deletions e2e/tests/link-control.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import {
test,
expect,
assertNoBlockRecovery,
waitForBlockEditor,
} from 'e2e/test-utils'

test.describe( 'Link control URL input', () => {
const createdPostIds: Array<string | number> = []

test.afterEach( async ( { requestUtils } ) => {
for ( const id of createdPostIds.splice( 0 ) ) {
await requestUtils.deletePost( id ).catch( () => undefined )
}
} )

test( 'accepts safe link values and rejects executable protocols', async ( {
page,
admin,
editor,
requestUtils,
stackable,
} ) => {
await admin.createNewPost( { title: 'Link Control URL Input' } )
await editor.saveDraft()
const postQuery = new URL( editor.page.url() ).search
const postId = new URLSearchParams( postQuery ).get( 'post' )
if ( postId ) {
createdPostIds.push( postId )
}

await stackable.dismissToursAndNotices()
await waitForBlockEditor( editor )

await editor.insertBlock( { name: 'stackable/button-group' } )
await stackable.pickDefaultLayout( editor )
await stackable.selectBlockByName( editor, 'stackable/button' )

await stackable.openInspectorTab( 'Style' )

const inspector = page.getByRole( 'region', { name: 'Editor settings' } )
const linkPanel = inspector.locator( '.ugb-toggle-panel-body.ugb-panel--link' )
await expect( linkPanel ).toBeVisible()
if ( ! ( await linkPanel.getAttribute( 'class' ) || '' ).includes( 'is-opened' ) ) {
await linkPanel.locator( '.components-panel__body-toggle' ).click()
}
await expect( linkPanel ).toHaveClass( /is-opened/ )

const linkControl = linkPanel.locator( '.stk-link-control' ).filter( {
has: page.locator( '.stk-control-label', { hasText: /Link \/ URL/ } ),
} )
const input = linkControl.getByRole( 'combobox', { name: 'URL' } )
await expect( input ).toBeVisible()

const buttonBlock = editor.canvas.locator( '[data-type="stackable/button"]' ).first()
const clientId = await buttonBlock.getAttribute( 'data-block' )

const setLinkValue = async ( value: string ) => {
await input.click()
await input.fill( value )
await page.keyboard.press( 'Escape' )
await input.blur()
}

await setLinkValue( 'https://example.com' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'https://example.com' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )
await editor.saveDraft()
const safeContent = await editor.getEditedPostContent()
expect( safeContent ).toContain( 'href="https://example.com"' )

await setLinkValue( 'http://192.168.0.10' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'http://192.168.0.10' )

await setLinkValue( 'example.com' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'https://example.com' )

Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
await setLinkValue( '[my_shortcode]' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( '[my_shortcode]' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )

await setLinkValue( '!#stk_dynamic/current-page/url!#' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( '!#stk_dynamic/current-page/url!#' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )

await setLinkValue( '{{permalink}}' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( '{{permalink}}' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )

await setLinkValue( 'https://' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'https://' )
await expect( linkControl ).toContainText( 'Please enter a valid URL.' )

await setLinkValue( 'javascript:void(0)' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'javascript:void(0)' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )

await setLinkValue( 'javascript:;' )
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( 'javascript:;' )
await expect( linkControl ).not.toContainText( 'Please enter a valid URL.' )

await input.click()
await input.fill( 'javascript:alert(document.domain)' )
await expect( linkControl ).toContainText( 'Please enter a valid URL.' )
await input.blur()
await expect.poll( async () => {
const attributes = await editor.getBlockAttributes( clientId )
return attributes.linkUrl
} ).toBe( '' )

const legacyContent = safeContent.replace(
'href="https://example.com"',
'href="javascript:void(0)"'
)
const legacyPost = await requestUtils.createPost( {
title: 'Legacy Button Link',
status: 'publish',
content: legacyContent,
} )
createdPostIds.push( legacyPost.id )

await admin.editPost( String( legacyPost.id ) )
await stackable.dismissToursAndNotices()
await waitForBlockEditor( editor )
await assertNoBlockRecovery( editor )
} )
} )
96 changes: 96 additions & 0 deletions src/components/link-control/__test__/validate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* Internal dependencies
*/
import {
isPassThroughLinkValue,
isUrlLike,
isValidLinkValue,
normalizeLinkValue,
} from '../validate'

describe( 'link-control validation', () => {
it( 'treats empty values as pass-through', () => {
expect( isPassThroughLinkValue( '' ) ).toBe( true )
expect( isPassThroughLinkValue( ' ' ) ).toBe( true )
expect( isPassThroughLinkValue( undefined ) ).toBe( true )
expect( isValidLinkValue( '' ) ).toBe( true )
} )

it( 'allows shortcodes without treating them as URLs', () => {
expect( isPassThroughLinkValue( '[my_shortcode]' ) ).toBe( true )
expect( isPassThroughLinkValue( '[contact-form-7 id="1"]' ) ).toBe( true )
expect( isPassThroughLinkValue( '[site.url]' ) ).toBe( true )
expect( isValidLinkValue( '[my_shortcode]' ) ).toBe( true )
expect( normalizeLinkValue( '[my_shortcode]' ) ).toBe( '[my_shortcode]' )
} )

it( 'allows dynamic content tokens', () => {
const token = '!#stk_dynamic/current-page/url!#'
expect( isPassThroughLinkValue( token ) ).toBe( true )
expect( isValidLinkValue( token ) ).toBe( true )
expect( normalizeLinkValue( token ) ).toBe( token )
} )

it( 'allows other non-URL strings', () => {
expect( isPassThroughLinkValue( '{{permalink}}' ) ).toBe( true )
expect( isPassThroughLinkValue( '%post_url%' ) ).toBe( true )
expect( isPassThroughLinkValue( 'hello' ) ).toBe( true )
expect( isValidLinkValue( 'hello' ) ).toBe( true )
expect( isUrlLike( 'hello' ) ).toBe( false )
} )

it( 'recognizes URL-like values', () => {
expect( isUrlLike( 'https://example.com' ) ).toBe( true )
expect( isUrlLike( 'example.com' ) ).toBe( true )
expect( isUrlLike( 'www.example.com' ) ).toBe( true )
expect( isUrlLike( '#section' ) ).toBe( true )
expect( isUrlLike( '/about' ) ).toBe( true )
expect( isUrlLike( 'mailto:hi@example.com' ) ).toBe( true )
} )

it( 'accepts valid URLs, anchors, and relative paths', () => {
expect( isValidLinkValue( 'https://example.com' ) ).toBe( true )
expect( isValidLinkValue( '#section' ) ).toBe( true )
expect( isValidLinkValue( '/about' ) ).toBe( true )
expect( isValidLinkValue( '../parent' ) ).toBe( true )
expect( isValidLinkValue( 'mailto:hi@example.com' ) ).toBe( true )
} )

it( 'rejects incomplete URL-like values', () => {
expect( isValidLinkValue( 'https://' ) ).toBe( false )
expect( isValidLinkValue( 'http://' ) ).toBe( false )
expect( normalizeLinkValue( 'http://' ) ).toBe( 'http://' )
} )

it.each( [
'javascript:alert(1)',
'javascript:void(0);alert(1)',
'javascript:;alert(1)',
'JAVASCRIPT:alert(1)',
'java\nscript:alert(1)',
'java\tscript:alert(1)',
'data:text/html,<script>alert(1)</script>',
'vbscript:msgbox(1)',
] )( 'rejects executable protocol value %s', value => {
expect( isPassThroughLinkValue( value ) ).toBe( false )
expect( isValidLinkValue( value ) ).toBe( false )
expect( normalizeLinkValue( value ) ).toBe( '' )
} )

it.each( [
'javascript:void(0)',
'javascript:;',
] )( 'allows inert script link value %s', value => {
expect( isValidLinkValue( value ) ).toBe( true )
expect( normalizeLinkValue( value ) ).toBe( value )
} )

it( 'prepends https to bare domains on normalize', () => {
expect( normalizeLinkValue( 'example.com' ) ).toBe( 'https://example.com' )
expect( normalizeLinkValue( ' example.com ' ) ).toBe( 'https://example.com' )
expect( normalizeLinkValue( 'http://192.168.0.10' ) ).toBe( 'http://192.168.0.10' )
expect( normalizeLinkValue( 'https://example.com' ) ).toBe( 'https://example.com' )
expect( normalizeLinkValue( '#section' ) ).toBe( '#section' )
expect( normalizeLinkValue( '/about' ) ).toBe( '/about' )
} )
} )
67 changes: 12 additions & 55 deletions src/components/link-control/editor.scss
Original file line number Diff line number Diff line change
@@ -1,81 +1,38 @@
.stk-link-control__input {
// Adjust width to ensure reset button is visible.
width: calc(100% - 32px);
min-width: 0;

> .block-editor-link-control {
min-width: auto;
> .block-editor-url-input {
min-width: 0;
width: 100%;

.components-base-control {
min-width: auto;
min-width: 0;
width: 100%;
}

.components-input-base {
height: 30px;
}

.block-editor-link-control__search-input-wrapper {
margin: 0;
}

.block-editor-link-control__field {
.components-base-control__field {
margin: 0;
}

.block-editor-link-control__search-item-header {
overflow: hidden;
white-space: nowrap;
}

}

.block-editor-url-input__input {
.block-editor-url-input__input,
.components-input-control__input {
margin: 0 !important;
width: 100% !important;
height: 30px !important;
padding: 6px 8px !important;
}
.block-editor-link-control__search-actions {
display: none;
}
.block-editor-link-control__search-results-wrapper {
margin-bottom: 24px !important;
margin-top: 0 !important;
.block-editor-link-control__search-results {
margin: 0;
}
}
.block-editor-link-control__search-item {
flex-wrap: wrap;
}
.block-editor-link-control__search-item.is-current {
margin-top: -2px;
padding: 0 !important;
}
.block-editor-link-control__search-item-details {
max-width: 140px !important;
overflow: hidden;
}
}

.block-editor-link-control__search-item-icon {
display: none;
}

.block-editor-link-control__search-enter {
position: absolute;
top: 0;
right: 3px;
button:hover {
box-shadow: none !important;
}
.stk-link-control--invalid {
.components-base-control__help {
color: #cc1818;
}
}

// Adjust the location of the dynamic and reset buttons since our control is taller.
.stk-link-control {
.block-editor-link-control__field {
margin: auto;
}
.stk-dynamic-content-control {
display: flex;
align-items: center;
Expand Down
Loading
Loading