php-extended/php-css-selector-object

A library that implements the php-extended/php-css-selector-interface interface library.

4.0.0 2022-05-31 08:18 UTC

README

A library that implements the php-extended/php-css-selector-interface interface library.

coverage build status

Installation

The installation of this library is made via composer. Download composer.phar from their website. Then add to your composer.json :

	"require": {
		...
		"php-extended/php-css-selector-object": "^3",
		...
	}

Then run php composer.phar update to install this library. The autoloading of all classes of this library is made through composer's autoloader.

Basic Usage

To build a css-selector tree, just use the collection node and the single nodes :


use PhpExtended\Css\CssAndSelector;
use PhpExtended\Css\CssOrSelector;
use PhpExtended\Css\CssNotSelector;
use PhpExtended\Css\CssAttributeEqualsSelector;
use PhpExtended\Css\CssElementSelector;
use PhpExtended\Css\CssStateSelector;

$s = new CssElementSelector('p'); // p
$s = new CssAndSelector($s, new CssAttributeEqualsSelector('class', 'foo')); // p.foo
$s2 = new CssAttributeEqualsSelector('id', 'bar'); // #bar
$s2 = new CssAndSelector($s2, new CssAttributeEqualsSelector('data-target', 'foobar')); // #bar[data-target=foobar]
$s = new CssNotSelector($s, new CssAttributeEqualsSelector('class', 'boo')); // p.foo:not(.boo)
$s = new CssOrSelector($s, $s2); // p.foo:not(.boo), #bar[data-target=foobar]

To parse css selector data, do the following :


use PhpExtended\Css\CssSelectorParser;

$parser = new CssSelectorParser();
$node = $parser->parse('div p:not(.foo)');
// $node is now a CssAbstractSelectorInterface

License

MIT (See license file).