powertools / dom-query
PHP PowerTools DOM-Query Component
dev-master
2017-10-13 16:24 UTC
Requires
- php: >=5.4.0
- powertools/html5: 1.0
- symfony/css-selector: 2.0
This package is not auto-updated.
Last update: 2024-11-05 08:35:38 UTC
README
PHPPowertools is a web application framework for PHP >= 5.4.
PHPPowertools/DOM-Query is the first component of the PHPPowertools that has been released to the public.
The purpose of this component is to provide a jQuery-like interface for crawling XML and HTML documents. Under the hood, it uses symfony/CssSelector for converting CSS selectors to XPath queries
The jQuery way :
// Find the elements that match selector 'div.foo' $s = $('div.foo'); // Pass an element object (DOM Element) $s = $(document.body); // Pass a jQuery object $s = $($('p + p'));
The DOM-Query way :
namespace App; use \PowerTools\DOM_Query; // Get file content $htmlcode = file_get_contents('https://github.com'); // Create a new DOM_Query instance, using a string as a source $H = new DOM_Query($htmlcode); // Create a new DOM_Query instance, using an existing DOM_Query instance as a source $H = new DOM_Query($H->select('body')); // Find the elements that match selector 'div.foo' $s = $H->select('div.foo'); // Pass an element object (DOM Element) $s = $H->select($documentBody); // Pass a DOM Query instance $s = $H->select($H->select('p + p'));
Example use :
// Select the body tag $body = $H->select('body'); // Combine different classes as one selector to get all site blocks $siteblocks = $body->select('.site-header, .masthead, .site-body, .site-footer'); // Nest your methods just like you would with jQuery $siteblocks->select('button')->add('span')->addClass('icon icon-printer'); // Use a lambda function to set the text of all site blocks $siteblocks->text(function($i, $val) { return $i . " - " . $val->attr('class'); }); // Append the following HTML to all site blocks $siteblocks->append('<div class="site-center"></div>'); // Use a descendant selector to select the site's footer $sitefooter = $body->select('.site-footer > .site-center'); // Set some attributes for the site's footer $sitefooter->attr(array('id' => 'aweeesome', 'data-val' => 'see')); // Use a lambda function to set the attributes of all site blocks $siteblocks->attr('data-val', function($i, $val) { return $i . " - " . $val->attr('class') . " - photo by Kelly Clark"; }); // Select the parent of the site's footer $sitefooterparent = $sitefooter->parent(); // Remove the class of all i-tags within the site's footer's parent $sitefooterparent->select('i')->removeAttr('class'); // Wrap the site's footer within two nex selectors $sitefooter->wrap('<section><div class="footer-wrapper"></div></section>'); [...]
Supported methods :
- $ (1)
- $.parseHTML
- $.parseXML
- $.parseJSON
- $selection.add
- $selection.addClass
- $selection.after
- $selection.append
- $selection.attr
- $selection.before
- $selection.children
- $selection.closest
- $selection.contents
- $selection.detach
- $selection.each
- $selection.eq
- $selection.empty (2)
- $selection.find
- $selection.first
- $selection.get
- $selection.hasClass
- $selection.insertAfter
- $selection.insertBefore
- $selection.is
- $selection.last
- $selection.parent
- $selection.parents
- $selection.remove
- $selection.removeAttr
- $selection.removeClass
- $selection.text
- $selection.wrap
- Renamed 'select', for obvious reasons
- Renamed 'void', since 'empty' is a reserved word in PHP