fix #222 Browsers block data URI (#224)

* fix #222 Browsers block data URI

* add spaces and blank lines
This commit is contained in:
Igin
2018-04-13 02:00:52 +08:00
committed by Jeff Schiller
parent 396cce40eb
commit edb8a75402
3 changed files with 74 additions and 9 deletions

View File

@@ -193,7 +193,8 @@ TODOS
enterNewLinkURL: 'Enter the new hyperlink URL',
errorLoadingSVG: 'Error: Unable to load SVG data',
URLloadFail: 'Unable to load from URL',
retrieving: 'Retrieving \'%s\' ...'
retrieving: 'Retrieving \'%s\' ...',
popupWindowBlocked: 'Popup window may be blocked by browser'
}
};
@@ -1086,10 +1087,15 @@ TODOS
exportWindowName = data.exportWindowName;
if (exportWindowName) {
exportWindow = window.open('', exportWindowName); // A hack to get the window via JSON-able name without opening a new one
exportWindow = window.open(svgedit.utilities.blankPageObjectURL || '', exportWindowName); // A hack to get the window via JSON-able name without opening a new one
}
exportWindow.location.href = data.datauri;
if (!exportWindow || exportWindow.closed) {
$.alert(uiStrings.notification.popupWindowBlocked);
return;
}
exportWindow.location.href = data.bloburl || data.datauri;
var done = $.pref('export_notice_done');
if (done !== 'all') {
var note = uiStrings.notification.saveFromBrowser.replace('%s', data.type);
@@ -3602,6 +3608,7 @@ TODOS
svgCanvas.save(saveOpts);
};
var loadingURL;
var clickExport = function() {
$.select('Select an image type for export: ', [
// See http://kangax.github.io/jstests/toDataUrl_mime_type_test/ for a useful list of MIME types and browser support
@@ -3620,10 +3627,20 @@ TODOS
editor.exportWindowCt++;
}
exportWindowName = curConfig.canvasName + editor.exportWindowCt;
exportWindow = window.open(
'data:text/html;charset=utf-8,' + encodeURIComponent('<title>' + str + '</title><h1>' + str + '</h1>'),
exportWindowName
);
var popHTML, popURL;
if (loadingURL) {
popURL = loadingURL;
} else {
popHTML = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>' + str + '</title></head><body><h1>' + str + '</h1></body><html>';
if (typeof URL && URL.createObjectURL) {
var blob = new Blob([popHTML], {type:'text/html'});
popURL = URL.createObjectURL(blob);
} else {
popURL = 'data:text/html;base64;charset=utf-8,' + svgedit.utilities.encode64(popHTML);
}
loadingURL = popURL;
}
exportWindow = window.open(popURL, exportWindowName);
}
if (imgType === 'PDF') {
if (!customExportPDF) {

View File

@@ -4063,8 +4063,15 @@ this.rasterExport = function(imgType, quality, exportWindowName) {
canvg(c, str, {renderCallback: function() {
var dataURLType = (type === 'ICO' ? 'BMP' : type).toLowerCase();
var datauri = quality ? c.toDataURL('image/' + dataURLType, quality) : c.toDataURL('image/' + dataURLType);
call('exported', {datauri: datauri, svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality, exportWindowName: exportWindowName});
if (c.toBlob) {
c.toBlob(function (blob) {
var bloburl = svgedit.utilities.createObjectURL(blob);
call('exported', {datauri: datauri, bloburl: bloburl, svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality, exportWindowName: exportWindowName});
}, mimeType, quality);
return;
}
var bloburl = svgedit.utilities.dataURLToObjectURL(datauri);
call('exported', {datauri: datauri, bloburl: bloburl, svg: str, issues: issues, type: imgType, mimeType: mimeType, quality: quality, exportWindowName: exportWindowName});
}});
})();
};

View File

@@ -170,6 +170,47 @@ svgedit.utilities.encodeUTF8 = function (argString) {
return unescape(encodeURIComponent(argString));
};
/**
* convert dataURL to object URL
* @param {string} dataurl
* @return {string} object URL or empty string
*/
svgedit.utilities.dataURLToObjectURL = function (dataurl) {
if (typeof Uint8Array == 'undefined' || typeof Blob == 'undefined' || typeof URL == 'undefined' || !URL.createObjectURL) {
return '';
}
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
var blob = new Blob([u8arr], {type:mime});
return URL.createObjectURL(blob);
};
/**
* get object URL for a blob object
* @param {Blob} blob A Blob object or File object
* @return {string} object URL or empty string
*/
svgedit.utilities.createObjectURL = function (blob) {
if (!blob || typeof URL == 'undefined' || !URL.createObjectURL) {
return '';
}
return URL.createObjectURL(blob);
};
/**
* @property {string} blankPageObjectURL
*/
svgedit.utilities.blankPageObjectURL = function () {
if (typeof Blob == 'undefined') {
return '';
}
var blob = new Blob(['<html><head><title>SVG-edit</title></head><body>&nbsp;</body></html>'], {type:'text/html'});
return svgedit.utilities.createObjectURL(blob);
}();
// Function: svgedit.utilities.convertToXMLReferences
// Converts a string to use XML references
svgedit.utilities.convertToXMLReferences = function(input) {