@@ -42,6 +42,27 @@ angular.module('mm.core')
4242 var extractVariableRegex = new RegExp ( '{{([^|]+)(|.*)?}}' , 'i' ) ,
4343 tagsToIgnore = [ 'AUDIO' , 'VIDEO' , 'BUTTON' , 'INPUT' , 'SELECT' , 'TEXTAREA' , 'A' ] ;
4444
45+ /**
46+ * Add mm-external-content and its extra attributes to a certain element.
47+ *
48+ * @param {Object } el DOM element to add the attributes to.
49+ * @param {String } [component] Component.
50+ * @param {Number } [componentId] Component ID.
51+ * @param {String } [siteId] Site ID.
52+ */
53+ function addExternalContent ( el , component , componentId , siteId ) {
54+ el . setAttribute ( 'mm-external-content' , '' ) ;
55+ if ( component ) {
56+ el . setAttribute ( 'component' , component ) ;
57+ if ( componentId ) {
58+ el . setAttribute ( 'component-id' , componentId ) ;
59+ }
60+ }
61+ if ( siteId ) {
62+ el . setAttribute ( 'siteid' , siteId ) ;
63+ }
64+ }
65+
4566 /**
4667 * Returns the number of characters to shorten the text. If the text shouldn't be shortened, returns undefined.
4768 *
@@ -80,6 +101,15 @@ angular.module('mm.core')
80101 }
81102 }
82103
104+ /**
105+ * Add class to adapt media to a certain element.
106+ *
107+ * @param {Object } el Dom element to add the class to.
108+ */
109+ function addMediaAdaptClass ( el ) {
110+ angular . element ( el ) . addClass ( 'mm-media-adapt-width' ) ;
111+ }
112+
83113 /**
84114 * Format contents and render.
85115 *
@@ -175,44 +205,21 @@ angular.module('mm.core')
175205 return $mmText . formatText ( text , attrs . clean , attrs . singleline , shorten ) . then ( function ( formatted ) {
176206
177207 var el = element [ 0 ] ,
178- elWidth = el . offsetWidth || el . width || el . clientWidth ;
179-
180- function addMediaAdaptClass ( el ) {
181- angular . element ( el ) . addClass ( 'mm-media-adapt-width' ) ;
182- }
183-
184- // Convert the content into DOM.
185- var dom = angular . element ( '<div>' ) . html ( formatted ) ;
208+ elWidth = el . offsetWidth || el . width || el . clientWidth ,
209+ dom = angular . element ( '<div>' ) . html ( formatted ) ; // Convert the content into DOM.
186210
187211 // Walk through the content to find the links and add our directive to it.
188212 // Important: We need to look for links first because in 'img' we add new links without mm-browser.
189213 angular . forEach ( dom . find ( 'a' ) , function ( anchor ) {
190- anchor . setAttribute ( 'mm-external-content' , '' ) ;
191214 anchor . setAttribute ( 'mm-browser' , '' ) ;
192- if ( component ) {
193- anchor . setAttribute ( 'component' , component ) ;
194- if ( componentId ) {
195- anchor . setAttribute ( 'component-id' , componentId ) ;
196- }
197- }
198- if ( siteId ) {
199- anchor . setAttribute ( 'siteid' , siteId ) ;
200- }
215+ addExternalContent ( anchor , component , componentId , siteId ) ;
201216 } ) ;
202217
203218 // Walk through the content to find images, and add our directive.
204219 angular . forEach ( dom . find ( 'img' ) , function ( img ) {
205220 addMediaAdaptClass ( img ) ;
206- img . setAttribute ( 'mm-external-content' , '' ) ;
207- if ( component ) {
208- img . setAttribute ( 'component' , component ) ;
209- if ( componentId ) {
210- img . setAttribute ( 'component-id' , componentId ) ;
211- }
212- }
213- if ( siteId ) {
214- img . setAttribute ( 'siteid' , siteId ) ;
215- }
221+ addExternalContent ( img , component , componentId , siteId ) ;
222+
216223 // Check if image width has been adapted. If so, add an icon to view the image at full size.
217224 var imgWidth = img . offsetWidth || img . width || img . clientWidth ;
218225 if ( imgWidth > elWidth ) {
@@ -228,8 +235,12 @@ angular.module('mm.core')
228235 }
229236 } ) ;
230237
231- angular . forEach ( dom . find ( 'audio' ) , addMediaAdaptClass ) ;
232- angular . forEach ( dom . find ( 'video' ) , addMediaAdaptClass ) ;
238+ angular . forEach ( dom . find ( 'audio' ) , function ( el ) {
239+ treatMedia ( el , component , componentId , siteId ) ;
240+ } ) ;
241+ angular . forEach ( dom . find ( 'video' ) , function ( el ) {
242+ treatMedia ( el , component , componentId , siteId ) ;
243+ } ) ;
233244 angular . forEach ( dom . find ( 'iframe' ) , addMediaAdaptClass ) ;
234245
235246 return dom . html ( ) ;
@@ -255,6 +266,25 @@ angular.module('mm.core')
255266 }
256267 }
257268
269+ /**
270+ * Add media adapt class and mm-external-content to the media element and their child sources.
271+ *
272+ * @param {Object } el DOM element.
273+ * @param {String } [component] Component.
274+ * @param {Number } [componentId] Component ID.
275+ * @param {String } [siteId] Site ID.
276+ */
277+ function treatMedia ( el , component , componentId , siteId ) {
278+ addMediaAdaptClass ( el ) ;
279+
280+ addExternalContent ( el , component , componentId , siteId ) ;
281+ angular . forEach ( angular . element ( el ) . find ( 'source' ) , function ( source ) {
282+ source . setAttribute ( 'target-src' , source . getAttribute ( 'src' ) ) ;
283+ source . removeAttribute ( 'src' ) ;
284+ addExternalContent ( source , component , componentId , siteId ) ;
285+ } ) ;
286+ }
287+
258288 return {
259289 restrict : 'E' ,
260290 scope : true ,
0 commit comments