From 80c16aa6ef403da8371ef501a988e5b3b49bb20b Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 14 Oct 2013 01:24:52 +0000 Subject: [PATCH] Move back to string messages for postMessage for sake of IE9 (though not with eval); avoid IE9 error with localStorage and missing select icon git-svn-id: http://svg-edit.googlecode.com/svn/trunk@2590 eee81c28-f429-11dd-99c0-75d572ba1ddd --- editor/embedapi.js | 22 ++++++++++++++-------- editor/extensions/ext-imagelib.js | 7 +++++++ editor/svg-editor.js | 23 +++++++++++++++-------- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/editor/embedapi.js b/editor/embedapi.js index 120ccbe5..536435d5 100644 --- a/editor/embedapi.js +++ b/editor/embedapi.js @@ -82,17 +82,22 @@ function EmbeddedSVGEdit(frame){ } // Older IE may need a polyfill for addEventListener, but so it would for SVG - window.addEventListener("message", function(e){ - if (!e.data || typeof e.data !== "object" || e.data.namespace !== "svg-edit") { + window.addEventListener('message', function(e){ + // We accept and post strings for the sake of IE9 support + if (typeof e.data !== 'string') { + return; + } + var data = e.data && JSON.parse(e.data); + if (!data || typeof data !== 'object' || data.namespace !== 'svg-edit') { return; } - var data = e.data.result || e.data.error, - cbid = e.data.id; + var result = data.result || data.error, + cbid = data.id; if(t.callbacks[cbid]){ - if(e.data.result){ - t.callbacks[cbid](data); + if(data.result){ + t.callbacks[cbid](result); }else{ - t.callbacks[cbid](data, "error"); + t.callbacks[cbid](result, "error"); } } }, false); @@ -105,7 +110,8 @@ EmbeddedSVGEdit.prototype.send = function(name, args, callback){ this.callbacks[cbid] = callback; setTimeout(function(){ // delay for the callback to be set in case its synchronous // Todo: Handle non-JSON arguments and return values (undefined, nonfinite numbers, functions, and built-in objects like Date, RegExp), etc.? - t.frame.contentWindow.postMessage({namespace: "svgCanvas", id: cbid, name: name, args: args}, '*'); + // We accept and post strings for the sake of IE9 support + t.frame.contentWindow.postMessage(JSON.stringify({namespace: "svgCanvas", id: cbid, name: name, args: args}), '*'); }, 0); return cbid; }; diff --git a/editor/extensions/ext-imagelib.js b/editor/extensions/ext-imagelib.js index 5e9c27b3..3d241626 100644 --- a/editor/extensions/ext-imagelib.js +++ b/editor/extensions/ext-imagelib.js @@ -68,6 +68,13 @@ svgEditor.addExtension("imagelib", function() { // Do nothing return; } + try { // This block can be removed if embedAPI moves away from a string to an object (if IE9 support not needed) + var res = JSON.parse(response); + if (res.namespace) { // Part of embedAPI communications + return; + } + } + catch (e) {} var char1 = response.charAt(0); diff --git a/editor/svg-editor.js b/editor/svg-editor.js index e13cd2e5..340429f6 100644 --- a/editor/svg-editor.js +++ b/editor/svg-editor.js @@ -259,7 +259,7 @@ } } else { var name = 'svgedit-' + Editor.curConfig.canvasName; - var cached = window.localStorage.getItem(name); + var cached = window.localStorage && window.localStorage.getItem(name); if (cached) { Editor.loadFromString(cached); } @@ -597,7 +597,9 @@ selectedElement = elems[0]; } } else { - setIcon('#tool_select', 'select'); + setTimeout(function () { + setIcon('#tool_select', 'select'); + }, 1000); } }; @@ -4571,16 +4573,21 @@ // Callback handler for embedapi.js try { window.addEventListener('message', function(e) { - if (!e.data || typeof e.data !== 'object' || e.data.namespace !== 'svgCanvas') { + // We accept and post strings for the sake of IE9 support + if (typeof e.data !== 'string') { + return; + } + var data = JSON.parse(e.data); + if (!data || typeof data !== 'object' || data.namespace !== 'svgCanvas') { return; } - var cbid = e.data.id, - name = e.data.name, - args = e.data.args; + var cbid = data.id, + name = data.name, + args = data.args; try { - e.source.postMessage({namespace: 'svg-edit', id: cbid, result: svgCanvas[name](args)}, '*'); + e.source.postMessage(JSON.stringify({namespace: 'svg-edit', id: cbid, result: svgCanvas[name](args)}), '*'); } catch(err) { - e.source.postMessage({namespace: 'svg-edit', id: cbid, error: err.message}, '*'); + e.source.postMessage(JSON.stringify({namespace: 'svg-edit', id: cbid, error: err.message}), '*'); } }, false); } catch(err) {