rdx/js-dom

JS-like DOM traversal

1.10 2024-10-27 19:41 UTC

This package is auto-updated.

Last update: 2024-10-27 19:43:07 UTC


README

Uses PHP's native DOMDocument and Symfony's CssSelector.

use rdx\jsdom\Node;

$doc = Node::create(file_get_contents('pathe.html'));

// Find 1 element. Returns Node|null.
$section = $doc->query('section.schedule-simple');

// Find all elements. Returns array.
$movies = $section->queryAll('.schedule-simple__item');
foreach ($movies as $movie) {
	// Every element is a Node.
	$a = $movie->query('h4 > a');

	// Every element has an innerText/textContent.
	$title = $a->getText();

	// Attributes are array access.
	$href = $a['href'];
}

Known issues:

  • Won't select cross-current, like querySelector[All] does:
    Document: body > section > div
    Current: section
    Selector: body div
    Should match the div, but it doesn't. (jQuery doesn't either.)