← All changes
|
vendor/phpoffice/phpexcel/Classes/PHPExcel/Helper/HTML.php
+42
-88
3.1.1
→
3.0.3
View file →
| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | class PHPExcel_Helper_HTML |
| 4 | 4 | { |
| 5 | 5 | protected static $colourMap = array( |
| 6 | - 'aliceblue' => 'f0f8ff', | |
| 6 | + 'aliceblue' => 'f0f8ff', | |
| 7 | 7 | 'antiquewhite' => 'faebd7', |
| 8 | 8 | 'antiquewhite1' => 'ffefdb', |
| 9 | 9 | 'antiquewhite2' => 'eedfcc', |
| 10 | 10 | 'antiquewhite3' => 'cdc0b0', |
| @@ -525,14 +525,14 @@ | ||
| 525 | 525 | protected $face; |
| 526 | 526 | protected $size; |
| 527 | 527 | protected $color; |
| 528 | 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; | |
| 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 | 535 | |
| 536 | 536 | protected $startTagCallbacks = array( |
| 537 | 537 | 'font' => 'startFontTag', |
| 538 | 538 | 'b' => 'startBoldTag', |
| @@ -572,10 +572,9 @@ | ||
| 572 | 572 | protected $stringData = ''; |
| 573 | 573 | |
| 574 | 574 | protected $richTextObject; |
| 575 | 575 | |
| 576 | - protected function initialise() | |
| 577 | - { | |
| 576 | + protected function initialise() { | |
| 578 | 577 | $this->face = $this->size = $this->color = null; |
| 579 | 578 | $this->bold = $this->italic = $this->underline = $this->superscript = $this->subscript = $this->strikethrough = false; |
| 580 | 579 | |
| 581 | 580 | $this->stack = array(); |
| @@ -582,50 +581,29 @@ | ||
| 582 | 581 | |
| 583 | 582 | $this->stringData = ''; |
| 584 | 583 | } |
| 585 | 584 | |
| 586 | - public function toRichTextObject($html) | |
| 587 | - { | |
| 585 | + public function toRichTextObject($html) { | |
| 588 | 586 | $this->initialise(); |
| 589 | 587 | |
| 590 | - // Create a new DOM object | |
| 591 | - $dom = new \DOMDocument; | |
| 592 | - // Load the HTML file into the DOM object | |
| 588 | + // Create a new DOM object | |
| 589 | + $dom = new domDocument; | |
| 590 | + // Load the HTML file into the DOM object | |
| 593 | 591 | // Note the use of error suppression, because typically this will be an html fragment, so not fully valid markup |
| 594 | 592 | $loaded = @$dom->loadHTML($html); |
| 595 | 593 | |
| 596 | - // Discard excess white space | |
| 594 | + // Discard excess white space | |
| 597 | 595 | $dom->preserveWhiteSpace = false; |
| 598 | 596 | |
| 599 | 597 | $this->richTextObject = new PHPExcel_RichText();; |
| 600 | 598 | $this->parseElements($dom); |
| 601 | - | |
| 602 | - // Clean any further spurious whitespace | |
| 603 | - $this->cleanWhitespace(); | |
| 604 | - | |
| 605 | 599 | return $this->richTextObject; |
| 606 | 600 | } |
| 607 | 601 | |
| 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 | - { | |
| 602 | + protected function buildTextRun() { | |
| 624 | 603 | $text = $this->stringData; |
| 625 | - if (trim($text) === '') { | |
| 604 | + if (trim($text) === '') | |
| 626 | 605 | return; |
| 627 | - } | |
| 628 | 606 | |
| 629 | 607 | $richtextRun = $this->richTextObject->createTextRun($this->stringData); |
| 630 | 608 | if ($this->face) { |
| 631 | 609 | $richtextRun->getFont()->setName($this->face); |
| @@ -633,9 +611,9 @@ | ||
| 633 | 611 | if ($this->size) { |
| 634 | 612 | $richtextRun->getFont()->setSize($this->size); |
| 635 | 613 | } |
| 636 | 614 | if ($this->color) { |
| 637 | - $richtextRun->getFont()->setColor(new PHPExcel_Style_Color('ff' . $this->color)); | |
| 615 | + $richtextRun->getFont()->setColor( new PHPExcel_Style_Color( 'ff' . $this->color ) ); | |
| 638 | 616 | } |
| 639 | 617 | if ($this->bold) { |
| 640 | 618 | $richtextRun->getFont()->setBold(true); |
| 641 | 619 | } |
| @@ -656,24 +634,21 @@ | ||
| 656 | 634 | } |
| 657 | 635 | $this->stringData = ''; |
| 658 | 636 | } |
| 659 | 637 | |
| 660 | - protected function rgbToColour($rgb) | |
| 661 | - { | |
| 638 | + protected function rgbToColour($rgb) { | |
| 662 | 639 | preg_match_all('/\d+/', $rgb, $values); |
| 663 | - foreach ($values[0] as &$value) { | |
| 640 | + foreach($values[0] as &$value) { | |
| 664 | 641 | $value = str_pad(dechex($value), 2, '0', STR_PAD_LEFT); |
| 665 | 642 | } |
| 666 | 643 | return implode($values[0]); |
| 667 | 644 | } |
| 668 | 645 | |
| 669 | - protected function colourNameLookup($rgb) | |
| 670 | - { | |
| 646 | + protected function colourNameLookup($rgb) { | |
| 671 | 647 | return self::$colourMap[$rgb]; |
| 672 | 648 | } |
| 673 | 649 | |
| 674 | - protected function startFontTag($tag) | |
| 675 | - { | |
| 650 | + protected function startFontTag($tag) { | |
| 676 | 651 | foreach ($tag->attributes as $attribute) { |
| 677 | 652 | $attributeName = strtolower($attribute->name); |
| 678 | 653 | $attributeValue = $attribute->value; |
| 679 | 654 | |
| @@ -679,9 +654,9 @@ | ||
| 679 | 654 | |
| 680 | 655 | if ($attributeName == 'color') { |
| 681 | 656 | if (preg_match('/rgb\s*\(/', $attributeValue)) { |
| 682 | 657 | $this->$attributeName = $this->rgbToColour($attributeValue); |
| 683 | - } elseif (strpos(trim($attributeValue), '#') === 0) { | |
| 658 | + } elseif(strpos(trim($attributeValue), '#') === 0) { | |
| 684 | 659 | $this->$attributeName = ltrim($attributeValue, '#'); |
| 685 | 660 | } else { |
| 686 | 661 | $this->$attributeName = $this->colourNameLookup($attributeValue); |
| 687 | 662 | } |
| @@ -690,91 +665,71 @@ | ||
| 690 | 665 | } |
| 691 | 666 | } |
| 692 | 667 | } |
| 693 | 668 | |
| 694 | - protected function endFontTag() | |
| 695 | - { | |
| 669 | + protected function endFontTag() { | |
| 696 | 670 | $this->face = $this->size = $this->color = null; |
| 697 | 671 | } |
| 698 | 672 | |
| 699 | - protected function startBoldTag() | |
| 700 | - { | |
| 673 | + protected function startBoldTag() { | |
| 701 | 674 | $this->bold = true; |
| 702 | 675 | } |
| 703 | 676 | |
| 704 | - protected function endBoldTag() | |
| 705 | - { | |
| 677 | + protected function endBoldTag() { | |
| 706 | 678 | $this->bold = false; |
| 707 | 679 | } |
| 708 | 680 | |
| 709 | - protected function startItalicTag() | |
| 710 | - { | |
| 681 | + protected function startItalicTag() { | |
| 711 | 682 | $this->italic = true; |
| 712 | 683 | } |
| 713 | 684 | |
| 714 | - protected function endItalicTag() | |
| 715 | - { | |
| 685 | + protected function endItalicTag() { | |
| 716 | 686 | $this->italic = false; |
| 717 | 687 | } |
| 718 | 688 | |
| 719 | - protected function startUnderlineTag() | |
| 720 | - { | |
| 689 | + protected function startUnderlineTag() { | |
| 721 | 690 | $this->underline = true; |
| 722 | 691 | } |
| 723 | 692 | |
| 724 | - protected function endUnderlineTag() | |
| 725 | - { | |
| 693 | + protected function endUnderlineTag() { | |
| 726 | 694 | $this->underline = false; |
| 727 | 695 | } |
| 728 | 696 | |
| 729 | - protected function startSubscriptTag() | |
| 730 | - { | |
| 697 | + protected function startSubscriptTag() { | |
| 731 | 698 | $this->subscript = true; |
| 732 | 699 | } |
| 733 | 700 | |
| 734 | - protected function endSubscriptTag() | |
| 735 | - { | |
| 701 | + protected function endSubscriptTag() { | |
| 736 | 702 | $this->subscript = false; |
| 737 | 703 | } |
| 738 | 704 | |
| 739 | - protected function startSuperscriptTag() | |
| 740 | - { | |
| 705 | + protected function startSuperscriptTag() { | |
| 741 | 706 | $this->superscript = true; |
| 742 | 707 | } |
| 743 | 708 | |
| 744 | - protected function endSuperscriptTag() | |
| 745 | - { | |
| 709 | + protected function endSuperscriptTag() { | |
| 746 | 710 | $this->superscript = false; |
| 747 | 711 | } |
| 748 | 712 | |
| 749 | - protected function startStrikethruTag() | |
| 750 | - { | |
| 713 | + protected function startStrikethruTag() { | |
| 751 | 714 | $this->strikethrough = true; |
| 752 | 715 | } |
| 753 | 716 | |
| 754 | - protected function endStrikethruTag() | |
| 755 | - { | |
| 717 | + protected function endStrikethruTag() { | |
| 756 | 718 | $this->strikethrough = false; |
| 757 | 719 | } |
| 758 | 720 | |
| 759 | - protected function breakTag() | |
| 760 | - { | |
| 761 | - $this->stringData .= "\n"; | |
| 721 | + protected function breakTag() { | |
| 722 | + $this->stringData .= PHP_EOL; | |
| 762 | 723 | } |
| 763 | 724 | |
| 764 | - protected function parseTextNode(DOMText $textNode) | |
| 765 | - { | |
| 766 | - $domText = preg_replace( | |
| 767 | - '/\s+/u', | |
| 768 | - ' ', | |
| 769 | - str_replace(array("\r", "\n"), ' ', $textNode->nodeValue) | |
| 770 | - ); | |
| 725 | + protected function parseTextNode(DOMText $textNode) { | |
| 726 | + $domText = preg_replace('/\s+/u', ' ', ltrim($textNode->nodeValue)); | |
| 771 | 727 | $this->stringData .= $domText; |
| 772 | 728 | $this->buildTextRun(); |
| 773 | 729 | } |
| 774 | 730 | |
| 775 | - protected function handleCallback($element, $callbackTag, $callbacks) | |
| 776 | - { | |
| 731 | + protected function handleCallback($element, $callbackTag, $callbacks) { | |
| 777 | 732 | if (isset($callbacks[$callbackTag])) { |
| 778 | 733 | $elementHandler = $callbacks[$callbackTag]; |
| 779 | 734 | if (method_exists($this, $elementHandler)) { |
| 780 | 735 | call_user_func(array($this, $elementHandler), $element); |
| @@ -781,10 +736,9 @@ | ||
| 781 | 736 | } |
| 782 | 737 | } |
| 783 | 738 | } |
| 784 | 739 | |
| 785 | - protected function parseElementNode(DOMElement $element) | |
| 786 | - { | |
| 740 | + protected function parseElementNode(DOMElement $element) { | |
| 787 | 741 | $callbackTag = strtolower($element->nodeName); |
| 788 | 742 | $this->stack[] = $callbackTag; |
| 789 | 743 | |
| 790 | 744 | $this->handleCallback($element, $callbackTag, $this->startTagCallbacks); |
| @@ -789,15 +743,15 @@ | ||
| 789 | 743 | |
| 790 | 744 | $this->handleCallback($element, $callbackTag, $this->startTagCallbacks); |
| 791 | 745 | |
| 792 | 746 | $this->parseElements($element); |
| 747 | + $this->stringData .= ' '; | |
| 793 | 748 | array_pop($this->stack); |
| 794 | 749 | |
| 795 | 750 | $this->handleCallback($element, $callbackTag, $this->endTagCallbacks); |
| 796 | 751 | } |
| 797 | 752 | |
| 798 | - protected function parseElements(DOMNode $element) | |
| 799 | - { | |
| 753 | + protected function parseElements(DOMNode $element) { | |
| 800 | 754 | foreach ($element->childNodes as $child) { |
| 801 | 755 | if ($child instanceof DOMText) { |
| 802 | 756 | $this->parseTextNode($child); |
| 803 | 757 | } elseif ($child instanceof DOMElement) { |