| @@ -22,23 +22,42 @@ | ||
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Internal dependencies. |
| 25 | 25 | */ |
| 26 | -import { shortcode_attrs_to_string } from './common/functions'; | |
| 26 | +import { shortcodeAttrsToString } from './common/functions'; | |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | + * Converts a set of parsed Shortcode attributes to a parameters string. | |
| 30 | + * | |
| 31 | + * @param {Object} shortcodeAttrs The Shortcode attributes. | |
| 32 | + * @return {string} The parameters string. | |
| 33 | + */ | |
| 34 | +const convertShortcodeAttrsToParametersString = ( shortcodeAttrs ) => { | |
| 35 | + // Remove the `id` attribute from the Shortcode parameters, as it is handled separately. | |
| 36 | + delete shortcodeAttrs.named.id; | |
| 37 | + | |
| 38 | + let parameters = shortcodeAttrsToString( shortcodeAttrs ); | |
| 39 | + | |
| 40 | + // Replace curly quotation marks around a value with normal ones. | |
| 41 | + parameters = parameters.replace( /=“([^”]*)”/g, '="$1"' ); | |
| 42 | + | |
| 43 | + // Decode HTML entities like `&`, `<`, `>`, `[`, and `]` that were encoded in the Shortcode. | |
| 44 | + parameters = parameters.replaceAll( '&', '&' ).replaceAll( '<', '<' ).replaceAll( '>', '>' ).replaceAll( '[', '[' ).replaceAll( ']', ']' ); | |
| 45 | + | |
| 46 | + return parameters; | |
| 47 | +}; | |
| 48 | + | |
| 49 | +/** | |
| 29 | 50 | * Converts a textual Shortcode to a TablePress table block. |
| 30 | 51 | * |
| 31 | 52 | * @param {string} content The Shortcode as a text string. |
| 32 | 53 | * @return {Object} TablePress table block. |
| 33 | 54 | */ |
| 34 | -const convertShortcodeTextToBlock = function( content ) { | |
| 55 | +const convertShortcodeTextToBlock = ( content ) => { | |
| 35 | 56 | let shortcodeAttrs = shortcode.next( tp.table.shortcode, content ).shortcode.attrs; |
| 36 | 57 | shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object. |
| 37 | 58 | const id = shortcodeAttrs.named.id; |
| 38 | - delete shortcodeAttrs.named.id; | |
| 39 | - let parameters = shortcode_attrs_to_string( shortcodeAttrs ); | |
| 40 | - parameters = parameters.replace( /=“([^”]*)”/g, '="$1"' ); // Replace curly quotation marks around a value with normal ones. | |
| 59 | + const parameters = convertShortcodeAttrsToParametersString( shortcodeAttrs ); | |
| 41 | 60 | return createBlock( block.name, { id, parameters } ); |
| 42 | 61 | }; |
| 43 | 62 | |
| 44 | 63 | const transforms = { |
| @@ -57,10 +76,9 @@ | ||
| 57 | 76 | parameters: { |
| 58 | 77 | type: 'string', |
| 59 | 78 | shortcode: ( shortcodeAttrs ) => { |
| 60 | 79 | shortcodeAttrs = { named: { ...shortcodeAttrs.named }, numeric: [ ...shortcodeAttrs.numeric ] }; // Use object destructuring to get a clone of the object. |
| 61 | - delete shortcodeAttrs.named.id; | |
| 62 | - return shortcode_attrs_to_string( shortcodeAttrs ); | |
| 80 | + return convertShortcodeAttrsToParametersString( shortcodeAttrs ); | |
| 63 | 81 | }, |
| 64 | 82 | }, |
| 65 | 83 | }, |
| 66 | 84 | }, |
| @@ -92,8 +110,10 @@ | ||
| 92 | 110 | transform: ( { id, parameters } ) => { |
| 93 | 111 | parameters = parameters.trim(); |
| 94 | 112 | if ( '' !== parameters ) { |
| 95 | 113 | parameters += ' '; |
| 114 | + // Encode characters that cause problems in Shortcodes, e.g. due to sanitization, like `&`, `<`, `>`, `[`, and `]` to HTML entities. | |
| 115 | + parameters = parameters.replaceAll( '&', '&' ).replaceAll( '<', '<' ).replaceAll( '>', '>' ).replaceAll( '[', '[' ).replaceAll( ']', ']' ); | |
| 96 | 116 | } |
| 97 | 117 | const text = `[${ tp.table.shortcode } id=${ id } ${ parameters }/]`; |
| 98 | 118 | return createBlock( 'core/shortcode', { text } ); |
| 99 | 119 | }, |