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