webforge-dom
A very very very very very simple aproach to build an api like jquery for html (testing) in php.
It provides some helper for DOM*** Classes in PHP (that were difficult to debug before)., (*1)
uses the symfony CSSSelector class, (*2)
installation
Use Composer to install., (*3)
composer require -v --prefer-source webforge/dom:1.0.*
to run the tests use:, (*4)
phpunit
usage
Lets say, we're operating on this html:, (*5)
Please Login
Reset your password, (*6)
$hiddenInput = Query::create('form.main', $this->html)->find('input[name="submitted"]');
// returns an instanceof Query with the html: <input type="hidden" name="submitted" value="true" />
$fieldsetUserData = Query::create('form.main', $this->html)->find('fieldset')->eq(0);
returns an instanceof Query with the html: <fieldset class="user-data group"><input .. <br /><input ..<br /></fieldset>
, (*7)
$url = Query::create('a', '<a href="http://www.ps-webforge.com" class="def"></a>')->attr('href');
// 'http://www.ps-webforge.com'
works like the jquery attr
, (*8)
$innerHtml = Query::create('fieldset:first', $this->html)->html();
// '<input type="text" name="email" value="" /><br /><br /><input type="text" name="name" value="" /><br />'
Returns the html from all children combined., (*9)
$html = Query::create('fieldset:first [name="email"]', $this->html)->outerHtml();
// '<input type="text" name="email" value="" />'
Returns the html from the element and all its children combined., (*10)
Note: The output from outerHtml() and html() is not exactly identical to the parts in the original html, because it is reformatted internally by the PHP-DOM functions., (*11)
$true = Query::create('fieldset:first', $this->html)->hasClass('user-data');
$true = Query::create('fieldset:first', $this->html)->hasClass('group');
$false = Query::create('fieldset:first', $this->html)->hasClass('user-data.group');
Checks if the element has a specific, single class., (*12)
issues
- a selector like
something:eq(0)
does not exactly behave like it would be in jquery. Currently it is rewritten to :nth-of-type(0). But this is not exactly the same
- it's currently not possible to do a find() on a set of matched elements (PR welcome)
- hasClass does not work with combined classes like:
user-data.group