No description
- PHP 95%
- Shell 5%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| lib | ||
| scripts | ||
| tests | ||
| vendor-bin/phpunit | ||
| .gitignore | ||
| composer.json | ||
| composer.lock | ||
| README.md | ||
URL resolver for HTML
A simple library which resolves relative URLs in HTML DOM trees
Features
- Supports both
DOMDocumentandDom\Documenttrees (since PHP 8.4) - Handles all URL-bearing attributes in HTML, SVG, and MathML, including whitespace-separated URL-list attributes and
srcsetattributes - Honours HTML
<base>elements in HTML documents
Requirements
- PHP 7.1 or later
- The
domextension - The
mensbeam/urilibrary or other polyfill for theuriextension 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:baseattribute 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