Skip to content

xprt64/php-css

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Css

A PHP Library used to parse CSS and select elements from a DOMDocument

Contains code from github.com/tj/php-selector (the selector part)

  • Composer enabled

  • Contains PHPUnit tests

Examples

Given the folowing PHP code

$dom	=	new \DOMDocument();

$dom->loadHTML(<<<HTMLCODE
<html>
	<head>
		<style type="text/css">
			div.class1
			{
				color: #ff000;
			}
		</style>
		<style type="text/css">
			p.class2
			{
				text-align:center;
			}
			/* a comment the must be ignored by the parser */	
			/* the next , intentionally put here, the parser should ignore it */
			,p span
			{
				font-weight:bold;
			}
			a,
			span.link
			{
				cursor: pointer;
			}
		</style>
 	</head>
	<body>
		<div class="class1">test1</div>
		<p class="class2 class1">test1 <span class="link">in span</span></p>
   </body>
</html>
HTMLCODE
);

Selecting elements

Select elements like this:

Selector::selectElements("div", $dom)

This should return a DOMNodeList with one element.

Selector::selectElements(".class1,.class2", $dom)

This should return a DOMNodeList with 2 elements.

Applying CSS styles

$parser = new	Parser($dom);

$parser->loadRulesFromDom();

$span = $this->dom->getElementsByTagName('span')[0];

$span_styles = $parser->getStylesFromCssRules($span);

styles should contain 2 CSS styles:

font-weight:bold;
cursor: pointer;

*

Contribution

*

More Information

License

(The MIT License)

Copyright © Constantin GALBENU <xprt64@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

PHP DOM parser / queries with CSS selectors

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%