Révision 2b3c8cc1
Ajouté par Assos Assos il y a presque 9 ans
drupal7/sites/all/modules/media/modules/media_wysiwyg/js/media_wysiwyg.filter.js | ||
---|---|---|
46 | 46 |
var source = Drupal.settings.mediaSourceMap[media_definition.fid]; |
47 | 47 |
media = document.createElement(source.tagName); |
48 | 48 |
media.src = source.src; |
49 |
media.innerHTML = source.innerHTML; |
|
49 | 50 |
} |
50 | 51 |
|
51 | 52 |
// Apply attributes. |
... | ... | |
72 | 73 |
// Rewrite the tagmap in case any of the macros have changed. |
73 | 74 |
Drupal.settings.tagmap = {}; |
74 | 75 |
|
75 |
// Wrap the content to be able to use replaceWith() and html(). |
|
76 |
content = $('<div>').append(content); |
|
77 |
var media = $('.media-element', content); |
|
78 |
|
|
79 |
if (media.length) { |
|
80 |
// Replace all media elements with their respective macros. |
|
81 |
media.replaceWith(function() { |
|
82 |
var el = $(this), |
|
83 |
macro = Drupal.media.filter.create_macro(el); |
|
84 |
|
|
85 |
// Store the markup for more efficient rendering later. |
|
86 |
// @see replaceTokenWidthPlaceholder() |
|
87 |
Drupal.settings.tagmap[macro] = Drupal.media.filter.outerHTML(el); |
|
88 |
|
|
89 |
return macro; |
|
90 |
}); |
|
76 |
// Replace all media placeholders with their JSON macro representations. |
|
77 |
// |
|
78 |
// There are issues with using jQuery to parse the WYSIWYG content (see |
|
79 |
// http://drupal.org/node/1280758), and parsing HTML with regular |
|
80 |
// expressions is a terrible idea (see http://stackoverflow.com/a/1732454/854985) |
|
81 |
// |
|
82 |
// WYSIWYG editors act wacky with complex placeholder markup anyway, so an |
|
83 |
// image is the most reliable and most usable anyway: images can be moved by |
|
84 |
// dragging and dropping, and can be resized using interactive handles. |
|
85 |
// |
|
86 |
// Media requests a WYSIWYG place holder rendering of the file by passing |
|
87 |
// the wysiwyg => 1 flag in the settings array when calling |
|
88 |
// media_get_file_without_label(). |
|
89 |
// |
|
90 |
// Finds the media-element class. |
|
91 |
var classRegex = 'class=[\'"][^\'"]*?media-element'; |
|
92 |
// Image tag with the media-element class. |
|
93 |
var regex = '<img[^>]+' + classRegex + '[^>]*?>'; |
|
94 |
// Or a span with the media-element class (used for documents). |
|
95 |
// \S\s catches any character, including a linebreak; JavaScript does not |
|
96 |
// have a dotall flag. |
|
97 |
regex += '|<span[^>]+' + classRegex + '[^>]*?>[\\S\\s]+?</span>'; |
|
98 |
var matches = content.match(RegExp(regex, 'gi')); |
|
99 |
if (matches) { |
|
100 |
for (i = 0; i < matches.length; i++) { |
|
101 |
markup = matches[i]; |
|
102 |
macro = Drupal.media.filter.create_macro($(markup)); |
|
103 |
Drupal.settings.tagmap[macro] = markup; |
|
104 |
content = content.replace(markup, macro); |
|
105 |
} |
|
91 | 106 |
} |
92 | 107 |
|
93 |
return content.html();
|
|
108 |
return content; |
|
94 | 109 |
}, |
95 | 110 |
|
96 | 111 |
/** |
... | ... | |
129 | 144 |
Drupal.media.filter.ensureSourceMap(); |
130 | 145 |
Drupal.settings.mediaSourceMap[info.fid] = { |
131 | 146 |
tagName: element[0].tagName, |
132 |
src: element[0].src |
|
147 |
src: element[0].src, |
|
148 |
innerHTML: element[0].innerHTML |
|
133 | 149 |
} |
134 | 150 |
} |
135 | 151 |
|
... | ... | |
142 | 158 |
// Store the fid in the DOM to retrieve the data from the info map. |
143 | 159 |
element.attr('data-fid', info.fid); |
144 | 160 |
|
145 |
// Add media-element class so we can find markup element later.
|
|
146 |
var classes = ['media-element'];
|
|
161 |
// Add data-media-element attribute so we can find the markup element later.
|
|
162 |
element.attr('data-media-element', '1')
|
|
147 | 163 |
|
164 |
var classes = ['media-element']; |
|
148 | 165 |
if (info.view_mode) { |
149 | 166 |
classes.push('file-' + info.view_mode.replace(/_/g, '-')); |
150 | 167 |
} |
151 | 168 |
element.addClass(classes.join(' ')); |
152 | 169 |
|
170 |
// Apply link_text if present. |
|
171 |
if (info.link_text) { |
|
172 |
$('a', element).html(info.link_text); |
|
173 |
} |
|
174 |
|
|
153 | 175 |
return element; |
154 | 176 |
}, |
155 | 177 |
|
... | ... | |
162 | 184 |
create_macro: function (element) { |
163 | 185 |
var file_info = Drupal.media.filter.extract_file_info(element); |
164 | 186 |
if (file_info) { |
187 |
if (typeof file_info.link_text == 'string') { |
|
188 |
// Make sure the link_text-html-tags are properly escaped. |
|
189 |
file_info.link_text = file_info.link_text.replace(/</g, '<').replace(/>/g, '>'); |
|
190 |
} |
|
165 | 191 |
return '[[' + JSON.stringify(file_info) + ']]'; |
166 | 192 |
} |
167 | 193 |
return false; |
Formats disponibles : Unified diff
Weekly update of contrib modules