From 4caeb5f7c5ded0732c4a03860fe65ddf428b9910 Mon Sep 17 00:00:00 2001 From: Mayank-Tripathi32 Date: Wed, 8 Jan 2025 00:01:55 +0530 Subject: [PATCH] feat: added download attribute for buttons link --- docs/reference-guides/core-blocks.md | 2 +- packages/block-library/src/button/block.json | 7 +++++ packages/block-library/src/button/edit.js | 6 +++++ .../src/button/get-updated-link-attributes.js | 26 +++++++++++++++++++ packages/block-library/src/button/save.js | 2 ++ 5 files changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 26a865f0cdab75..b82963ec40d19b 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -52,7 +52,7 @@ Prompt visitors to take action with a button-style link. ([Source](https://githu - **Category:** design - **Parent:** core/buttons - **Supports:** anchor, color (background, gradients, text), interactivity (clientNavigation), shadow (), spacing (padding), splitting, typography (fontSize, lineHeight), ~~alignWide~~, ~~align~~, ~~reusable~~ -- **Attributes:** backgroundColor, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width +- **Attributes:** backgroundColor, download, gradient, linkTarget, placeholder, rel, tagName, text, textAlign, textColor, title, type, url, width ## Buttons diff --git a/packages/block-library/src/button/block.json b/packages/block-library/src/button/block.json index 6fcb7aca4c5923..3a6721f5a5d55a 100644 --- a/packages/block-library/src/button/block.json +++ b/packages/block-library/src/button/block.json @@ -55,6 +55,13 @@ "attribute": "rel", "role": "content" }, + "download": { + "type": "boolean", + "source": "attribute", + "selector": "a", + "attribute": "download", + "role": "content" + }, "placeholder": { "type": "string" }, diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js index 593066d6555b40..a656df43ad6cb8 100644 --- a/packages/block-library/src/button/edit.js +++ b/packages/block-library/src/button/edit.js @@ -59,6 +59,10 @@ const LINK_SETTINGS = [ id: 'nofollow', title: __( 'Mark as nofollow' ), }, + { + id: 'download', + title: __( 'Download file' ), + }, ]; function useEnter( props ) { @@ -380,6 +384,7 @@ function ButtonEdit( props ) { url: newURL, opensInNewTab: newOpensInNewTab, nofollow: newNofollow, + download: newDownload, } ) => setAttributes( getUpdatedLinkAttributes( { @@ -387,6 +392,7 @@ function ButtonEdit( props ) { url: newURL, opensInNewTab: newOpensInNewTab, nofollow: newNofollow, + download: newDownload, } ) ) } diff --git a/packages/block-library/src/button/get-updated-link-attributes.js b/packages/block-library/src/button/get-updated-link-attributes.js index 0e39a1815a3188..c0d81d1590c113 100644 --- a/packages/block-library/src/button/get-updated-link-attributes.js +++ b/packages/block-library/src/button/get-updated-link-attributes.js @@ -16,12 +16,14 @@ import { prependHTTP } from '@wordpress/url'; * @param {string} attributes.url The current link url. * @param {boolean} attributes.opensInNewTab Whether the link should open in a new window. * @param {boolean} attributes.nofollow Whether the link should be marked as nofollow. + * @param {boolean} attributes.download Whether the link should allow download. */ export function getUpdatedLinkAttributes( { rel = '', url = '', opensInNewTab, nofollow, + download = false, } ) { let newLinkTarget; // Since `rel` is editable attribute, we need to check for existing values and proceed accordingly. @@ -46,9 +48,33 @@ export function getUpdatedLinkAttributes( { updatedRel = updatedRel?.replace( relRegex, '' ).trim(); } + const allowDownload = url && isSameOrigin( url ) ? download : undefined; + return { url: prependHTTP( url ), linkTarget: newLinkTarget, rel: updatedRel || undefined, + download: allowDownload, }; } + +/** + * Checks if the URL is same origin. + * Allow relative URLs. + * + * @param {string} urlString The URL to check. + * @return {boolean} Whether the URL is same origin. + */ +function isSameOrigin( urlString ) { + // Allow relative URLs + if ( urlString.startsWith( '/' ) ) { + return true; + } + + try { + const url = new URL( urlString, window.location.origin ); + return url.origin === window.location.origin; + } catch { + return false; + } +} diff --git a/packages/block-library/src/button/save.js b/packages/block-library/src/button/save.js index 4255868d50fbc5..dcd647ea29b181 100644 --- a/packages/block-library/src/button/save.js +++ b/packages/block-library/src/button/save.js @@ -30,6 +30,7 @@ export default function save( { attributes, className } ) { title, url, width, + download, } = attributes; const TagName = tagName || 'a'; @@ -83,6 +84,7 @@ export default function save( { attributes, className } ) { value={ text } target={ isButtonTag ? null : linkTarget } rel={ isButtonTag ? null : rel } + download={ isButtonTag ? null : download } /> );