rdx/js-dom

JS-like DOM traversal

1.11 2024-12-19 16:05 UTC

This package is auto-updated.

Last update: 2024-12-19 16:09:54 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.)