function Html::transformRootRelativeUrlsToAbsolute

Same name in other branches
  1. 9 core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::transformRootRelativeUrlsToAbsolute()
  2. 8.9.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::transformRootRelativeUrlsToAbsolute()
  3. 11.x core/lib/Drupal/Component/Utility/Html.php \Drupal\Component\Utility\Html::transformRootRelativeUrlsToAbsolute()

Converts all root-relative URLs to absolute URLs.

Does not change any existing protocol-relative or absolute URLs. Does not change other relative URLs because they would result in different absolute URLs depending on the current path. For example: when the same content containing such a relative URL (for example 'image.png'), is served from its canonical URL (for example 'http://example.com/some-article') or from a listing or feed (for example 'http://example.com/all-articles') their "current path" differs, resulting in different absolute URLs: 'http://example.com/some-article/image.png' versus 'http://example.com/all-articles/image.png'. Only one can be correct. Therefore relative URLs that are not root-relative cannot be safely transformed and should generally be avoided.

Necessary for HTML that is served outside of a website, for example, RSS and email.

Parameters

string $html: The partial HTML snippet to load. Invalid markup will be corrected on import.

string $scheme_and_host: The root URL, which has a URI scheme, host and optional port.

Return value

string The updated HTML snippet.

4 calls to Html::transformRootRelativeUrlsToAbsolute()
HtmlTest::testTransformRootRelativeUrlsToAbsolute in core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@covers ::transformRootRelativeUrlsToAbsolute @dataProvider providerTestTransformRootRelativeUrlsToAbsolute
HtmlTest::testTransformRootRelativeUrlsToAbsoluteAssertion in core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
@covers ::transformRootRelativeUrlsToAbsolute @dataProvider providerTestTransformRootRelativeUrlsToAbsoluteAssertion
MailManager::doMail in core/lib/Drupal/Core/Mail/MailManager.php
Composes and optionally sends an email message.
RssResponseRelativeUrlFilter::transformRootRelativeUrlsToAbsolute in core/lib/Drupal/Core/EventSubscriber/RssResponseRelativeUrlFilter.php
Converts all root-relative URLs to absolute URLs in RSS markup.

File

core/lib/Drupal/Component/Utility/Html.php, line 470

Class

Html
Provides DOMDocument helpers for parsing and serializing HTML strings.

Namespace

Drupal\Component\Utility

Code

public static function transformRootRelativeUrlsToAbsolute($html, $scheme_and_host) {
    assert(empty(array_diff(array_keys(parse_url($scheme_and_host)), [
        "scheme",
        "host",
        "port",
    ])), '$scheme_and_host contains scheme, host and port at most.');
    assert(isset(parse_url($scheme_and_host)["scheme"]), '$scheme_and_host is absolute and hence has a scheme.');
    assert(isset(parse_url($scheme_and_host)["host"]), '$base_url is absolute and hence has a host.');
    $html_dom = Html::load($html);
    $xpath = new \DOMXpath($html_dom);
    // Update all root-relative URLs to absolute URLs in the given HTML.
    // Perform on attributes that may contain a single URI.
    foreach (static::$uriAttributes as $attr) {
        foreach ($xpath->query("//*[starts-with(@{$attr}, '/') and not(starts-with(@{$attr}, '//'))]") as $node) {
            $node->setAttribute($attr, $scheme_and_host . $node->getAttribute($attr));
        }
    }
    // Perform on each URI within "srcset" attributes.
    foreach ($xpath->query("//*[@srcset]") as $node) {
        // @see https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
        // @see https://html.spec.whatwg.org/multipage/embedded-content.html#image-candidate-string
        $image_candidate_strings = explode(',', $node->getAttribute('srcset'));
        $image_candidate_strings = array_filter(array_map('trim', $image_candidate_strings));
        foreach ($image_candidate_strings as $key => $image_candidate_string) {
            if ($image_candidate_string[0] === '/' && $image_candidate_string[1] !== '/') {
                $image_candidate_strings[$key] = $scheme_and_host . $image_candidate_string;
            }
        }
        $node->setAttribute('srcset', implode(', ', $image_candidate_strings));
    }
    return Html::serialize($html_dom);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.