/* * shortcode.js 1.1.0 * by @nicinabox * License: MIT * Issues: https://github.com/nicinabox/shortcode.js/issues */ /* jshint strict: false, unused: false */ var Shortcode = function(el, tags) { if (!el) { return; } this.el = el; this.tags = tags; this.matches = []; this.regex = '\\[{name}(\\s[\\s\\S]*?)?\\]' + '(?:((?!\\s*?(?:\\[{name}|\\[\\/(?!{name})))[\\s\\S]*?)' + '(\\[\/{name}\\]))?'; if (this.el.jquery) { this.el = this.el[0]; } this.matchTags(); this.convertMatchesToNodes(); this.replaceNodes(); }; Shortcode.prototype.matchTags = function() { var html = this.el.outerHTML, instances, match, re, contents, regex, tag, options; for (var key in this.tags) { if (!this.tags.hasOwnProperty(key)) { return; } re = this.template(this.regex, { name: key }); instances = html.match(new RegExp(re, 'g')) || []; for (var i = 0, len = instances.length; i < len; i++) { match = instances[i].match(new RegExp(re)); contents = match[3] ? '' : undefined; tag = match[0]; regex = this.escapeTagRegExp(tag); options = this.parseOptions(match[1]); if (match[2]) { contents = match[2].trim(); tag = tag.replace(contents, '').replace(/\n\s*/g, ''); regex = this.escapeTagRegExp(tag).replace('\\]\\[', '\\]([\\s\\S]*?)\\['); } this.matches.push({ name: key, tag: tag, regex: regex, options: options, contents: contents }); } } }; Shortcode.prototype.convertMatchesToNodes = function() { var html = this.el.innerHTML, excludes, re, replacer; replacer = function(match, p1, p2, p3, p4, offset, string) { if (p1) { return match; } else { var node = document.createElement('span'); node.setAttribute('data-sc-tag', this.tag); node.className = 'sc-node sc-node-' + this.name; return node.outerHTML; } }; for (var i = 0, len = this.matches.length; i < len; i++) { excludes = '((data-sc-tag=")|(