forked from svgdotjs/svg.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.js
More file actions
executable file
·255 lines (229 loc) · 8.39 KB
/
Copy pathtext.js
File metadata and controls
executable file
·255 lines (229 loc) · 8.39 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// IMPORTANT!!!
// The native getBBox() on text elements isn't always accurate in the decimals.
// Therefore sometimes some rounding is used to make test work as expected.
describe('Text', function() {
var text
beforeEach(function() {
text = draw.text(loremIpsum).size(5)
})
afterEach(function() {
draw.clear()
})
describe('x()', function() {
it('returns the value of x without an argument', function() {
expect(text.x()).toBe(0)
})
it('sets the value of x with the first argument', function() {
text.x(123)
var box = text.bbox()
expect(box.x).toBeCloseTo(123)
})
it('sets the value of x based on the anchor with the first argument', function() {
text.x(123, true)
var box = text.bbox()
expect(box.x).toBeCloseTo(123)
})
})
describe('y()', function() {
it('returns the value of y without an argument', function() {
expect(text.y(0).y()).toBe(0)
})
it('returns the value of y when y is a percentual value', function() {
expect(text.y('45%').y()).toBe('45%')
})
it('sets the value of y with the first argument', function() {
text.y(345)
var box = text.bbox()
expect(box.y).toBe(345)
})
it('sets the value of y with a percent value', function() {
text.y('40%')
expect(text.node.getAttribute('y')).toBe('40%')
})
})
describe('cx()', function() {
it('returns the value of cx without an argument', function() {
var box = text.bbox()
expect(text.cx()).toBeCloseTo(box.width / 2)
})
it('sets the value of cx with the first argument', function() {
text.cx(123)
var box = text.bbox()
expect(box.cx).toBeCloseTo(123)
})
it('sets the value of cx based on the anchor with the first argument', function() {
text.cx(123, true)
var box = text.bbox()
expect(box.cx).toBeCloseTo(123)
})
})
describe('cy()', function() {
it('returns the value of cy without an argument', function() {
var box = text.bbox()
expect(text.cy()).toBe(box.cy)
})
it('sets the value of cy with the first argument', function() {
text.cy(345)
var box = text.bbox()
expect(Math.round(box.cy * 10) / 10).toBe(345)
})
})
describe('move()', function() {
it('sets the x and y position', function() {
text.move(123,456)
var box = text.bbox()
expect(box.x).toBeCloseTo(123)
expect(box.y).toBeCloseTo(456)
})
})
describe('center()', function() {
it('sets the cx and cy position', function() {
text.center(321, 567)
var box = text.bbox()
expect(box.cx).toBeCloseTo(321)
expect(box.cy).toBeCloseTo(567, 1)
})
})
describe('size()', function() {
it('should define the width and height of the element', function() {
text.size(50)
expect(text.attr('font-size').valueOf()).toBe(50)
})
})
describe('translate()', function() {
it('sets the translation of an element', function() {
text.transform({ x: 12, y: 12 })
expect(text.node.getAttribute('transform')).toBe('matrix(1,0,0,1,12,12)')
})
})
describe('text()', function() {
it('adds content in a nested tspan', function() {
text.text('It is a bear!')
expect(text.node.childNodes[0].nodeType).toBe(1)
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('It is a bear!')
})
it('adds content in a nested tspan even with an empty string', function() {
text.text('')
expect(text.node.childNodes[0].nodeType).toBe(1)
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('')
})
it('creates multiple lines with a newline separated string', function() {
text.text('It is\nJUST\na bear!')
expect(text.node.childNodes.length).toBe(3)
})
it('stores a reference to the tspan nodes in a set', function() {
text.text('It is\nJUST\na bear!')
expect(text.lines instanceof SVG.Set).toBe(true)
expect(text.lines.members.length).toBe(3)
})
it('stores the text value in the content reference', function() {
text.text('It is\nJUST\na bear!')
expect(text.content).toBe('It is\nJUST\na bear!')
})
it('gets the given content of a text element without an argument', function() {
text.text('It is another bear!')
expect(text.node.childNodes[0].nodeType).toBe(1)
expect(text.text()).toMatch('It is another bear!')
})
it('accepts a block as first arguments', function() {
text.text(function(add) {
add.tspan('mastaba')
add.plain('hut')
})
expect(text.node.childNodes[0].nodeType).toBe(1)
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('mastaba')
expect(text.node.childNodes[1].nodeType).toBe(3)
expect(text.node.childNodes[1].nodeValue).toBe('hut')
})
})
describe('plain()', function() {
it('adds content without a tspan', function() {
text.plain('It is a bear!')
expect(text.node.childNodes[0].nodeType).toBe(3)
expect(text.node.childNodes[0].nodeValue).toBe('It is a bear!')
})
it('clears content before adding new content', function() {
text.plain('It is not a bear!')
expect(text.node.childNodes.length).toBe(1)
expect(text.node.childNodes[0].nodeValue).toBe('It is not a bear!')
})
it('stores the text value in the content reference', function() {
text.plain('Just plain text!')
expect(text.content).toBe('Just plain text!')
})
})
describe('tspan()', function() {
it('adds content in a tspan', function() {
text.tspan('It is a bear!')
expect(text.node.childNodes[0].nodeType).toBe(1)
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('It is a bear!')
})
it('clears content before adding new content', function() {
text.tspan('It is not a bear!')
expect(text.node.childNodes.length).toBe(1)
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('It is not a bear!')
})
})
describe('clear()', function() {
it('removes all content', function() {
text.text(function(add) {
add.tspan('The first.')
add.tspan('The second.')
add.tspan('The third.')
})
expect(text.node.childNodes.length).toBe(3)
text.clear()
expect(text.node.childNodes.length).toBe(0)
})
it('initializes a new set for the lines reference', function() {
var lines = text.lines
text.clear()
expect(text.lines instanceof SVG.Set).toBe(true)
expect(text.lines).not.toBe(lines)
})
it('clears the stored content value', function() {
text.text('Stored locally.')
expect(text.content).toBe('Stored locally.')
})
})
describe('length()', function() {
it('gets total length of text', function() {
text.text(function(add) {
add.tspan('The first.')
add.tspan('The second.')
add.tspan('The third.')
})
expect(text.length()).toBe(text.lines.get(0).length() + text.lines.get(1).length() + text.lines.get(2).length())
})
})
describe('build()', function() {
it('enables adding multiple plain text nodes when given true', function() {
text.clear().build(true)
text.plain('A great piece!')
text.plain('Another great piece!')
expect(text.node.childNodes[0].nodeValue).toBe('A great piece!')
expect(text.node.childNodes[1].nodeValue).toBe('Another great piece!')
})
it('enables adding multiple tspan nodes when given true', function() {
text.clear().build(true)
text.tspan('A great piece!')
text.tspan('Another great piece!')
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('A great piece!')
expect(text.node.childNodes[1].childNodes[0].nodeValue).toBe('Another great piece!')
})
it('disables adding multiple plain text nodes when given false', function() {
text.clear().build(true)
text.plain('A great piece!')
text.build(false).plain('Another great piece!')
expect(text.node.childNodes[0].nodeValue).toBe('Another great piece!')
expect(text.node.childNodes[1]).toBe(undefined)
})
it('disables adding multiple tspan nodes when given false', function() {
text.clear().build(true)
text.tspan('A great piece!')
text.build(false).tspan('Another great piece!')
expect(text.node.childNodes[0].childNodes[0].nodeValue).toBe('Another great piece!')
expect(text.node.childNodes[1]).toBe(undefined)
})
})
})