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