wp/wp-includes/js/shortcode.js
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     1 // Utility functions for parsing and handling shortcodes in JavaScript.
     1 // Utility functions for parsing and handling shortcodes in JavaScript.
     2 
     2 
     3 // Ensure the global `wp` object exists.
     3 /**
       
     4  * Ensure the global `wp` object exists.
       
     5  *
       
     6  * @namespace wp
       
     7  */
     4 window.wp = window.wp || {};
     8 window.wp = window.wp || {};
     5 
     9 
     6 (function(){
    10 (function(){
     7 	wp.shortcode = {
    11 	wp.shortcode = {
     8 		// ### Find the next matching shortcode
    12 		// ### Find the next matching shortcode
   132 			// 3. An attribute name, that corresponds to...
   136 			// 3. An attribute name, that corresponds to...
   133 			// 4. a value in single quotes.
   137 			// 4. a value in single quotes.
   134 			// 5. An attribute name, that corresponds to...
   138 			// 5. An attribute name, that corresponds to...
   135 			// 6. an unquoted value.
   139 			// 6. an unquoted value.
   136 			// 7. A numeric attribute in double quotes.
   140 			// 7. A numeric attribute in double quotes.
   137 			// 8. An unquoted numeric attribute.
   141 			// 8. A numeric attribute in single quotes.
   138 			pattern = /(\w+)\s*=\s*"([^"]*)"(?:\s|$)|(\w+)\s*=\s*\'([^\']*)\'(?:\s|$)|(\w+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/g;
   142 			// 9. An unquoted numeric attribute.
       
   143 			pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|'([^']*)'(?:\s|$)|(\S+)(?:\s|$)/g;
   139 
   144 
   140 			// Map zero-width spaces to actual spaces.
   145 			// Map zero-width spaces to actual spaces.
   141 			text = text.replace( /[\u00a0\u200b]/g, ' ' );
   146 			text = text.replace( /[\u00a0\u200b]/g, ' ' );
   142 
   147 
   143 			// Match and normalize attributes.
   148 			// Match and normalize attributes.
   150 					named[ match[5].toLowerCase() ] = match[6];
   155 					named[ match[5].toLowerCase() ] = match[6];
   151 				} else if ( match[7] ) {
   156 				} else if ( match[7] ) {
   152 					numeric.push( match[7] );
   157 					numeric.push( match[7] );
   153 				} else if ( match[8] ) {
   158 				} else if ( match[8] ) {
   154 					numeric.push( match[8] );
   159 					numeric.push( match[8] );
       
   160 				} else if ( match[9] ) {
       
   161 					numeric.push( match[9] );
   155 				}
   162 				}
   156 			}
   163 			}
   157 
   164 
   158 			return {
   165 			return {
   159 				named:   named,
   166 				named:   named,
   324 				content = options.content || '';
   331 				content = options.content || '';
   325 
   332 
   326 			_.each( options.attrs, function( value, attr ) {
   333 			_.each( options.attrs, function( value, attr ) {
   327 				text += ' ' + attr;
   334 				text += ' ' + attr;
   328 
   335 
   329 				// Use empty attribute notation where possible.
       
   330 				if ( '' === value ) {
       
   331 					return;
       
   332 				}
       
   333 
       
   334 				// Convert boolean values to strings.
   336 				// Convert boolean values to strings.
   335 				if ( _.isBoolean( value ) ) {
   337 				if ( _.isBoolean( value ) ) {
   336 					value = value ? 'true' : 'false';
   338 					value = value ? 'true' : 'false';
   337 				}
   339 				}
   338 
   340