Automattic\WooCommerce\Vendor\Pelago\Emogrifier\HtmlProcessor

AbstractHtmlProcessor::addContentTypeMetaTagprivateWC 1.0

Adds a Content-Type meta tag for the charset.

This method also ensures that there is a HEAD element.

Method of the class: AbstractHtmlProcessor{}

No Hooks.

Returns

String. the HTML with the meta tag added

Usage

// private - for code of main (parent) class only
$result = $this->addContentTypeMetaTag( $html ): string;
$html(string) (required)
.

AbstractHtmlProcessor::addContentTypeMetaTag() code WC 10.4.3

private function addContentTypeMetaTag(string $html): string
{
    if ($this->hasContentTypeMetaTagInHead($html)) {
        return $html;
    }

    // We are trying to insert the meta tag to the right spot in the DOM.
    // If we just prepended it to the HTML, we would lose attributes set to the HTML tag.
    $hasHeadTag = (new Preg())->match('/<head[\\s>]/i', $html) !== 0;
    $hasHtmlTag = \stripos($html, '<html') !== false;

    if ($hasHeadTag) {
        $reworkedHtml = (new Preg())->replace(
            '/<head(?=[\\s>])([^>]*+)>/i',
            '<head$1>' . self::CONTENT_TYPE_META_TAG,
            $html
        );
    } elseif ($hasHtmlTag) {
        $reworkedHtml = (new Preg())->replace(
            '/<html(.*?)>/is',
            '<html$1><head>' . self::CONTENT_TYPE_META_TAG . '</head>',
            $html
        );
    } else {
        $reworkedHtml = self::CONTENT_TYPE_META_TAG . $html;
    }

    return $reworkedHtml;
}