Completed
Push — develop ( febbe8...0477e6 )
by Adrien
25:31 queued 16:01
created

Html::startBoldTag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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