Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions www/addons/mod_book/services/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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');
Expand All @@ -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));
}
})();

Expand All @@ -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('<div>');
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);
}
});
});
Expand Down
25 changes: 4 additions & 21 deletions www/addons/mod_page/services/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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));
}
})();

Expand All @@ -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('<div>');
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);
}
});
});
Expand Down
27 changes: 5 additions & 22 deletions www/addons/mod_resource/services/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div>');
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();
}
});
});
Expand Down
42 changes: 42 additions & 0 deletions www/core/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div>'),
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;
};
});