Completed
Push — develop ( 8cf911...93ccf7 )
by Adrien
33:03
created

HTML::buildTextRun()   F

Complexity

Conditions 11
Paths 513

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 11.0077

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 24
c 1
b 0
f 0
nc 513
nop 0
dl 0
loc 37
ccs 24
cts 25
cp 0.96
crap 11.0077
rs 3.1764

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Helper;
4
5
use DOMDocument;
6
use DOMElement;
7
use DOMNode;
8
use DOMText;
9
10
/**
11
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
12
 *
13
 * This library is free software; you can redistribute it and/or
14
 * modify it under the terms of the GNU Lesser General Public
15
 * License as published by the Free Software Foundation; either
16
 * version 2.1 of the License, or (at your option) any later version.
17
 *
18
 * This library is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21
 * Lesser General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Lesser General Public
24
 * License along with this library; if not, write to the Free Software
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
26
 *
27
 * @category   PhpSpreadsheet
28
 * @copyright  Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
29
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
30
 * @version    ##VERSION##, ##DATE##
31
 */
32
class HTML
33
{
34
    protected static $colourMap = [
35
        'aliceblue' => 'f0f8ff',
36
        'antiquewhite' => 'faebd7',
37
        'antiquewhite1' => 'ffefdb',
38
        'antiquewhite2' => 'eedfcc',
39
        'antiquewhite3' => 'cdc0b0',
40
        'antiquewhite4' => '8b8378',
41
        'aqua' => '00ffff',
42
        'aquamarine1' => '7fffd4',
43
        'aquamarine2' => '76eec6',
44
        'aquamarine4' => '458b74',
45
        'azure1' => 'f0ffff',
46
        'azure2' => 'e0eeee',
47
        'azure3' => 'c1cdcd',
48
        'azure4' => '838b8b',
49
        'beige' => 'f5f5dc',
50
        'bisque1' => 'ffe4c4',
51
        'bisque2' => 'eed5b7',
52
        'bisque3' => 'cdb79e',
53
        'bisque4' => '8b7d6b',
54
        'black' => '000000',
55
        'blanchedalmond' => 'ffebcd',
56
        'blue' => '0000ff',
57
        'blue1' => '0000ff',
58
        'blue2' => '0000ee',
59
        'blue4' => '00008b',
60
        'blueviolet' => '8a2be2',
61
        'brown' => 'a52a2a',
62
        'brown1' => 'ff4040',
63
        'brown2' => 'ee3b3b',
64
        'brown3' => 'cd3333',
65
        'brown4' => '8b2323',
66
        'burlywood' => 'deb887',
67
        'burlywood1' => 'ffd39b',
68
        'burlywood2' => 'eec591',
69
        'burlywood3' => 'cdaa7d',
70
        'burlywood4' => '8b7355',
71
        'cadetblue' => '5f9ea0',
72
        'cadetblue1' => '98f5ff',
73
        'cadetblue2' => '8ee5ee',
74
        'cadetblue3' => '7ac5cd',
75
        'cadetblue4' => '53868b',
76
        'chartreuse1' => '7fff00',
77
        'chartreuse2' => '76ee00',
78
        'chartreuse3' => '66cd00',
79
        'chartreuse4' => '458b00',
80
        'chocolate' => 'd2691e',
81
        'chocolate1' => 'ff7f24',
82
        'chocolate2' => 'ee7621',
83
        'chocolate3' => 'cd661d',
84
        'coral' => 'ff7f50',
85
        'coral1' => 'ff7256',
86
        'coral2' => 'ee6a50',
87
        'coral3' => 'cd5b45',
88
        'coral4' => '8b3e2f',
89
        'cornflowerblue' => '6495ed',
90
        'cornsilk1' => 'fff8dc',
91
        'cornsilk2' => 'eee8cd',
92
        'cornsilk3' => 'cdc8b1',
93
        'cornsilk4' => '8b8878',
94
        'cyan1' => '00ffff',
95
        'cyan2' => '00eeee',
96
        'cyan3' => '00cdcd',
97
        'cyan4' => '008b8b',
98
        'darkgoldenrod' => 'b8860b',
99
        'darkgoldenrod1' => 'ffb90f',
100
        'darkgoldenrod2' => 'eead0e',
101
        'darkgoldenrod3' => 'cd950c',
102
        'darkgoldenrod4' => '8b6508',
103
        'darkgreen' => '006400',
104
        'darkkhaki' => 'bdb76b',
105
        'darkolivegreen' => '556b2f',
106
        'darkolivegreen1' => 'caff70',
107
        'darkolivegreen2' => 'bcee68',
108
        'darkolivegreen3' => 'a2cd5a',
109
        'darkolivegreen4' => '6e8b3d',
110
        'darkorange' => 'ff8c00',
111
        'darkorange1' => 'ff7f00',
112
        'darkorange2' => 'ee7600',
113
        'darkorange3' => 'cd6600',
114
        'darkorange4' => '8b4500',
115
        'darkorchid' => '9932cc',
116
        'darkorchid1' => 'bf3eff',
117
        'darkorchid2' => 'b23aee',
118
        'darkorchid3' => '9a32cd',
119
        'darkorchid4' => '68228b',
120
        'darksalmon' => 'e9967a',
121
        'darkseagreen' => '8fbc8f',
122
        'darkseagreen1' => 'c1ffc1',
123
        'darkseagreen2' => 'b4eeb4',
124
        'darkseagreen3' => '9bcd9b',
125
        'darkseagreen4' => '698b69',
126
        'darkslateblue' => '483d8b',
127
        'darkslategray' => '2f4f4f',
128
        'darkslategray1' => '97ffff',
129
        'darkslategray2' => '8deeee',
130
        'darkslategray3' => '79cdcd',
131
        'darkslategray4' => '528b8b',
132
        'darkturquoise' => '00ced1',
133
        'darkviolet' => '9400d3',
134
        'deeppink1' => 'ff1493',
135
        'deeppink2' => 'ee1289',
136
        'deeppink3' => 'cd1076',
137
        'deeppink4' => '8b0a50',
138
        'deepskyblue1' => '00bfff',
139
        'deepskyblue2' => '00b2ee',
140
        'deepskyblue3' => '009acd',
141
        'deepskyblue4' => '00688b',
142
        'dimgray' => '696969',
143
        'dodgerblue1' => '1e90ff',
144
        'dodgerblue2' => '1c86ee',
145
        'dodgerblue3' => '1874cd',
146
        'dodgerblue4' => '104e8b',
147
        'firebrick' => 'b22222',
148
        'firebrick1' => 'ff3030',
149
        'firebrick2' => 'ee2c2c',
150
        'firebrick3' => 'cd2626',
151
        'firebrick4' => '8b1a1a',
152
        'floralwhite' => 'fffaf0',
153
        'forestgreen' => '228b22',
154
        'fuchsia' => 'ff00ff',
155
        'gainsboro' => 'dcdcdc',
156
        'ghostwhite' => 'f8f8ff',
157
        'gold1' => 'ffd700',
158
        'gold2' => 'eec900',
159
        'gold3' => 'cdad00',
160
        'gold4' => '8b7500',
161
        'goldenrod' => 'daa520',
162
        'goldenrod1' => 'ffc125',
163
        'goldenrod2' => 'eeb422',
164
        'goldenrod3' => 'cd9b1d',
165
        'goldenrod4' => '8b6914',
166
        'gray' => 'bebebe',
167
        'gray1' => '030303',
168
        'gray10' => '1a1a1a',
169
        'gray11' => '1c1c1c',
170
        'gray12' => '1f1f1f',
171
        'gray13' => '212121',
172
        'gray14' => '242424',
173
        'gray15' => '262626',
174
        'gray16' => '292929',
175
        'gray17' => '2b2b2b',
176
        'gray18' => '2e2e2e',
177
        'gray19' => '303030',
178
        'gray2' => '050505',
179
        'gray20' => '333333',
180
        'gray21' => '363636',
181
        'gray22' => '383838',
182
        'gray23' => '3b3b3b',
183
        'gray24' => '3d3d3d',
184
        'gray25' => '404040',
185
        'gray26' => '424242',
186
        'gray27' => '454545',
187
        'gray28' => '474747',
188
        'gray29' => '4a4a4a',
189
        'gray3' => '080808',
190
        'gray30' => '4d4d4d',
191
        'gray31' => '4f4f4f',
192
        'gray32' => '525252',
193
        'gray33' => '545454',
194
        'gray34' => '575757',
195
        'gray35' => '595959',
196
        'gray36' => '5c5c5c',
197
        'gray37' => '5e5e5e',
198
        'gray38' => '616161',
199
        'gray39' => '636363',
200
        'gray4' => '0a0a0a',
201
        'gray40' => '666666',
202
        'gray41' => '696969',
203
        'gray42' => '6b6b6b',
204
        'gray43' => '6e6e6e',
205
        'gray44' => '707070',
206
        'gray45' => '737373',
207
        'gray46' => '757575',
208
        'gray47' => '787878',
209
        'gray48' => '7a7a7a',
210
        'gray49' => '7d7d7d',
211
        'gray5' => '0d0d0d',
212
        'gray50' => '7f7f7f',
213
        'gray51' => '828282',
214
        'gray52' => '858585',
215
        'gray53' => '878787',
216
        'gray54' => '8a8a8a',
217
        'gray55' => '8c8c8c',
218
        'gray56' => '8f8f8f',
219
        'gray57' => '919191',
220
        'gray58' => '949494',
221
        'gray59' => '969696',
222
        'gray6' => '0f0f0f',
223
        'gray60' => '999999',
224
        'gray61' => '9c9c9c',
225
        'gray62' => '9e9e9e',
226
        'gray63' => 'a1a1a1',
227
        'gray64' => 'a3a3a3',
228
        'gray65' => 'a6a6a6',
229
        'gray66' => 'a8a8a8',
230
        'gray67' => 'ababab',
231
        'gray68' => 'adadad',
232
        'gray69' => 'b0b0b0',
233
        'gray7' => '121212',
234
        'gray70' => 'b3b3b3',
235
        'gray71' => 'b5b5b5',
236
        'gray72' => 'b8b8b8',
237
        'gray73' => 'bababa',
238
        'gray74' => 'bdbdbd',
239
        'gray75' => 'bfbfbf',
240
        'gray76' => 'c2c2c2',
241
        'gray77' => 'c4c4c4',
242
        'gray78' => 'c7c7c7',
243
        'gray79' => 'c9c9c9',
244
        'gray8' => '141414',
245
        'gray80' => 'cccccc',
246
        'gray81' => 'cfcfcf',
247
        'gray82' => 'd1d1d1',
248
        'gray83' => 'd4d4d4',
249
        'gray84' => 'd6d6d6',
250
        'gray85' => 'd9d9d9',
251
        'gray86' => 'dbdbdb',
252
        'gray87' => 'dedede',
253
        'gray88' => 'e0e0e0',
254
        'gray89' => 'e3e3e3',
255
        'gray9' => '171717',
256
        'gray90' => 'e5e5e5',
257
        'gray91' => 'e8e8e8',
258
        'gray92' => 'ebebeb',
259
        'gray93' => 'ededed',
260
        'gray94' => 'f0f0f0',
261
        'gray95' => 'f2f2f2',
262
        'gray97' => 'f7f7f7',
263
        'gray98' => 'fafafa',
264
        'gray99' => 'fcfcfc',
265
        'green' => '00ff00',
266
        'green1' => '00ff00',
267
        'green2' => '00ee00',
268
        'green3' => '00cd00',
269
        'green4' => '008b00',
270
        'greenyellow' => 'adff2f',
271
        'honeydew1' => 'f0fff0',
272
        'honeydew2' => 'e0eee0',
273
        'honeydew3' => 'c1cdc1',
274
        'honeydew4' => '838b83',
275
        'hotpink' => 'ff69b4',
276
        'hotpink1' => 'ff6eb4',
277
        'hotpink2' => 'ee6aa7',
278
        'hotpink3' => 'cd6090',
279
        'hotpink4' => '8b3a62',
280
        'indianred' => 'cd5c5c',
281
        'indianred1' => 'ff6a6a',
282
        'indianred2' => 'ee6363',
283
        'indianred3' => 'cd5555',
284
        'indianred4' => '8b3a3a',
285
        'ivory1' => 'fffff0',
286
        'ivory2' => 'eeeee0',
287
        'ivory3' => 'cdcdc1',
288
        'ivory4' => '8b8b83',
289
        'khaki' => 'f0e68c',
290
        'khaki1' => 'fff68f',
291
        'khaki2' => 'eee685',
292
        'khaki3' => 'cdc673',
293
        'khaki4' => '8b864e',
294
        'lavender' => 'e6e6fa',
295
        'lavenderblush1' => 'fff0f5',
296
        'lavenderblush2' => 'eee0e5',
297
        'lavenderblush3' => 'cdc1c5',
298
        'lavenderblush4' => '8b8386',
299
        'lawngreen' => '7cfc00',
300
        'lemonchiffon1' => 'fffacd',
301
        'lemonchiffon2' => 'eee9bf',
302
        'lemonchiffon3' => 'cdc9a5',
303
        'lemonchiffon4' => '8b8970',
304
        'light' => 'eedd82',
305
        'lightblue' => 'add8e6',
306
        'lightblue1' => 'bfefff',
307
        'lightblue2' => 'b2dfee',
308
        'lightblue3' => '9ac0cd',
309
        'lightblue4' => '68838b',
310
        'lightcoral' => 'f08080',
311
        'lightcyan1' => 'e0ffff',
312
        'lightcyan2' => 'd1eeee',
313
        'lightcyan3' => 'b4cdcd',
314
        'lightcyan4' => '7a8b8b',
315
        'lightgoldenrod1' => 'ffec8b',
316
        'lightgoldenrod2' => 'eedc82',
317
        'lightgoldenrod3' => 'cdbe70',
318
        'lightgoldenrod4' => '8b814c',
319
        'lightgoldenrodyellow' => 'fafad2',
320
        'lightgray' => 'd3d3d3',
321
        'lightpink' => 'ffb6c1',
322
        'lightpink1' => 'ffaeb9',
323
        'lightpink2' => 'eea2ad',
324
        'lightpink3' => 'cd8c95',
325
        'lightpink4' => '8b5f65',
326
        'lightsalmon1' => 'ffa07a',
327
        'lightsalmon2' => 'ee9572',
328
        'lightsalmon3' => 'cd8162',
329
        'lightsalmon4' => '8b5742',
330
        'lightseagreen' => '20b2aa',
331
        'lightskyblue' => '87cefa',
332
        'lightskyblue1' => 'b0e2ff',
333
        'lightskyblue2' => 'a4d3ee',
334
        'lightskyblue3' => '8db6cd',
335
        'lightskyblue4' => '607b8b',
336
        'lightslateblue' => '8470ff',
337
        'lightslategray' => '778899',
338
        'lightsteelblue' => 'b0c4de',
339
        'lightsteelblue1' => 'cae1ff',
340
        'lightsteelblue2' => 'bcd2ee',
341
        'lightsteelblue3' => 'a2b5cd',
342
        'lightsteelblue4' => '6e7b8b',
343
        'lightyellow1' => 'ffffe0',
344
        'lightyellow2' => 'eeeed1',
345
        'lightyellow3' => 'cdcdb4',
346
        'lightyellow4' => '8b8b7a',
347
        'lime' => '00ff00',
348
        'limegreen' => '32cd32',
349
        'linen' => 'faf0e6',
350
        'magenta' => 'ff00ff',
351
        'magenta2' => 'ee00ee',
352
        'magenta3' => 'cd00cd',
353
        'magenta4' => '8b008b',
354
        'maroon' => 'b03060',
355
        'maroon1' => 'ff34b3',
356
        'maroon2' => 'ee30a7',
357
        'maroon3' => 'cd2990',
358
        'maroon4' => '8b1c62',
359
        'medium' => '66cdaa',
360
        'mediumaquamarine' => '66cdaa',
361
        'mediumblue' => '0000cd',
362
        'mediumorchid' => 'ba55d3',
363
        'mediumorchid1' => 'e066ff',
364
        'mediumorchid2' => 'd15fee',
365
        'mediumorchid3' => 'b452cd',
366
        'mediumorchid4' => '7a378b',
367
        'mediumpurple' => '9370db',
368
        'mediumpurple1' => 'ab82ff',
369
        'mediumpurple2' => '9f79ee',
370
        'mediumpurple3' => '8968cd',
371
        'mediumpurple4' => '5d478b',
372
        'mediumseagreen' => '3cb371',
373
        'mediumslateblue' => '7b68ee',
374
        'mediumspringgreen' => '00fa9a',
375
        'mediumturquoise' => '48d1cc',
376
        'mediumvioletred' => 'c71585',
377
        'midnightblue' => '191970',
378
        'mintcream' => 'f5fffa',
379
        'mistyrose1' => 'ffe4e1',
380
        'mistyrose2' => 'eed5d2',
381
        'mistyrose3' => 'cdb7b5',
382
        'mistyrose4' => '8b7d7b',
383
        'moccasin' => 'ffe4b5',
384
        'navajowhite1' => 'ffdead',
385
        'navajowhite2' => 'eecfa1',
386
        'navajowhite3' => 'cdb38b',
387
        'navajowhite4' => '8b795e',
388
        'navy' => '000080',
389
        'navyblue' => '000080',
390
        'oldlace' => 'fdf5e6',
391
        'olive' => '808000',
392
        'olivedrab' => '6b8e23',
393
        'olivedrab1' => 'c0ff3e',
394
        'olivedrab2' => 'b3ee3a',
395
        'olivedrab4' => '698b22',
396
        'orange' => 'ffa500',
397
        'orange1' => 'ffa500',
398
        'orange2' => 'ee9a00',
399
        'orange3' => 'cd8500',
400
        'orange4' => '8b5a00',
401
        'orangered1' => 'ff4500',
402
        'orangered2' => 'ee4000',
403
        'orangered3' => 'cd3700',
404
        'orangered4' => '8b2500',
405
        'orchid' => 'da70d6',
406
        'orchid1' => 'ff83fa',
407
        'orchid2' => 'ee7ae9',
408
        'orchid3' => 'cd69c9',
409
        'orchid4' => '8b4789',
410
        'pale' => 'db7093',
411
        'palegoldenrod' => 'eee8aa',
412
        'palegreen' => '98fb98',
413
        'palegreen1' => '9aff9a',
414
        'palegreen2' => '90ee90',
415
        'palegreen3' => '7ccd7c',
416
        'palegreen4' => '548b54',
417
        'paleturquoise' => 'afeeee',
418
        'paleturquoise1' => 'bbffff',
419
        'paleturquoise2' => 'aeeeee',
420
        'paleturquoise3' => '96cdcd',
421
        'paleturquoise4' => '668b8b',
422
        'palevioletred' => 'db7093',
423
        'palevioletred1' => 'ff82ab',
424
        'palevioletred2' => 'ee799f',
425
        'palevioletred3' => 'cd6889',
426
        'palevioletred4' => '8b475d',
427
        'papayawhip' => 'ffefd5',
428
        'peachpuff1' => 'ffdab9',
429
        'peachpuff2' => 'eecbad',
430
        'peachpuff3' => 'cdaf95',
431
        'peachpuff4' => '8b7765',
432
        'pink' => 'ffc0cb',
433
        'pink1' => 'ffb5c5',
434
        'pink2' => 'eea9b8',
435
        'pink3' => 'cd919e',
436
        'pink4' => '8b636c',
437
        'plum' => 'dda0dd',
438
        'plum1' => 'ffbbff',
439
        'plum2' => 'eeaeee',
440
        'plum3' => 'cd96cd',
441
        'plum4' => '8b668b',
442
        'powderblue' => 'b0e0e6',
443
        'purple' => 'a020f0',
444
        'rebeccapurple' => '663399',
445
        'purple1' => '9b30ff',
446
        'purple2' => '912cee',
447
        'purple3' => '7d26cd',
448
        'purple4' => '551a8b',
449
        'red' => 'ff0000',
450
        'red1' => 'ff0000',
451
        'red2' => 'ee0000',
452
        'red3' => 'cd0000',
453
        'red4' => '8b0000',
454
        'rosybrown' => 'bc8f8f',
455
        'rosybrown1' => 'ffc1c1',
456
        'rosybrown2' => 'eeb4b4',
457
        'rosybrown3' => 'cd9b9b',
458
        'rosybrown4' => '8b6969',
459
        'royalblue' => '4169e1',
460
        'royalblue1' => '4876ff',
461
        'royalblue2' => '436eee',
462
        'royalblue3' => '3a5fcd',
463
        'royalblue4' => '27408b',
464
        'saddlebrown' => '8b4513',
465
        'salmon' => 'fa8072',
466
        'salmon1' => 'ff8c69',
467
        'salmon2' => 'ee8262',
468
        'salmon3' => 'cd7054',
469
        'salmon4' => '8b4c39',
470
        'sandybrown' => 'f4a460',
471
        'seagreen1' => '54ff9f',
472
        'seagreen2' => '4eee94',
473
        'seagreen3' => '43cd80',
474
        'seagreen4' => '2e8b57',
475
        'seashell1' => 'fff5ee',
476
        'seashell2' => 'eee5de',
477
        'seashell3' => 'cdc5bf',
478
        'seashell4' => '8b8682',
479
        'sienna' => 'a0522d',
480
        'sienna1' => 'ff8247',
481
        'sienna2' => 'ee7942',
482
        'sienna3' => 'cd6839',
483
        'sienna4' => '8b4726',
484
        'silver' => 'c0c0c0',
485
        'skyblue' => '87ceeb',
486
        'skyblue1' => '87ceff',
487
        'skyblue2' => '7ec0ee',
488
        'skyblue3' => '6ca6cd',
489
        'skyblue4' => '4a708b',
490
        'slateblue' => '6a5acd',
491
        'slateblue1' => '836fff',
492
        'slateblue2' => '7a67ee',
493
        'slateblue3' => '6959cd',
494
        'slateblue4' => '473c8b',
495
        'slategray' => '708090',
496
        'slategray1' => 'c6e2ff',
497
        'slategray2' => 'b9d3ee',
498
        'slategray3' => '9fb6cd',
499
        'slategray4' => '6c7b8b',
500
        'snow1' => 'fffafa',
501
        'snow2' => 'eee9e9',
502
        'snow3' => 'cdc9c9',
503
        'snow4' => '8b8989',
504
        'springgreen1' => '00ff7f',
505
        'springgreen2' => '00ee76',
506
        'springgreen3' => '00cd66',
507
        'springgreen4' => '008b45',
508
        'steelblue' => '4682b4',
509
        'steelblue1' => '63b8ff',
510
        'steelblue2' => '5cacee',
511
        'steelblue3' => '4f94cd',
512
        'steelblue4' => '36648b',
513
        'tan' => 'd2b48c',
514
        'tan1' => 'ffa54f',
515
        'tan2' => 'ee9a49',
516
        'tan3' => 'cd853f',
517
        'tan4' => '8b5a2b',
518
        'teal' => '008080',
519
        'thistle' => 'd8bfd8',
520
        'thistle1' => 'ffe1ff',
521
        'thistle2' => 'eed2ee',
522
        'thistle3' => 'cdb5cd',
523
        'thistle4' => '8b7b8b',
524
        'tomato1' => 'ff6347',
525
        'tomato2' => 'ee5c42',
526
        'tomato3' => 'cd4f39',
527
        'tomato4' => '8b3626',
528
        'turquoise' => '40e0d0',
529
        'turquoise1' => '00f5ff',
530
        'turquoise2' => '00e5ee',
531
        'turquoise3' => '00c5cd',
532
        'turquoise4' => '00868b',
533
        'violet' => 'ee82ee',
534
        'violetred' => 'd02090',
535
        'violetred1' => 'ff3e96',
536
        'violetred2' => 'ee3a8c',
537
        'violetred3' => 'cd3278',
538
        'violetred4' => '8b2252',
539
        'wheat' => 'f5deb3',
540
        'wheat1' => 'ffe7ba',
541
        'wheat2' => 'eed8ae',
542
        'wheat3' => 'cdba96',
543
        'wheat4' => '8b7e66',
544
        'white' => 'ffffff',
545
        'whitesmoke' => 'f5f5f5',
546
        'yellow' => 'ffff00',
547
        'yellow1' => 'ffff00',
548
        'yellow2' => 'eeee00',
549
        'yellow3' => 'cdcd00',
550
        'yellow4' => '8b8b00',
551
        'yellowgreen' => '9acd32',
552
    ];
553
554
    protected $face;
555
    protected $size;
556
    protected $color;
557
558
    protected $bold = false;
559
    protected $italic = false;
560
    protected $underline = false;
561
    protected $superscript = false;
562
    protected $subscript = false;
563
    protected $strikethrough = false;
564
565
    protected $startTagCallbacks = [
566
        'font' => 'startFontTag',
567
        'b' => 'startBoldTag',
568
        'strong' => 'startBoldTag',
569
        'i' => 'startItalicTag',
570
        'em' => 'startItalicTag',
571
        'u' => 'startUnderlineTag',
572
        'ins' => 'startUnderlineTag',
573
        'del' => 'startStrikethruTag',
574
        'sup' => 'startSuperscriptTag',
575
        'sub' => 'startSubscriptTag',
576
    ];
577
578
    protected $endTagCallbacks = [
579
        'font' => 'endFontTag',
580
        'b' => 'endBoldTag',
581
        'strong' => 'endBoldTag',
582
        'i' => 'endItalicTag',
583
        'em' => 'endItalicTag',
584
        'u' => 'endUnderlineTag',
585
        'ins' => 'endUnderlineTag',
586
        'del' => 'endStrikethruTag',
587
        'sup' => 'endSuperscriptTag',
588
        'sub' => 'endSubscriptTag',
589
        'br' => 'breakTag',
590
        'p' => 'breakTag',
591
        'h1' => 'breakTag',
592
        'h2' => 'breakTag',
593
        'h3' => 'breakTag',
594
        'h4' => 'breakTag',
595
        'h5' => 'breakTag',
596
        'h6' => 'breakTag',
597
    ];
598
599
    protected $stack = [];
600
601
    protected $stringData = '';
602
603
    protected $richTextObject;
604
605 1
    protected function initialise()
606
    {
607 1
        $this->face = $this->size = $this->color = null;
608 1
        $this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false;
609
610 1
        $this->stack = [];
611
612 1
        $this->stringData = '';
613 1
    }
614
615 1
    public function toRichTextObject($html)
616
    {
617 1
        $this->initialise();
618
619
        //    Create a new DOM object
620 1
        $dom = new DOMDocument();
621
        //    Load the HTML file into the DOM object
622
        //  Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup
623 1
        $loaded = @$dom->loadHTML($html);
0 ignored issues
show
Unused Code introduced by
$loaded is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
624
625
        //    Discard excess white space
626 1
        $dom->preserveWhiteSpace = false;
627
628 1
        $this->richTextObject = new \PhpOffice\PhpSpreadsheet\RichText();
629 1
        $this->parseElements($dom);
630
631
        // Clean any further spurious whitespace
632 1
        $this->cleanWhitespace();
633
634 1
        return $this->richTextObject;
635
    }
636
637 1
    protected function cleanWhitespace()
638
    {
639 1
        foreach ($this->richTextObject->getRichTextElements() as $key => $element) {
640 1
            $text = $element->getText();
641
            // Trim any leading spaces on the first run
642 1
            if ($key == 0) {
643 1
                $text = ltrim($text);
644
            }
645
            // Trim any spaces immediately after a line break
646 1
            $text = preg_replace('/\n */mu', "\n", $text);
647 1
            $element->setText($text);
648
        }
649 1
    }
650
651 1
    protected function buildTextRun()
652
    {
653 1
        $text = $this->stringData;
654 1
        if (trim($text) === '') {
655 1
            return;
656
        }
657
658 1
        $richtextRun = $this->richTextObject->createTextRun($this->stringData);
659 1
        if ($this->face) {
660
            $richtextRun->getFont()->setName($this->face);
661
        }
662 1
        if ($this->size) {
663 1
            $richtextRun->getFont()->setSize($this->size);
664
        }
665 1
        if ($this->color) {
666 1
            $richtextRun->getFont()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('ff' . $this->color));
667
        }
668 1
        if ($this->bold) {
669 1
            $richtextRun->getFont()->setBold(true);
670
        }
671 1
        if ($this->italic) {
672 1
            $richtextRun->getFont()->setItalic(true);
673
        }
674 1
        if ($this->underline) {
675 1
            $richtextRun->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
676
        }
677 1
        if ($this->superscript) {
678 1
            $richtextRun->getFont()->setSuperScript(true);
679
        }
680 1
        if ($this->subscript) {
681 1
            $richtextRun->getFont()->setSubScript(true);
682
        }
683 1
        if ($this->strikethrough) {
684 1
            $richtextRun->getFont()->setStrikethrough(true);
685
        }
686 1
        $this->stringData = '';
687 1
    }
688
689 1
    protected function rgbToColour($rgb)
690
    {
691 1
        preg_match_all('/\d+/', $rgb, $values);
692 1
        foreach ($values[0] as &$value) {
693 1
            $value = str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
694
        }
695
696 1
        return implode($values[0]);
697
    }
698
699 1
    protected function colourNameLookup($rgb)
700
    {
701 1
        return self::$colourMap[$rgb];
702
    }
703
704 1
    protected function startFontTag($tag)
705
    {
706 1
        foreach ($tag->attributes as $attribute) {
707 1
            $attributeName = strtolower($attribute->name);
708 1
            $attributeValue = $attribute->value;
709
710 1
            if ($attributeName == 'color') {
711 1
                if (preg_match('/rgb\s*\(/', $attributeValue)) {
712 1
                    $this->$attributeName = $this->rgbToColour($attributeValue);
713 1
                } elseif (strpos(trim($attributeValue), '#') === 0) {
714 1
                    $this->$attributeName = ltrim($attributeValue, '#');
715
                } else {
716 1
                    $this->$attributeName = $this->colourNameLookup($attributeValue);
717
                }
718
            } else {
719 1
                $this->$attributeName = $attributeValue;
720
            }
721
        }
722 1
    }
723
724 1
    protected function endFontTag()
725
    {
726 1
        $this->face = $this->size = $this->color = null;
727 1
    }
728
729 1
    protected function startBoldTag()
730
    {
731 1
        $this->bold = true;
732 1
    }
733
734 1
    protected function endBoldTag()
735
    {
736 1
        $this->bold = false;
737 1
    }
738
739 1
    protected function startItalicTag()
740
    {
741 1
        $this->italic = true;
742 1
    }
743
744 1
    protected function endItalicTag()
745
    {
746 1
        $this->italic = false;
747 1
    }
748
749 1
    protected function startUnderlineTag()
750
    {
751 1
        $this->underline = true;
752 1
    }
753
754 1
    protected function endUnderlineTag()
755
    {
756 1
        $this->underline = false;
757 1
    }
758
759 1
    protected function startSubscriptTag()
760
    {
761 1
        $this->subscript = true;
762 1
    }
763
764 1
    protected function endSubscriptTag()
765
    {
766 1
        $this->subscript = false;
767 1
    }
768
769 1
    protected function startSuperscriptTag()
770
    {
771 1
        $this->superscript = true;
772 1
    }
773
774 1
    protected function endSuperscriptTag()
775
    {
776 1
        $this->superscript = false;
777 1
    }
778
779 1
    protected function startStrikethruTag()
780
    {
781 1
        $this->strikethrough = true;
782 1
    }
783
784 1
    protected function endStrikethruTag()
785
    {
786 1
        $this->strikethrough = false;
787 1
    }
788
789 1
    protected function breakTag()
790
    {
791 1
        $this->stringData .= "\n";
792 1
    }
793
794 1
    protected function parseTextNode(DOMText $textNode)
795
    {
796 1
        $domText = preg_replace(
797 1
            '/\s+/u',
798 1
            ' ',
799 1
            str_replace(["\r", "\n"], ' ', $textNode->nodeValue)
800
        );
801 1
        $this->stringData .= $domText;
802 1
        $this->buildTextRun();
803 1
    }
804
805
    /**
806
     * @param DOMElement $element
807
     * @param string $callbackTag
808
     * @param array $callbacks
809
     */
810 1
    protected function handleCallback(DOMElement $element, $callbackTag, array $callbacks)
811
    {
812 1
        if (isset($callbacks[$callbackTag])) {
813 1
            $elementHandler = $callbacks[$callbackTag];
814 1
            if (method_exists($this, $elementHandler)) {
815 1
                call_user_func([$this, $elementHandler], $element);
816
            }
817
        }
818 1
    }
819
820 1
    protected function parseElementNode(DOMElement $element)
821
    {
822 1
        $callbackTag = strtolower($element->nodeName);
823 1
        $this->stack[] = $callbackTag;
824
825 1
        $this->handleCallback($element, $callbackTag, $this->startTagCallbacks);
826
827 1
        $this->parseElements($element);
828 1
        array_pop($this->stack);
829
830 1
        $this->handleCallback($element, $callbackTag, $this->endTagCallbacks);
831 1
    }
832
833 1
    protected function parseElements(DOMNode $element)
834
    {
835 1
        foreach ($element->childNodes as $child) {
836 1
            if ($child instanceof DOMText) {
837 1
                $this->parseTextNode($child);
838
            } elseif ($child instanceof DOMElement) {
839 1
                $this->parseElementNode($child);
840
            }
841
        }
842 1
    }
843
}
844