Skip to content

Commit

Permalink
fix(transform): always consolidate after setting
Browse files Browse the repository at this point in the history
This prevents an IE / MS Edge bug where

  setTransform(translate(100, 200))

would end up as

  <g ... transform="translate(100px, 200px)" />

breaking diagram interaction.
  • Loading branch information
nikku committed Feb 8, 2019
1 parent 0e97fe1 commit 980e9d6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
30 changes: 19 additions & 11 deletions lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
function wrapMatrix(transformList, transform) {
if (transform instanceof SVGMatrix) {
return transformList.createSVGTransformFromMatrix(transform);
} else {
return transform;
}

return transform;
}


function setTransforms(transformList, transforms) {
var i, t;

Expand All @@ -18,20 +19,27 @@ function setTransforms(transformList, transforms) {
for (i = 0; (t = transforms[i]); i++) {
transformList.appendItem(wrapMatrix(transformList, t));
}

transformList.consolidate();
}

/**
* Get or set the transforms on the given node.
*
* @param {SVGElement} node
* @param {SVGTransform|SVGMatrix|Array<SVGTransform|SVGMatrix>} [transforms]
*
* @return {SVGTransform} the consolidated transform
*/
export default function transform(node, transforms) {
var transformList = node.transform.baseVal;

if (arguments.length === 1) {
return transformList.consolidate();
} else {
if (transforms.length) {
setTransforms(transformList, transforms);
} else {
transformList.initialize(wrapMatrix(transformList, transforms));
if (transforms) {

if (!Array.isArray(transforms)) {
transforms = [ transforms ];
}

setTransforms(transformList, transforms);
}

return transformList.consolidate();
}
52 changes: 35 additions & 17 deletions test/spec/transform.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
create,
createMatrix,
createMatrix as matrix,
createTransform,
attr,
transform
Expand All @@ -13,16 +13,26 @@ import {

describe('transform', function() {

it('should create and set transform', function() {
it('should apply matrix', function() {

// given
var matrix = createMatrix({ e: 100, f: 100 }),
g = create('g');
var g = create('g');

// when
var t = createTransform(matrix);
transform(g, matrix({ e: 100, f: 100 }));

// then
expect(normalizeAttr(attr(g, 'transform'))).to.eql('matrix(1 0 0 1 100 100)');
});

transform(g, t);

it('should apply translate', function() {

// given
var g = create('g');

// when
transform(g, translate(100, 100));

// then
expect(normalizeAttr(attr(g, 'transform'))).to.eql('matrix(1 0 0 1 100 100)');
Expand All @@ -32,11 +42,10 @@ describe('transform', function() {
it('should create and set transform form matrix', function() {

// given
var matrix = createMatrix({ e: 100, f: 100 }),
g = create('g');
var g = create('g');

// when
transform(g, matrix);
transform(g, matrix({ e: 100, f: 100 }));

// then
expect(normalizeAttr(attr(g, 'transform'))).to.eql('matrix(1 0 0 1 100 100)');
Expand All @@ -48,17 +57,26 @@ describe('transform', function() {
// given
var g = create('g');

var m1 = createMatrix({ e: -50, f: -80 }),
m2 = createMatrix({ e: 100, f: 100 }),
t = createTransform();

t.setTranslate(100, 100);

// when
transform(g, [ m1, m2, t ]);
transform(g, [
matrix({ e: -50, f: -80 }),
matrix({ e: 100, f: 100 }),
translate(100, 100)
]);

// then
expect(normalizeAttr(attr(g, 'transform'))).to.eql('matrix(1 0 0 1 150 120)');
});

});
});


// helpers //////////////////

function translate(x, y) {
var t = createTransform();

t.setTranslate(x, y);

return t;
}

0 comments on commit 980e9d6

Please sign in to comment.