Skip to content

Commit

Permalink
Dumper: added support for Dom classes in PHP 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 23, 2024
1 parent e7af752 commit ef17b90
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Tracy/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Tracy;

use Dom;
use Ds;
use Tracy\Dumper\Describer;
use Tracy\Dumper\Exposer;
Expand Down Expand Up @@ -81,6 +82,11 @@ class Dumper
\DOMNode::class => [Exposer::class, 'exposeDOMNode'],
\DOMNodeList::class => [Exposer::class, 'exposeDOMNodeList'],
\DOMNamedNodeMap::class => [Exposer::class, 'exposeDOMNodeList'],
Dom\Node::class => [Exposer::class, 'exposeDOMNode'],
Dom\NodeList::class => [Exposer::class, 'exposeDOMNodeList'],
Dom\NamedNodeMap::class => [Exposer::class, 'exposeDOMNodeList'],
Dom\TokenList::class => [Exposer::class, 'exposeDOMNodeList'],
Dom\HTMLCollection::class => [Exposer::class, 'exposeDOMNodeList'],
Ds\Collection::class => [Exposer::class, 'exposeDsCollection'],
Ds\Map::class => [Exposer::class, 'exposeDsMap'],
\WeakMap::class => [Exposer::class, 'exposeWeakMap'],
Expand Down
5 changes: 3 additions & 2 deletions src/Tracy/Dumper/Exposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
declare(strict_types=1);

namespace Tracy\Dumper;
use Dom;
use Ds;


Expand Down Expand Up @@ -141,7 +142,7 @@ public static function exposeArrayObject(\ArrayObject $obj, Value $value, Descri
}


public static function exposeDOMNode(\DOMNode $obj, Value $value, Describer $describer): void
public static function exposeDOMNode(\DOMNode|Dom\Node $obj, Value $value, Describer $describer): void
{
$props = preg_match_all('#^\s*\[([^\]]+)\] =>#m', print_r($obj, return: true), $tmp) ? $tmp[1] : [];
sort($props);
Expand All @@ -152,7 +153,7 @@ public static function exposeDOMNode(\DOMNode $obj, Value $value, Describer $des


public static function exposeDOMNodeList(
\DOMNodeList|\DOMNamedNodeMap $obj,
\DOMNodeList|\DOMNamedNodeMap|Dom\NodeList|Dom\NamedNodeMap|Dom\TokenList|Dom\HTMLCollection $obj,
Value $value,
Describer $describer,
): void
Expand Down
110 changes: 110 additions & 0 deletions tests/Tracy/Dumper.toText().specials.dom.php84.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Test: Tracy\Dumper::toText() & Dom
* @phpversion 8.4
*/

declare(strict_types=1);

use Tester\Assert;
use Tracy\Dumper;

require __DIR__ . '/../bootstrap.php';


$dom = Dom\HTMLDocument::createFromString('<!doctype html><ul><li class="a">Ahoj</ul>', Dom\HTML_NO_DEFAULT_NS);
$xpath = new Dom\XPath($dom);
$nodeList = $xpath->query('//li');
$namedNodeMap = $nodeList->item(0)->attributes;
$collection = $dom->getElementsByTagName('li');
$element = $nodeList->item(0);


Assert::match(
<<<'XX'
Dom\HTMLDocument #%d%
URL: 'about:blank'
baseURI: 'about:blank'
%A%
doctype: Dom\DocumentType #%d%
| attributes: null
| baseURI: 'about:blank'
| childNodes: %a%
| entities: Dom\DtdNamedNodeMap #%d% ...
| firstChild: null
%A%
documentElement: Dom\Element #%d%
%A%
XX,
Dumper::toText($dom, [Dumper::DEPTH => 2]),
);


Assert::match(
<<<'XX'
Dom\NodeList #%d%
length: 1
items: array (1)
| 0 => Dom\Element #%d% ...
XX,
Dumper::toText($nodeList, [Dumper::DEPTH => 2]),
);


Assert::match(
<<<'XX'
Dom\NamedNodeMap #%d%
length: 1
items: array (1)
| 'class' => Dom\Attr #%d%
| | attributes: null
%A%
XX,
Dumper::toText($namedNodeMap, [Dumper::DEPTH => 3]),
);


Assert::match(
<<<'XX'
Dom\HTMLCollection #%d%
length: 1
items: array (1)
| 'li' => Dom\Element #%d% ...
XX,
Dumper::toText($collection, [Dumper::DEPTH => 2]),
);


Assert::match(
<<<'XX'
Dom\Element #%d%
attributes: Dom\NamedNodeMap #%d% ...
baseURI: 'about:blank'
%A%
previousSibling: null
substitutedNodeValue: 'Ahoj'
tagName: 'li'
textContent: 'Ahoj'
XX,
Dumper::toText($element, [Dumper::DEPTH => 1]),
);


Assert::match(
<<<'XX'
Dom\TokenList #%d%
length: 1
items: array (1)
| 0 => 'a'
XX,
Dumper::toText($element->classList, [Dumper::DEPTH => 2]),
);


Assert::match(
<<<'XX'
Dom\XPath #%d%%A%
XX,
Dumper::toText($xpath, [Dumper::DEPTH => 1]),
);

0 comments on commit ef17b90

Please sign in to comment.