Skip to content

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

Open
@ledahu

Description

@ledahu

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 "";
    }
}


Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions