PageRenderTime 199ms CodeModel.GetById 60ms app.highlight 81ms RepoModel.GetById 50ms app.codeStats 0ms

/lib/phpexcel/PHPExcel/Helper/HTML.php

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