diff --git a/www/addons/mod_book/services/book.js b/www/addons/mod_book/services/book.js
index ea467b9dcd9..825c904b289 100644
--- a/www/addons/mod_book/services/book.js
+++ b/www/addons/mod_book/services/book.js
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_book')
* @ngdoc service
* @name $mmaModBook
*/
-.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModBookComponent) {
+.factory('$mmaModBook', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModBookComponent) {
$log = $log.getInstance('$mmaModBook');
var self = {};
@@ -262,7 +262,6 @@ angular.module('mm.addons.mod_book')
// Promise handling when we are in a browser.
promise = (function() {
- var deferred;
if (!indexUrl) {
// If ever that happens.
$log.debug('Could not locate the index chapter');
@@ -272,9 +271,7 @@ angular.module('mm.addons.mod_book')
return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModBookComponent, moduleId);
} else {
// We return the live URL.
- deferred = $q.defer();
- deferred.resolve($mmSite.fixPluginfileURL(indexUrl));
- return deferred.promise;
+ return $q.when($mmSite.fixPluginfileURL(indexUrl));
}
})();
@@ -286,22 +283,7 @@ angular.module('mm.addons.mod_book')
} else {
// Now that we have the content, we update the SRC to point back to
// the external resource. That will be caught by mm-format-text.
- var html = angular.element('
');
- html.html(response.data);
- angular.forEach(html.find('img'), function(img) {
- var src = paths[decodeURIComponent(img.getAttribute('src'))];
- if (typeof src !== 'undefined') {
- img.setAttribute('src', src);
- }
- });
- // We do the same for links.
- angular.forEach(html.find('a'), function(anchor) {
- var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
- if (typeof href !== 'undefined') {
- anchor.setAttribute('href', href);
- }
- });
- return html.html();
+ return $mmUtil.restoreSourcesInHtml(response.data, paths);
}
});
});
diff --git a/www/addons/mod_page/services/page.js b/www/addons/mod_page/services/page.js
index 6a0055f43d0..2ffda451264 100644
--- a/www/addons/mod_page/services/page.js
+++ b/www/addons/mod_page/services/page.js
@@ -21,7 +21,7 @@ angular.module('mm.addons.mod_page')
* @ngdoc service
* @name $mmaModPage
*/
-.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, mmaModPageComponent) {
+.factory('$mmaModPage', function($mmFilepool, $mmSite, $mmFS, $http, $log, $q, $mmSitesManager, $mmUtil, mmaModPageComponent) {
$log = $log.getInstance('$mmaModPage');
var self = {};
@@ -164,9 +164,7 @@ angular.module('mm.addons.mod_page')
return $mmFilepool.downloadUrl($mmSite.getId(), indexUrl, false, mmaModPageComponent, moduleId);
} else {
// We return the live URL.
- deferred = $q.defer();
- deferred.resolve($mmSite.fixPluginfileURL(indexUrl));
- return deferred.promise;
+ return $q.when($mmSite.fixPluginfileURL(indexUrl));
}
})();
@@ -177,23 +175,8 @@ angular.module('mm.addons.mod_page')
return $q.reject();
} else {
// Now that we have the content, we update the SRC to point back to
- // the external resource. That will be caught by mm-format-text.
- var html = angular.element('
');
- html.html(response.data);
- angular.forEach(html.find('img'), function(img) {
- var src = paths[decodeURIComponent(img.getAttribute('src'))];
- if (typeof src !== 'undefined') {
- img.setAttribute('src', src);
- }
- });
- // We do the same for links.
- angular.forEach(html.find('a'), function(anchor) {
- var href = paths[decodeURIComponent(anchor.getAttribute('href'))];
- if (typeof href !== 'undefined') {
- anchor.setAttribute('href', href);
- }
- });
- return html.html();
+ // the external resource. That will b caught by mm-format-text.
+ return $mmUtil.restoreSourcesInHtml(response.data, paths);
}
});
});
diff --git a/www/addons/mod_resource/services/resource.js b/www/addons/mod_resource/services/resource.js
index 430c38a74ac..f1e27bc041d 100644
--- a/www/addons/mod_resource/services/resource.js
+++ b/www/addons/mod_resource/services/resource.js
@@ -227,30 +227,13 @@ angular.module('mm.addons.mod_resource')
} else {
// Now that we have the content, we update the SRC to point back to
// the external resource. That will be caught by mm-format-text.
- var html = angular.element('
');
- html.append(response.data);
-
- angular.forEach(html.find('img'), function(img) {
- var src = paths[decodeURIComponent(img.getAttribute('src'))];
- if (typeof src !== 'undefined') {
- img.setAttribute('src', src);
- }
- });
- // We do the same for links.
- angular.forEach(html.find('a'), function(anchor) {
- var href = decodeURIComponent(anchor.getAttribute('href')),
- url = paths[href],
- ext = $mmFS.getFileExtension(href);
- if (typeof url !== 'undefined') {
- anchor.setAttribute('href', url);
- if (ext == 'html' || ext == 'html') {
- anchor.setAttribute('mma-mod-resource-html-link', 1);
- anchor.setAttribute('data-href', href);
- }
+ return $mmUtil.restoreSourcesInHtml(response.data, paths, function(anchor, href) {
+ var ext = $mmFS.getFileExtension(href);
+ if (ext == 'html' || ext == 'html') {
+ anchor.setAttribute('mma-mod-resource-html-link', 1);
+ anchor.setAttribute('data-href', href);
}
});
-
- return html.html();
}
});
});
diff --git a/www/core/lib/util.js b/www/core/lib/util.js
index 13ec86ae03f..27afede5662 100644
--- a/www/core/lib/util.js
+++ b/www/core/lib/util.js
@@ -943,6 +943,48 @@ angular.module('mm.core')
return params;
};
+ /**
+ * Given an HTML, searched all links and media and tries to restore original sources using the paths object.
+ *
+ * @module mm.core
+ * @ngdoc method
+ * @name $mmUtil#restoreSourcesInHtml
+ * @param {String} html HTML code.
+ * @param {Object} paths Object linking URLs in the html code with the real URLs to use.
+ * @param {Function} [anchorFn] Function to call with each anchor. Optional.
+ * @return {String} Treated HTML code.
+ */
+ self.restoreSourcesInHtml = function(html, paths, anchorFn) {
+ var div = angular.element('
'),
+ media;
+ div.html(html);
+
+ // Treat img, audio, video and source.
+ media = div[0].querySelectorAll('img, video, audio, source');
+ angular.forEach(media, function(el) {
+ var src = paths[decodeURIComponent(el.getAttribute('src'))];
+ if (typeof src !== 'undefined') {
+ el.setAttribute('src', src);
+ }
+ });
+
+ // We do the same for links.
+ angular.forEach(div.find('a'), function(anchor) {
+ var href = decodeURIComponent(anchor.getAttribute('href')),
+ url = paths[href];
+
+ if (typeof url !== 'undefined') {
+ anchor.setAttribute('href', url);
+
+ if (angular.isFunction(anchorFn)) {
+ anchorFn(anchor, href);
+ }
+ }
+ });
+
+ return div.html();
+ };
+
return self;
};
});