- Imagelib backward compatibility enhancement: Allow `namespace-key` as

alternative to `namespace` so as not to break old SVG-Edit which fail
  at *presence* of `namespace` (fixes #274)
- Forward compatibility enhancement: Once IE9 support may be dropped,
  we may post messages as objects, so don't break if objects received
  (embedded API, xdomain, Imagelib)
master
Brett Zamir 2018-09-25 03:06:22 +08:00
parent 7ff2721ba9
commit 704336c0f2
4 changed files with 19 additions and 7 deletions

View File

@ -31,6 +31,12 @@
- Fix (Context menus): Avoid showing double shortcuts (#285); add some
missing ones
- Fix (Star extension): Minor: Avoid erring if `inradius` is `NaN`
- Forward compatibility enhancement: Once IE9 support may be dropped,
we may post messages as objects, so don't break if objects received
(embedded API, xdomain, Imagelib)
- Imagelib backward compatibility enhancement: Allow `namespace-key` as
alternative to `namespace` so as not to break old SVG-Edit which fail
at *presence* of `namespace` (fixes #274)
- Refactoring: Avoid passing unused arguments, setting unused variables,
and making unnecessary checks; avoid useless call to `createSVGMatrix`
- Refactoring: Avoid useless assignment (courtesty lgtm)

View File

@ -63,11 +63,11 @@ function addCallback (t, {result, error, id: cbid}) {
function messageListener (e) {
// We accept and post strings as opposed to objects for the sake of IE9 support; this
// will most likely be changed in the future
if (typeof e.data !== 'string') {
if (!e.data || !['string', 'object'].includes(typeof e.data)) {
return;
}
const {allowedOrigins} = this,
data = e.data && JSON.parse(e.data);
data = typeof e.data === 'object' ? e.data : JSON.parse(e.data);
if (!data || typeof data !== 'object' || data.namespace !== 'svg-edit' ||
e.source !== this.frame.contentWindow ||
(!allowedOrigins.includes('*') && !allowedOrigins.includes(e.origin))

View File

@ -63,7 +63,7 @@ export default {
// Receive `postMessage` data
window.addEventListener('message', function ({origin, data: response}) {
if (!response || typeof response !== 'string') {
if (!response || !['string', 'object'].includes(typeof response)) {
// Do nothing
return;
}
@ -71,8 +71,14 @@ export default {
// Todo: This block can be removed (and the above check changed to
// insist on an object) if embedAPI moves away from a string to
// an object (if IE9 support not needed)
response = JSON.parse(response);
if (response.namespace !== 'imagelib') {
response = typeof response === 'object' ? response : JSON.parse(response);
if (response.namespace !== 'imagelib' &&
// Allow this alternative per https://github.com/SVG-Edit/svgedit/issues/274
// so that older libraries may post with `namespace-key` and not
// break older SVG-Edit versions which insisted on the *absence*
// of a `namespace` property
response['namespace-key'] !== 'imagelib'
) {
return;
}
if (!allowedImageLibOrigins.includes('*') && !allowedImageLibOrigins.includes(origin)) {

View File

@ -11,10 +11,10 @@ export default {
try {
window.addEventListener('message', function (e) {
// We accept and post strings for the sake of IE9 support
if (typeof e.data !== 'string' || e.data.charAt() === '|') {
if (!e.data || !['string', 'object'].includes(typeof e.data) || e.data.charAt() === '|') {
return;
}
const data = JSON.parse(e.data);
const data = typeof e.data === 'object' ? e.data : JSON.parse(e.data);
if (!data || typeof data !== 'object' || data.namespace !== 'svgCanvas') {
return;
}