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