Search by

mensbeam / html-urlresolver

JKingweb

Resolve URLs in HTML documents

Package info

codeberg.org/mensbeam/html-urlresolver

Issues

pkg:composer/mensbeam/html-urlresolver

Statistics

Installs: 7

Dependents: 1

Suggesters: 0

1.0.1 2026-10-25 14:13 UTC

This package is auto-updated.

Last update: 2026-10-25 18:35:43 UTC


README

A simple library which resolves relative URLs in HTML DOM trees

Features

  • Supports both DOMDocument and Dom\Document trees (since PHP 8.4)
  • Handles all URL-bearing attributes in HTML, SVG, and MathML, including whitespace-separated URL-list attributes and srcset attributes
  • Honours HTML <base> elements in HTML documents

Requirements

  • PHP 7.1 or later
  • The dom extension
  • The mensbeam/uri library or other polyfill for the uri extension if using PHP versions prior to 8.5

Usage

Currently the library consists of a single static method, which will query a DOM tree for URL-bearing attributes starting from a context node (a document, element, or document fragment) and modify the tree in-place.

namespace MensBeam\HTML;

class UrlResolver {
    public static function resolveTree(\DOMDocument|\DOMElement|\DOMDocumentFragment|\Dom\Document|\Dom\Element|\Dom\DocumentFragment $node, \Uri\WhatWg\Url|string $baseUrl): void;
}

The $baseUrl argument must be an absolute URL; an exception will be thrown otherwise.

Notes

  • Since the process of resolution also involves some measure of normalization, absolute URLs already present in the document may not be identical after the resolution process
  • The presence of an (X)HTML <base> element with a relative URL in a non-HTML document (such as an SVG document) may yield unintuitive results as to that element's resolved URL; the element itself will not be factored into the base URL, however
  • The xml:base attribute is neither factored into URL resolution, nor modified during processing
  • HTML defines some attributes as containing non-empty URLs; if these attributes are in fact empty they are left unchanged during processing