wp/wp-includes/js/shortcode.js
author ymh <ymh.work@gmail.com>
Mon, 14 Oct 2019 18:28:13 +0200
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
permissions -rw-r--r--
upgrade wordpress to 5.2.3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     1
/**
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     2
 * Utility functions for parsing and handling shortcodes in JavaScript.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     3
 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     4
 * @output wp-includes/js/shortcode.js
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
     5
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     7
/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     8
 * Ensure the global `wp` object exists.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
     9
 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    10
 * @namespace wp
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    11
 */
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
window.wp = window.wp || {};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
(function(){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	wp.shortcode = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
		// ### Find the next matching shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
		// Given a shortcode `tag`, a block of `text`, and an optional starting
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
		// `index`, returns the next matching shortcode or `undefined`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
		// Shortcodes are formatted as an object that contains the match
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		// `content`, the matching `index`, and the parsed `shortcode` object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
		next: function( tag, text, index ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
			var re = wp.shortcode.regexp( tag ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
				match, result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
			re.lastIndex = index || 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
			match = re.exec( text );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    30
			if ( ! match ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
				return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    32
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
			// If we matched an escaped shortcode, try again.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    35
			if ( '[' === match[1] && ']' === match[7] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
				return wp.shortcode.next( tag, text, re.lastIndex );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
			result = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
				index:     match.index,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
				content:   match[0],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
				shortcode: wp.shortcode.fromMatch( match )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
			// If we matched a leading `[`, strip it from the match
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
			// and increment the index accordingly.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
			if ( match[1] ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    48
				result.content = result.content.slice( 1 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
				result.index++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			// If we matched a trailing `]`, strip it from the match.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
			if ( match[7] ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
				result.content = result.content.slice( 0, -1 );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
			return result;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
		// ### Replace matching shortcodes in a block of text
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		// Accepts a shortcode `tag`, content `text` to scan, and a `callback`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
		// to process the shortcode matches and return a replacement string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		// Returns the `text` with all shortcodes replaced.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		// Shortcode matches are objects that contain the shortcode `tag`,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		// a shortcode `attrs` object, the `content` between shortcode tags,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
		// and a boolean flag to indicate if the match was a `single` tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		replace: function( tag, text, callback ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
			return text.replace( wp.shortcode.regexp( tag ), function( match, left, tag, attrs, slash, content, closing, right ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
				// If both extra brackets exist, the shortcode has been
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
				// properly escaped.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
				if ( left === '[' && right === ']' ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
					return match;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    75
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
				// Create the match object and pass it through the callback.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
				var result = callback( wp.shortcode.fromMatch( arguments ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
				// Make sure to return any of the extra brackets if they
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
				// weren't used to escape the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
				return result ? left + result + right : match;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		// ### Generate a string from shortcode parameters
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
		// Creates a `wp.shortcode` instance and returns a string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
		// Accepts the same `options` as the `wp.shortcode()` constructor,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
		// containing a `tag` string, a string or object of `attrs`, a boolean
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		// indicating whether to format the shortcode using a `single` tag, and a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		// `content` string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		string: function( options ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			return new wp.shortcode( options ).string();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		// ### Generate a RegExp to identify a shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		// The base regex is functionally equivalent to the one found in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		// `get_shortcode_regex()` in `wp-includes/shortcodes.php`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
		// Capture groups:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		// 1. An extra `[` to allow for escaping shortcodes with double `[[]]`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		// 2. The shortcode name
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		// 3. The shortcode argument list
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		// 4. The self closing `/`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
		// 5. The content of a shortcode when it wraps some content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		// 6. The closing tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		// 7. An extra `]` to allow for escaping shortcodes with double `[[]]`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		regexp: _.memoize( function( tag ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
			return new RegExp( '\\[(\\[?)(' + tag + ')(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*(?:\\[(?!\\/\\2\\])[^\\[]*)*)(\\[\\/\\2\\]))?)(\\]?)', 'g' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		}),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
		// ### Parse shortcode attributes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
		// Shortcodes accept many types of attributes. These can chiefly be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		// divided into named and numeric attributes:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		// Named attributes are assigned on a key/value basis, while numeric
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		// attributes are treated as an array.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		// Named attributes can be formatted as either `name="value"`,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
		// `name='value'`, or `name=value`. Numeric attributes can be formatted
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
		// as `"value"` or just `value`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
		attrs: _.memoize( function( text ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
			var named   = {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
				numeric = [],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
				pattern, match;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
			// This regular expression is reused from `shortcode_parse_atts()`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			// in `wp-includes/shortcodes.php`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
			//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
			// Capture groups:
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
			//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
			// 1. An attribute name, that corresponds to...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
			// 2. a value in double quotes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
			// 3. An attribute name, that corresponds to...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			// 4. a value in single quotes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			// 5. An attribute name, that corresponds to...
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
			// 6. an unquoted value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			// 7. A numeric attribute in double quotes.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   145
			// 8. A numeric attribute in single quotes.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   146
			// 9. An unquoted numeric attribute.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   147
			pattern = /([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*'([^']*)'(?:\s|$)|([\w-]+)\s*=\s*([^\s'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|'([^']*)'(?:\s|$)|(\S+)(?:\s|$)/g;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
			// Map zero-width spaces to actual spaces.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
			text = text.replace( /[\u00a0\u200b]/g, ' ' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
			// Match and normalize attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
			while ( (match = pattern.exec( text )) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
				if ( match[1] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
					named[ match[1].toLowerCase() ] = match[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
				} else if ( match[3] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
					named[ match[3].toLowerCase() ] = match[4];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
				} else if ( match[5] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
					named[ match[5].toLowerCase() ] = match[6];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
				} else if ( match[7] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
					numeric.push( match[7] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
				} else if ( match[8] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
					numeric.push( match[8] );
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   164
				} else if ( match[9] ) {
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   165
					numeric.push( match[9] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
			return {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
				named:   named,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
				numeric: numeric
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		}),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		// ### Generate a Shortcode Object from a RegExp match
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
		// Accepts a `match` object from calling `regexp.exec()` on a `RegExp`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		// generated by `wp.shortcode.regexp()`. `match` can also be set to the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
		// `arguments` from a callback passed to `regexp.replace()`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
		fromMatch: function( match ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
			var type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
			if ( match[4] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
				type = 'self-closing';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   184
			} else if ( match[6] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
				type = 'closed';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   186
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
				type = 'single';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   188
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
			return new wp.shortcode({
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
				tag:     match[2],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
				attrs:   match[3],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
				type:    type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
				content: match[5]
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
	};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
	// Shortcode Objects
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
	// -----------------
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
	//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
	// Shortcode objects are generated automatically when using the main
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
	// `wp.shortcode` methods: `next()`, `replace()`, and `string()`.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
	//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
	// To access a raw representation of a shortcode, pass an `options` object,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
	// containing a `tag` string, a string or object of `attrs`, a string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
	// indicating the `type` of the shortcode ('single', 'self-closing', or
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
	// 'closed'), and a `content` string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
	wp.shortcode = _.extend( function( options ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		_.extend( this, _.pick( options || {}, 'tag', 'attrs', 'type', 'content' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		var attrs = this.attrs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		// Ensure we have a correctly formatted `attrs` object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		this.attrs = {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
			named:   {},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
			numeric: []
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
		};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   221
		if ( ! attrs ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   223
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		// Parse a string of attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		if ( _.isString( attrs ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
			this.attrs = wp.shortcode.attrs( attrs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
		// Identify a correctly formatted `attrs` object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
		} else if ( _.isEqual( _.keys( attrs ), [ 'named', 'numeric' ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			this.attrs = attrs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
		// Handle a flat object of attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			_.each( options.attrs, function( value, key ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
				this.set( key, value );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
			}, this );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
	}, wp.shortcode );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
	_.extend( wp.shortcode.prototype, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
		// ### Get a shortcode attribute
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
		// Automatically detects whether `attr` is named or numeric and routes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
		// it accordingly.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
		get: function( attr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
			return this.attrs[ _.isNumber( attr ) ? 'numeric' : 'named' ][ attr ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		// ### Set a shortcode attribute
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		// Automatically detects whether `attr` is named or numeric and routes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		// it accordingly.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
		set: function( attr, value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
			this.attrs[ _.isNumber( attr ) ? 'numeric' : 'named' ][ attr ] = value;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
			return this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
		// ### Transform the shortcode match into a string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
		string: function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
			var text    = '[' + this.tag;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
			_.each( this.attrs.numeric, function( value ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   264
				if ( /\s/.test( value ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
					text += ' "' + value + '"';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   266
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
					text += ' ' + value;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   268
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
			_.each( this.attrs.named, function( value, name ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
				text += ' ' + name + '="' + value + '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
			// If the tag is marked as `single` or `self-closing`, close the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
			// tag and ignore any additional content.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   277
			if ( 'single' === this.type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
				return text + ']';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   279
			} else if ( 'self-closing' === this.type ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
				return text + ' /]';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   281
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
			// Complete the opening tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
			text += ']';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   286
			if ( this.content ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
				text += this.content;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   288
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
			// Add the closing tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
			return text + '[/' + this.tag + ']';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
}());
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
// HTML utility functions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
// ----------------------
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
// Experimental. These functions may change or be removed in the future.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
(function(){
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
	wp.html = _.extend( wp.html || {}, {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
		// ### Parse HTML attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
		//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
		// Converts `content` to a set of parsed HTML attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
		// Utilizes `wp.shortcode.attrs( content )`, which is a valid superset of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
		// the HTML attribute specification. Reformats the attributes into an
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		// object that contains the `attrs` with `key:value` mapping, and a record
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
		// of the attributes that were entered using `empty` attribute syntax (i.e.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
		// with no value).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
		attrs: function( content ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
			var result, attrs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
			// If `content` ends in a slash, strip it.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   314
			if ( '/' === content[ content.length - 1 ] ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
				content = content.slice( 0, -1 );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   316
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
			result = wp.shortcode.attrs( content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
			attrs  = result.named;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			_.each( result.numeric, function( key ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   322
				if ( /\s/.test( key ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
					return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   324
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
				attrs[ key ] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
			return attrs;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
		// ### Convert an HTML-representation of an object to a string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
		string: function( options ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
			var text = '<' + options.tag,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
				content = options.content || '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
			_.each( options.attrs, function( value, attr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
				text += ' ' + attr;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
				// Convert boolean values to strings.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   341
				if ( _.isBoolean( value ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
					value = value ? 'true' : 'false';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   343
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
				text += '="' + value + '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
			// Return the result if it is a self-closing tag.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   349
			if ( options.single ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
				return text + ' />';
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   351
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
			// Complete the opening tag.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
			text += '>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			// If `content` is an object, recursively call this function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
			text += _.isObject( content ) ? wp.html.string( content ) : content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
			return text + '</' + options.tag + '>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
	});
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   362
}());