xyc
2024-05-17 6b24f642b01cf3cd1be0d5833273fa2867d389e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* eslint-env qunit */
 
import {NS} from '../editor/namespaces.js';
import * as utilities from '../editor/utilities.js';
import * as coords from '../editor/coords.js';
import * as recalculate from '../editor/recalculate.js';
 
// log function
QUnit.log((details) => {
  if (window.console && window.console.log) {
    window.console.log(details.result + ' :: ' + details.message);
  }
});
 
const root = document.getElementById('root');
const svgroot = document.createElementNS(NS.SVG, 'svg');
svgroot.id = 'svgroot';
root.append(svgroot);
const svg = document.createElementNS(NS.SVG, 'svg');
svgroot.append(svg);
 
let elemId = 1;
function setUp () {
  utilities.init(
    /**
    * @implements {module:utilities.EditorContext}
    */
    {
      getSVGRoot () { return svg; },
      getDOMDocument () { return null; },
      getDOMContainer () { return null; }
    }
  );
  coords.init(
    /**
    * @implements {module:coords.EditorContext}
    */
    {
      getGridSnapping () { return false; },
      getDrawing () {
        return {
          getNextId () { return '' + elemId++; }
        };
      }
    }
  );
  recalculate.init(
    /**
    * @implements {module:recalculate.EditorContext}
    */
    {
      getSVGRoot () { return svg; },
      getStartTransform () { return ''; },
      setStartTransform () {}
    }
  );
}
 
let elem;
 
function setUpRect () {
  setUp();
  elem = document.createElementNS(NS.SVG, 'rect');
  elem.setAttribute('x', '200');
  elem.setAttribute('y', '150');
  elem.setAttribute('width', '250');
  elem.setAttribute('height', '120');
  svg.append(elem);
}
 
function setUpTextWithTspan () {
  setUp();
  elem = document.createElementNS(NS.SVG, 'text');
  elem.setAttribute('x', '200');
  elem.setAttribute('y', '150');
 
  const tspan = document.createElementNS(NS.SVG, 'tspan');
  tspan.setAttribute('x', '200');
  tspan.setAttribute('y', '150');
 
  const theText = document.createTextNode('Foo bar');
  tspan.append(theText);
  elem.append(tspan);
  svg.append(elem);
}
 
function tearDown () {
  while (svg.hasChildNodes()) {
    svg.firstChild.remove();
  }
}
 
QUnit.test('Test recalculateDimensions() on rect with identity matrix', function (assert) {
  assert.expect(1);
 
  setUpRect();
  elem.setAttribute('transform', 'matrix(1,0,0,1,0,0)');
 
  recalculate.recalculateDimensions(elem);
 
  // Ensure that the identity matrix is swallowed and the element has no
  // transform on it.
  assert.equal(elem.hasAttribute('transform'), false);
 
  tearDown();
});
 
QUnit.test('Test recalculateDimensions() on rect with simple translate', function (assert) {
  assert.expect(5);
 
  setUpRect();
  elem.setAttribute('transform', 'translate(100,50)');
 
  recalculate.recalculateDimensions(elem);
 
  assert.equal(elem.hasAttribute('transform'), false);
  assert.equal(elem.getAttribute('x'), '300');
  assert.equal(elem.getAttribute('y'), '200');
  assert.equal(elem.getAttribute('width'), '250');
  assert.equal(elem.getAttribute('height'), '120');
  tearDown();
});
 
QUnit.test('Test recalculateDimensions() on text w/tspan with simple translate', function (assert) {
  assert.expect(5);
 
  setUpTextWithTspan();
  elem.setAttribute('transform', 'translate(100,50)');
 
  recalculate.recalculateDimensions(elem);
 
  // Ensure that the identity matrix is swallowed and the element has no
  // transform on it.
  assert.equal(elem.hasAttribute('transform'), false);
  assert.equal(elem.getAttribute('x'), '300');
  assert.equal(elem.getAttribute('y'), '200');
 
  const tspan = elem.firstElementChild;
  assert.equal(tspan.getAttribute('x'), '300');
  assert.equal(tspan.getAttribute('y'), '200');
 
  tearDown();
});
 
// TODO: Since recalculateDimensions() and surrounding code is
// probably the largest, most complicated and strange piece of
// code in SVG-edit, we need to write a whole lot of unit tests
// for it here.