PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.21k stars 2.68k forks source link

HTML writer don't handle fontStyle in link + Fix #2241

Open ledahu opened 2 years ago

ledahu commented 2 years ago

Describe the Bug

the HTML writer don't handle fontstyle

Steps to Reproduce

Please provide a code sample that reproduces the issue.

just look at https://github.com/PHPOffice/PHPWord/blob/develop/src/PhpWord/Writer/HTML/Element/Link.php

no style or class write

I done my fix , if it can help ...

adding{$this->setFontStyle()} in the <a> and adding the setFontStyle() rewrited from the text element.


namespace PhpOffice\PhpWord\Writer\HTML\Element;

use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;

/**
 * Link element HTML writer
 *
 * @since 0.10.0
 */
class Link extends Text
{
    /**
     * Write link
     *
     * @return string
     */
    public function write()
    {
        if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
            return '';
        }

        $prefix = $this->element->isInternal() ? '#' : '';
        $content = $this->writeOpening();
        if (Settings::isOutputEscapingEnabled()) {
            $content .= "<a href=\"{$prefix}{$this->escaper->escapeHtmlAttr($this->element->getSource())}\" {$this->setFontStyle()}>{$this->escaper->escapeHtml($this->element->getText())}</a>";
        } else {
            $content .= "<a href=\"{$prefix}{$this->element->getSource()}\" {$this->setFontStyle()}>{$this->element->getText()}</a>";
        }
        $content .= $this->writeClosing();

        return $content;
    }

    /**
     * Set font style.
     */
    private function setFontStyle()
    {
        $element = $this->element;
        $style = '';
        $fontStyle = $element->getFontStyle();
        $fStyleIsObject = ($fontStyle instanceof Font);
        if ($fStyleIsObject) {
            $styleWriter = new FontStyleWriter($fontStyle);
            $style = $styleWriter->write();
        } elseif (is_string($fontStyle)) {
            $style = $fontStyle;
        }
        if ($style) {
            $attribute = $fStyleIsObject ? 'style' : 'class';
             return " {$attribute}=\"{$style}\" ";
        }
        return "";
    }
}
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue for you, please try to help by debugging it further and sharing your results. Thank you for your contributions.