diff --git a/js/media.filter.js b/js/media.filter.js index a48d258..74c013c 100644 --- a/js/media.filter.js +++ b/js/media.filter.js @@ -41,8 +41,8 @@ } // Apply attributes. - var element = Drupal.media.filter.create_element(media, media_definition); - var markup = Drupal.media.filter.outerHTML(element); + var element = Drupal.media.filter.createElement(media, media_definition); + var markup = Drupal.media.filter.safeMarkup(element); content = content.replace(match, markup); } @@ -62,15 +62,16 @@ content = content.replace(/[\s]\/\>/g, '>'); // Re-build the macros in case any element has changed in the editor. - $('.media-element', content).each(function(i, el) { - var macro = Drupal.media.filter.create_macro($(el)); + $('.media-element', content).each(function(i, element) { + var markup = Drupal.media.filter.safeMarkup($(element)); + macro = Drupal.media.filter.createMacro($(element)); // Store the macro => html for more efficient rendering in // replaceTokenWithPlaceholder(). - Drupal.settings.tagmap[macro] = el.outerHTML; + Drupal.settings.tagmap[macro] = markup; // Replace the media element with its macro. - content = content.replace(el.outerHTML, macro); + content = content.replace(markup, macro); }); return content; @@ -86,32 +87,53 @@ * @return The registered element. */ registerNewElement: function (formattedMedia, fid) { - var element = Drupal.media.filter.create_element(formattedMedia.html, { + var element = Drupal.media.filter.createElement(formattedMedia.html, { fid: fid, view_mode: formattedMedia.type, attributes: formattedMedia.options }); - var markup = Drupal.media.filter.outerHTML(element), - macro = Drupal.media.filter.create_macro(element); + var markup = Drupal.media.filter.safeMarkup(element), + macro = Drupal.media.filter.createMacro(element); // Store macro/markup pair in the tagmap. Drupal.media.filter.ensure_tagmap(); Drupal.settings.tagmap[macro] = markup; - return element; + return markup; + }, + + /** + * Returns alt and title field values for use as html attributes. + * + * @param options (array) Options passed through a popup form submission. + */ + parseAttributeFields: function(options) { + var attributes = []; + + for (field in options) { + if (field.match('image_alt')) { + attributes['alt'] = options[field]; + } + + if (field.match('image_title')) { + attributes['title'] = options[field]; + } + } + + return attributes; }, /** - * Serializes file information as a url-encoded JSON object and stores it as a - * data attribute on the html element. + * Serializes file information as a url-encoded JSON object and stores it + * as a data attribute on the html element. * * @param html (string) * A html element to be used to represent the inserted media element. * @param info (object) * A object containing the media file information (fid, view_mode, etc). */ - create_element: function (html, info) { + createElement: function (html, info) { if ($('
').append(html).text().length === html.length) { // Element is not an html tag. Surround it in a span element // so we can pass the file attributes. @@ -137,7 +159,7 @@ // Adding media-element class so we can find markup element later. var classes = ['media-element']; - if(info.view_mode){ + if (info.view_mode) { classes.push('file-' + info.view_mode.replace(/_/g, '-')); } element.addClass(classes.join(' ')); @@ -151,8 +173,8 @@ * @param element (jQuery object) * A media element with attached serialized file info. */ - create_macro: function (element) { - var file_info = Drupal.media.filter.extract_file_info(element); + createMacro: function (element) { + var file_info = Drupal.media.filter.extractFileInfo(element); if (file_info) { return '[[' + JSON.stringify(file_info) + ']]'; } @@ -165,7 +187,7 @@ * @param element (jQuery object) * A media element with attached serialized file info. */ - extract_file_info: function (element) { + extractFileInfo: function (element) { var file_json = $.data(element, 'file_info') || element.data('file_info'), file_info, value; @@ -202,7 +224,28 @@ * @param element (jQuery object) */ outerHTML: function (element) { - return $('