From a9a81072dad851524ce46d7aebff22a7e8e6d9c0 Mon Sep 17 00:00:00 2001 From: Naerriel Date: Mon, 28 Jan 2019 20:14:40 +0100 Subject: [PATCH 1/4] Decode URL encoding slug in sidebar permalink --- packages/edit-post/src/components/sidebar/post-link/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/edit-post/src/components/sidebar/post-link/index.js b/packages/edit-post/src/components/sidebar/post-link/index.js index 760f71109911c0..436c332a989172 100644 --- a/packages/edit-post/src/components/sidebar/post-link/index.js +++ b/packages/edit-post/src/components/sidebar/post-link/index.js @@ -33,7 +33,8 @@ function PostLink( { } ) { const { prefix, suffix } = permalinkParts; let prefixElement, postNameElement, suffixElement; - const currentSlug = postSlug || cleanForSlug( postTitle ) || postID; + const currentSlug = decodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; + if ( isEditable ) { prefixElement = prefix && ( { prefix } From d2d6928b540748e7556a37191a760401b1ed8b5f Mon Sep 17 00:00:00 2001 From: Naerriel Date: Mon, 28 Jan 2019 20:22:19 +0100 Subject: [PATCH 2/4] Decode URL encoding slug in sidebar permalink pt.2. --- packages/edit-post/src/components/sidebar/post-link/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/edit-post/src/components/sidebar/post-link/index.js b/packages/edit-post/src/components/sidebar/post-link/index.js index 436c332a989172..681dc1f9ae426c 100644 --- a/packages/edit-post/src/components/sidebar/post-link/index.js +++ b/packages/edit-post/src/components/sidebar/post-link/index.js @@ -34,7 +34,6 @@ function PostLink( { const { prefix, suffix } = permalinkParts; let prefixElement, postNameElement, suffixElement; const currentSlug = decodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; - if ( isEditable ) { prefixElement = prefix && ( { prefix } From c3738d21752936b3092550c20207c9dbd33f4cf2 Mon Sep 17 00:00:00 2001 From: Naerriel Date: Wed, 30 Jan 2019 16:16:25 +0100 Subject: [PATCH 3/4] Decode URL encoding slug in sidebar permalink pt.3. --- packages/editor/src/components/post-permalink/index.js | 2 +- .../editor/src/components/post-publish-panel/postpublish.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/components/post-permalink/index.js b/packages/editor/src/components/post-permalink/index.js index c28a1d1754485f..7054b108318550 100644 --- a/packages/editor/src/components/post-permalink/index.js +++ b/packages/editor/src/components/post-permalink/index.js @@ -78,7 +78,7 @@ class PostPermalink extends Component { const ariaLabel = isCopied ? __( 'Permalink copied' ) : __( 'Copy the permalink' ); const { prefix, suffix } = permalinkParts; - const slug = postSlug || cleanForSlug( postTitle ) || postID; + const slug = decodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; const samplePermalink = ( isEditable ) ? prefix + slug + suffix : prefix; return ( diff --git a/packages/editor/src/components/post-publish-panel/postpublish.js b/packages/editor/src/components/post-publish-panel/postpublish.js index 0e0cc7f81862b1..6fee634dbff710 100644 --- a/packages/editor/src/components/post-publish-panel/postpublish.js +++ b/packages/editor/src/components/post-publish-panel/postpublish.js @@ -79,7 +79,7 @@ class PostPublishPanelPostpublish extends Component { /* translators: %s: post type singular name */ __( '%s address' ), postLabel ) } - value={ post.link } + value={ decodeURIComponent( post.link ) } onFocus={ this.onSelectInput } />
From 79c0cae8b9ae9c9ad6b2ac5f0946fb13e89ba171 Mon Sep 17 00:00:00 2001 From: Naerriel Date: Thu, 31 Jan 2019 16:59:21 +0100 Subject: [PATCH 4/4] Decode URL encoding slug in sidebar permalink pt.4. --- .../src/components/sidebar/post-link/index.js | 3 ++- .../src/components/post-permalink/index.js | 4 ++-- .../components/post-publish-panel/postpublish.js | 3 ++- packages/url/src/index.js | 16 ++++++++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/packages/edit-post/src/components/sidebar/post-link/index.js b/packages/edit-post/src/components/sidebar/post-link/index.js index 681dc1f9ae426c..0c24595b09803c 100644 --- a/packages/edit-post/src/components/sidebar/post-link/index.js +++ b/packages/edit-post/src/components/sidebar/post-link/index.js @@ -12,6 +12,7 @@ import { PanelBody, TextControl, ExternalLink } from '@wordpress/components'; import { withSelect, withDispatch } from '@wordpress/data'; import { compose, ifCondition, withState } from '@wordpress/compose'; import { cleanForSlug } from '@wordpress/editor'; +import { safeDecodeURIComponent } from '@wordpress/url'; /** * Module Constants @@ -33,7 +34,7 @@ function PostLink( { } ) { const { prefix, suffix } = permalinkParts; let prefixElement, postNameElement, suffixElement; - const currentSlug = decodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; + const currentSlug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; if ( isEditable ) { prefixElement = prefix && ( { prefix } diff --git a/packages/editor/src/components/post-permalink/index.js b/packages/editor/src/components/post-permalink/index.js index 7054b108318550..6c65947d4b87f5 100644 --- a/packages/editor/src/components/post-permalink/index.js +++ b/packages/editor/src/components/post-permalink/index.js @@ -12,7 +12,7 @@ import { Component } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { compose } from '@wordpress/compose'; import { ClipboardButton, Button, ExternalLink } from '@wordpress/components'; -import { safeDecodeURI } from '@wordpress/url'; +import { safeDecodeURI, safeDecodeURIComponent } from '@wordpress/url'; /** * Internal Dependencies @@ -78,7 +78,7 @@ class PostPermalink extends Component { const ariaLabel = isCopied ? __( 'Permalink copied' ) : __( 'Copy the permalink' ); const { prefix, suffix } = permalinkParts; - const slug = decodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; + const slug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID; const samplePermalink = ( isEditable ) ? prefix + slug + suffix : prefix; return ( diff --git a/packages/editor/src/components/post-publish-panel/postpublish.js b/packages/editor/src/components/post-publish-panel/postpublish.js index 6fee634dbff710..908ca456d2377a 100644 --- a/packages/editor/src/components/post-publish-panel/postpublish.js +++ b/packages/editor/src/components/post-publish-panel/postpublish.js @@ -10,6 +10,7 @@ import { PanelBody, Button, ClipboardButton, TextControl } from '@wordpress/comp import { __, sprintf } from '@wordpress/i18n'; import { Component, Fragment, createRef } from '@wordpress/element'; import { withSelect } from '@wordpress/data'; +import { safeDecodeURIComponent } from '@wordpress/url'; /** * Internal dependencies @@ -79,7 +80,7 @@ class PostPublishPanelPostpublish extends Component { /* translators: %s: post type singular name */ __( '%s address' ), postLabel ) } - value={ decodeURIComponent( post.link ) } + value={ safeDecodeURIComponent( post.link ) } onFocus={ this.onSelectInput } />
diff --git a/packages/url/src/index.js b/packages/url/src/index.js index da3077640e243f..b5714a22b612bf 100644 --- a/packages/url/src/index.js +++ b/packages/url/src/index.js @@ -287,3 +287,19 @@ export function filterURLForDisplay( url ) { return filteredURL; } + +/** + * Safely decodes a URI component with `decodeURIComponent`. Returns the URI component unmodified if + * `decodeURIComponent` throws an error. + * + * @param {string} uriComponent URI component to decode. + * + * @return {string} Decoded URI component if possible. + */ +export function safeDecodeURIComponent( uriComponent ) { + try { + return decodeURIComponent( uriComponent ); + } catch ( uriComponentError ) { + return uriComponent; + } +}