Repository navigation
feat: use a direct URL input in Stackable link controls #3756
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
Open
Arukuen
wants to merge
8
commits into
develop
Choose a base branch
from
feat/unrestricted-url-setting
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
787e071
feat: add an option in Stackable setting to use unrestricted link con…
Arukuen 82c570c
fix: undo adding of option, use URLInput for stackable LinkControl
Arukuen cf30d48
feat: validate Stackable link URLs while allowing shortcodes and tokens
bfintal 45f717e
fix: preserve explicit protocols and relative paths in link URLs
Arukuen 4029afe
fix: fix link validation for unsafe schemes and domains without proto…
Arukuen 1723ec0
fix: omit executable protocols from rendered links
Arukuen b768cba
test: describe file name, email, and localhost link behavior
bfintal d8054d0
fix: handle file names, emails, and localhost links
Arukuen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { | ||
| test, | ||
| expect, | ||
| 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, | ||
| 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 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( 'images/photo.jpg' ) | ||
| await expect.poll( async () => { | ||
| const attributes = await editor.getBlockAttributes( clientId ) | ||
| return attributes.linkUrl | ||
| } ).toBe( 'images/photo.jpg' ) | ||
|
|
||
| await setLinkValue( 'docs/readme.md' ) | ||
| await expect.poll( async () => { | ||
| const attributes = await editor.getBlockAttributes( clientId ) | ||
| return attributes.linkUrl | ||
| } ).toBe( 'docs/readme.md' ) | ||
|
|
||
| await setLinkValue( 'example.org' ) | ||
| await expect.poll( async () => { | ||
| const attributes = await editor.getBlockAttributes( clientId ) | ||
| return attributes.linkUrl | ||
| } ).toBe( 'https://example.org' ) | ||
|
|
||
| await setLinkValue( 'example.photography' ) | ||
| await expect.poll( async () => { | ||
| const attributes = await editor.getBlockAttributes( clientId ) | ||
| return attributes.linkUrl | ||
| } ).toBe( 'https://example.photography' ) | ||
|
|
||
| await setLinkValue( 'example.com:8443' ) | ||
| await expect.poll( async () => { | ||
| const attributes = await editor.getBlockAttributes( clientId ) | ||
| return attributes.linkUrl | ||
| } ).toBe( 'https://example.com:8443' ) | ||
|
|
||
| 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 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( '' ) | ||
|
|
||
| await page.evaluate( id => { | ||
| window.wp.data.dispatch( 'core/block-editor' ).updateBlockAttributes( id, { | ||
| linkUrl: 'javascript:alert(document.domain)', | ||
| } ) | ||
| }, clientId ) | ||
| await editor.saveDraft() | ||
|
|
||
| const savedContent = await editor.getEditedPostContent() | ||
| expect( savedContent ).not.toContain( 'href="javascript:' ) | ||
| } ) | ||
| } ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * 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 ) | ||
| } ) | ||
|
|
||
| it.each( [ | ||
| '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( 'prepends https to bare domains on normalize', () => { | ||
| expect( normalizeLinkValue( 'example.com' ) ).toBe( 'https://example.com' ) | ||
| expect( normalizeLinkValue( 'example.com/about' ) ).toBe( 'https://example.com/about' ) | ||
| expect( normalizeLinkValue( 'example.photography' ) ).toBe( 'https://example.photography' ) | ||
| expect( normalizeLinkValue( 'example.com:8443' ) ).toBe( 'https://example.com:8443' ) | ||
| 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' ) | ||
| } ) | ||
|
|
||
| it( 'leaves non-domain paths as entered', () => { | ||
| expect( normalizeLinkValue( 'images/photo.jpg' ) ).toBe( 'images/photo.jpg' ) | ||
| expect( normalizeLinkValue( 'docs/readme.md' ) ).toBe( 'docs/readme.md' ) | ||
| } ) | ||
|
|
||
| // A file name is not a website. The extension must not be treated as a domain. | ||
| it( 'keeps file names instead of adding https', () => { | ||
| expect( isUrlLike( 'readme.md' ) ).toBe( false ) | ||
| expect( isValidLinkValue( 'readme.md' ) ).toBe( true ) | ||
| expect( normalizeLinkValue( 'readme.md' ) ).toBe( 'readme.md' ) | ||
| expect( normalizeLinkValue( 'photo.jpg' ) ).toBe( 'photo.jpg' ) | ||
| expect( normalizeLinkValue( 'jquery.min.js' ) ).toBe( 'jquery.min.js' ) | ||
| expect( normalizeLinkValue( 'index.html' ) ).toBe( 'index.html' ) | ||
| } ) | ||
|
|
||
| // An email is not a website. It must stay as the user typed it. | ||
| it( 'keeps an email address instead of turning it into a website', () => { | ||
| expect( isValidLinkValue( 'hi@example.com' ) ).toBe( true ) | ||
| expect( normalizeLinkValue( 'hi@example.com' ) ).toBe( 'hi@example.com' ) | ||
| } ) | ||
|
|
||
| // localhost:3000 is a host and a port, same idea as example.com:8443. | ||
| it( 'adds https to localhost with a port', () => { | ||
| expect( isValidLinkValue( 'localhost:3000' ) ).toBe( true ) | ||
| expect( normalizeLinkValue( 'localhost:3000' ) ).toBe( 'https://localhost:3000' ) | ||
| } ) | ||
| } ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.