Automattic\WooCommerce\Vendor\Pelago\Emogrifier\HtmlProcessor

AbstractHtmlProcessor::hasContentTypeMetaTagInHeadprivateWC 1.0

Tests whether the given HTML has a valid Content-Type metadata element within the <head> element. Due to tag omission rules, HTML parsers are expected to end the <head> element and start the <body> element upon encountering a start tag for any element which is permitted only within the <body>.

Method of the class: AbstractHtmlProcessor{}

No Hooks.

Returns

true|false.

Usage

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

AbstractHtmlProcessor::hasContentTypeMetaTagInHead() code WC 10.5.0

private function hasContentTypeMetaTagInHead(string $html): bool
{
    (new Preg())->match(
        '%^.*?(?=<meta(?=\\s)[^>]*\\shttp-equiv=(["\']?+)Content-Type\\g{-1}[\\s/>])%is',
        $html,
        $matches
    );
    if (isset($matches[0])) {
        $htmlBefore = $matches[0];
        try {
            $hasContentTypeMetaTagInHead = !$this->hasEndOfHeadElement($htmlBefore);
        } catch (\RuntimeException $exception) {
            // If something unexpected occurs, assume the `Content-Type` that was found is valid.
            \trigger_error($exception->getMessage());
            $hasContentTypeMetaTagInHead = true;
        }
    } else {
        $hasContentTypeMetaTagInHead = false;
    }

    return $hasContentTypeMetaTagInHead;
}