No description
  • PHP 95%
  • Shell 5%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-10-25 14:13:13 -04:00
lib Resolve empty attributes unless they are defined as non-empty 2026-10-25 14:13:13 -04:00
scripts Initial imp[ementation 2026-10-24 10:12:35 -04:00
tests Resolve empty attributes unless they are defined as non-empty 2026-10-25 14:13:13 -04:00
vendor-bin/phpunit Initial imp[ementation 2026-10-24 10:12:35 -04:00
.gitignore Initial imp[ementation 2026-10-24 10:12:35 -04:00
composer.json Initial imp[ementation 2026-10-24 10:12:35 -04:00
composer.lock Initial imp[ementation 2026-10-24 10:12:35 -04:00
README.md Resolve empty attributes unless they are defined as non-empty 2026-10-25 14:13:13 -04:00

URL resolver for HTML

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