PluginProbe
TablePress – Tables in WordPress made easy / 3.1
TablePress – Tables in WordPress made easy v3.1
3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 2.4.4 All 44 releases
tablepress / blocks / table / src / common / functions.js

functions.js in TablePress – Tables in WordPress made easy 3.1, at blocks/table/src/common/functions.js

51 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Common functions for the TablePress/table block.
3 *
4 * @package TablePress
5 * @subpackage Blocks
6 * @author Tobias Bäthge
7 * @since 2.0.0
8 */
9
10 /**
11 * Converts a set of named and numeric Shortcode attributes to a string.
12 *
13 * This function is similar to @wordpress/shortcode's `string()` function,
14 * but only returns the attributes string and not a full Shortcode.
15 *
16 * @param {Object} shortcodeAttrs The named and numeric Shortcode attributes.
17 * @return {string} The attributes as a key=value string.
18 */
19 export const shortcodeAttrsToString = ( shortcodeAttrs ) => {
20 // Convert named attributes.
21 let shortcodeAttrsString = Object.entries( shortcodeAttrs.named ).map( ( [ attribute, value ] ) => {
22 let enclose = ''; // Don't enclose values by default.
23
24 // Remove curly quotation marks around a value.
25 value = value.replace( /([^]*)/g, '$1' );
26
27 // Use " as delimiter if value contains whitespace or is empty.
28 if ( /\s/.test( value ) || '' === value ) {
29 enclose = '"';
30 }
31
32 // Use ' as delimiter if value contains ".
33 if ( value.includes( '"' ) ) {
34 enclose = '\'';
35 }
36
37 return `${ attribute }=${ enclose }${ value }${ enclose }`;
38 } ).join( ' ' );
39
40 // Convert numeric attributes.
41 shortcodeAttrs.numeric.forEach( ( value ) => {
42 if ( /\s/.test( value ) ) {
43 shortcodeAttrsString += ' "' + value + '"';
44 } else {
45 shortcodeAttrsString += ' ' + value;
46 }
47 } );
48
49 return shortcodeAttrsString;
50 };
51