-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix escaping issues in editor and rendered block
- Loading branch information
Showing
5 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,12 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { escape } from './utils'; | ||
|
||
export default function save( { attributes } ) { | ||
return ( | ||
<pre> | ||
<code>{ escape( attributes.content ) }</code> | ||
</pre> | ||
); | ||
} |
This file contains 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,61 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { flow } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { escapeEditableHTML } from '@wordpress/escape-html'; | ||
|
||
/** | ||
* Escapes ampersands, shortcodes, and links. | ||
* | ||
* @param {string} content The content of a code block. | ||
* @return {string} The given content with some characters escaped. | ||
*/ | ||
export function escape( content ) { | ||
return flow( | ||
escapeEditableHTML, | ||
escapeOpeningSquareBrackets, | ||
escapeProtocolInIsolatedUrls | ||
)( content || '' ); | ||
} | ||
|
||
/** | ||
* Returns the given content with all opening shortcode characters converted | ||
* into their HTML entity counterpart (i.e. [ => [). For instance, a | ||
* shortcode like [embed] becomes [embed] | ||
* | ||
* This function replicates the escaping of HTML tags, where a tag like | ||
* <strong> becomes <strong>. | ||
* | ||
* @param {string} content The content of a code block. | ||
* @return {string} The given content with its opening shortcode characters | ||
* converted into their HTML entity counterpart | ||
* (i.e. [ => [) | ||
*/ | ||
function escapeOpeningSquareBrackets( content ) { | ||
return content.replace( /\[/g, '[' ); | ||
} | ||
|
||
/** | ||
* Converts the first two forward slashes of any isolated URL into their HTML | ||
* counterparts (i.e. // => //). For instance, https://youtube.com/watch?x | ||
* becomes https://youtube.com/watch?x. | ||
* | ||
* An isolated URL is a URL that sits in its own line, surrounded only by spacing | ||
* characters. | ||
* | ||
* See https://github.com/WordPress/wordpress-develop/blob/5.1.1/src/wp-includes/class-wp-embed.php#L403 | ||
* | ||
* @param {string} content The content of a code block. | ||
* @return {string} The given content with its ampersands converted into | ||
* their HTML entity counterpart (i.e. & => &) | ||
*/ | ||
function escapeProtocolInIsolatedUrls( content ) { | ||
return content.replace( | ||
/^(\s*https?:)\/\/([^\s<>"]+\s*)$/m, | ||
'$1//$2' | ||
); | ||
} |
This file contains 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