Skip to content

Commit

Permalink
Decode permalinks with URL encoding (#13551)
Browse files Browse the repository at this point in the history
* Decode URL encoding slug in sidebar permalink

* Decode URL encoding slug in sidebar permalink pt.2.

* Decode URL encoding slug in sidebar permalink pt.3.

* Decode URL encoding slug in sidebar permalink pt.4.
  • Loading branch information
Naerriel authored and youknowriad committed Mar 6, 2019
1 parent 9789e4f commit 8a1fbe9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/edit-post/src/components/sidebar/post-link/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -33,7 +34,7 @@ function PostLink( {
} ) {
const { prefix, suffix } = permalinkParts;
let prefixElement, postNameElement, suffixElement;
const currentSlug = postSlug || cleanForSlug( postTitle ) || postID;
const currentSlug = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID;
if ( isEditable ) {
prefixElement = prefix && (
<span className="edit-post-post-link__link-prefix">{ prefix }</span>
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/components/post-permalink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = safeDecodeURIComponent( postSlug ) || cleanForSlug( postTitle ) || postID;
const samplePermalink = ( isEditable ) ? prefix + slug + suffix : prefix;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,7 +80,7 @@ class PostPublishPanelPostpublish extends Component {
/* translators: %s: post type singular name */
__( '%s address' ), postLabel
) }
value={ post.link }
value={ safeDecodeURIComponent( post.link ) }
onFocus={ this.onSelectInput }
/>
<div className="post-publish-panel__postpublish-buttons">
Expand Down
16 changes: 16 additions & 0 deletions packages/url/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 8a1fbe9

Please sign in to comment.