PHP CSS Parser
--------------
[![build status](https://api.travis-ci.org/sabberworm/PHP-CSS-Parser.svg)](https://travis-ci.org/sabberworm/PHP-CSS-Parser) [![HHVM Status](http://hhvm.h4cc.de/badge/sabberworm/php-css-parser.svg)](http://hhvm.h4cc.de/package/sabberworm/php-css-parser)
A Parser for CSS Files written in PHP. Allows extraction of CSS files into a data structure, manipulation of said structure and output as (optimized) CSS.
## Usage
### Installation using composer
Add php-css-parser to your composer.json
```json
{
"require": {
"sabberworm/php-css-parser": "*"
}
}
```
### Extraction
To use the CSS Parser, create a new instance. The constructor takes the following form:
```php
new Sabberworm\CSS\Parser($sText);
```
To read a file, for example, you’d do the following:
```php
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
$oCssDocument = $oCssParser->parse();
```
The resulting CSS document structure can be manipulated prior to being output.
### Options
#### Charset
The charset option is used only if no @charset declaration is found in the CSS file. UTF-8 is the default, so you won’t have to create a settings object at all if you don’t intend to change that.
```php
$oSettings = Sabberworm\CSS\Settings::create()->withDefaultCharset('windows-1252');
new Sabberworm\CSS\Parser($sText, $oSettings);
```
#### Strict parsing
To have the parser choke on invalid rules, supply a thusly configured Sabberworm\CSS\Settings object:
```php
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'), Sabberworm\CSS\Settings::create()->beStrict());
```
#### Disable multibyte functions
To achieve faster parsing, you can choose to have PHP-CSS-Parser use regular string functions instead of `mb_*` functions. This should work fine in most cases, even for UTF-8 files, as all the multibyte characters are in string literals. Still it’s not recommended to use this with input you have no control over as it’s not thoroughly covered by test cases.
```php
$oSettings = Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
new Sabberworm\CSS\Parser($sText, $oSettings);
```
### Manipulation
The resulting data structure consists mainly of five basic types: `CSSList`, `RuleSet`, `Rule`, `Selector` and `Value`. There are two additional types used: `Import` and `Charset` which you won’t use often.
#### CSSList
`CSSList` represents a generic CSS container, most likely containing declaration blocks (rule sets with a selector) but it may also contain at-rules, charset declarations, etc. `CSSList` has the following concrete subtypes:
* `Document` – representing the root of a CSS file.
* `MediaQuery` – represents a subsection of a CSSList that only applies to a output device matching the contained media query.
To access the items stored in a `CSSList` – like the document you got back when calling `$oCssParser->parse()` –, use `getContents()`, then iterate over that collection and use instanceof to check whether you’re dealing with another `CSSList`, a `RuleSet`, a `Import` or a `Charset`.
To append a new item (selector, media query, etc.) to an existing `CSSList`, construct it using the constructor for this class and use the `append($oItem)` method.
#### RuleSet
`RuleSet` is a container for individual rules. The most common form of a rule set is one constrained by a selector. The following concrete subtypes exist:
* `AtRuleSet` – for generic at-rules which do not match the ones specifically mentioned like @import, @charset or @media. A common example for this is @font-face.
* `DeclarationBlock` – a RuleSet constrained by a `Selector`; contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the matching elements.
Note: A `CSSList` can contain other `CSSList`s (and `Import`s as well as a `Charset`) while a `RuleSet` can only contain `Rule`s.
If you want to manipulate a `RuleSet`, use the methods `addRule(Rule $oRule)`, `getRules()` and `removeRule($mRule)` (which accepts either a Rule instance or a rule name; optionally suffixed by a dash to remove all related rules).
#### Rule
`Rule`s just have a key (the rule) and a value. These values are all instances of a `Value`.
#### Value
`Value` is an abstract class that only defines the `render` method. The concrete subclasses for atomic value types are:
* `Size` – consists of a numeric `size` value and a unit.
* `Color` – colors can be input in the form #rrggbb, #rgb or schema(val1, val2, …) but are always stored as an array of ('s' => val1, 'c' => val2, 'h' => val3, …) and output in the second form.
* `CSSString` – this is just a wrapper for quoted strings to distinguish them from keywords; always output with double quotes.
* `URL` – URLs in CSS; always output in URL("") notation.
There is another abstract subclass of `Value`, `ValueList`. A `ValueList` represents a lists of `Value`s, separated by some separation character (mostly `,`, whitespace, or `/`). There are two types of `ValueList`s:
* `RuleValueList` – The default type, used to represent all multi-valued rules like `font: bold 12px/3 Helvetica, Verdana, sans-serif;` (where the value would be a whitespace-separated list of the primitive value `bold`, a slash-separated list and a comma-separated list).
* `CSSFunction` – A special kind of value that also contains a function name and where the values are the function’s arguments. Also handles equals-sign-separated argument lists like `filter: alpha(opacity=90);`.
#### Convenience methods
There are a few convenience methods on Document to ease finding, manipulating and deleting rules:
* `getAllDeclarationBlocks()` – does what it says; no matter how deeply nested your selectors are. Aliased as `getAllSelectors()`.
* `getAllRuleSets()` – does what it says; no matter how deeply nested your rule sets are.
* `getAllValues()` – finds all `Value` objects inside `Rule`s.
## To-Do
* More convenience methods [like `selectorsWithElement($sId/Class/TagName)`, `attributesOfType($sType)`, `removeAttributesOfType($sType)`]
* Real multibyte support. Currently only multibyte charsets whose first 255 code points take up only one byte and are identical with ASCII are supported (yes, UTF-8 fits this description).
* Named color support (using `Color` instead of an anonymous string literal)
## Use cases
### Use `Parser` to prepend an id to all selectors
```php
$sMyId = "#my_id";
$oParser = new Sabberworm\CSS\Parser($sText);
$oCss = $oParser->parse();
foreach($oCss->getAllDeclarationBlocks() as $oBlock) {
foreach($oBlock->getSelectors() as $oSelector) {
//Loop over all selector parts (the comma-separated strings in a selector) and prepend the id
$oSelector->setSelector($sMyId.' '.$oSelector->getSelector());
}
}
```
### Shrink all absolute sizes to half
```php
$oParser = new Sabberworm\CSS\Parser($sText);
$oCss = $oParser->parse();
foreach($oCss->getAllValues() as $mValue) {
if($mValue instanceof CSSSize && !$mValue->isRelative()) {
$mValue->setSize($mValue->getSize()/2);
}
}
```
### Remove unwanted rules
```php
$oParser = new Sabberworm\CSS\Parser($sText);
$oCss = $oParser->parse();
foreach($oCss->getAllRuleSets() as $oRuleSet) {
$oRuleSet->removeRule('font-'); //Note that the added dash will make this remove all rules starting with font- (like font-size, font-weight, etc.) as well as a potential font-rule
$oRuleSet->removeRule('cursor');
}
```
### Output
To output the entire CSS document into a variable, just use `->render()`:
```php
$oCssParser = new Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
$oCssDocument = $oCssParser->parse();
print $oCssDocument->render();
```
If you want to format the output, pass an instance of type `Sabberworm\CSS\OutputFormat`:
```php
$oFormat = Sabberworm\CSS\OutputFormat::create()->indentWithSpaces(4)->setSpaceBetweenRules("\n");
print $oCssDocument->render($oFormat);
```
Or use one
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码-少儿编程系统源码.zip
共2000个文件
php:1331个
gif:230个
txt:150个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 48 浏览量
2022-11-22
15:55:33
上传
评论 1
收藏 18.57MB ZIP 举报
温馨提示
PHP实例开发源码—少儿编程系统源码.zip PHP实例开发源码—少儿编程系统源码.zip PHP实例开发源码—少儿编程系统源码.zip
资源推荐
资源详情
资源评论
收起资源包目录
PHP实例开发源码-少儿编程系统源码.zip (2000个子文件)
.htaccess_apache2.4 53B
Helvetica-Oblique.afm 73KB
Helvetica.afm 73KB
Helvetica-BoldOblique.afm 68KB
Helvetica-Bold.afm 68KB
Times-Italic.afm 65KB
Times-Bold.afm 63KB
Times-Roman.afm 59KB
Times-BoldItalic.afm 58KB
Courier-Oblique.afm 15KB
Courier-BoldOblique.afm 15KB
Courier-Bold.afm 15KB
Courier.afm 15KB
Symbol.afm 10KB
ZapfDingbats.afm 9KB
step1.php.bak 4KB
test.bmp 0B
ReaderWriter.cd 6KB
Architecture.cd 2KB
CHANGELOG 1KB
Changelog 939B
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
config 1KB
COPYING 7KB
CREDITS 341B
PHPExcel_Writer_Serialized.cs 2KB
PHPExcel_Reader_Excel5.cs 1KB
PHPExcel_Reader_Serialized.cs 972B
PHPExcel_IOFactory.cs 853B
PHPExcel.cs 822B
PHPExcel_Writer_Excel2007.cs 537B
PHPExcel_Reader_Excel2007.cs 536B
IWriter.cs 231B
IReader.cs 230B
Worksheet.cs 181B
ClassDiagrams.csproj 3KB
layui.css 71KB
layui.css 68KB
layui.css 59KB
fgx.css 20KB
layer.css 14KB
layer.css 14KB
layer.css 14KB
theme.css 14KB
install.css 11KB
template.css 11KB
layui.mobile.css 10KB
layui.mobile.css 10KB
layui.mobile.css 10KB
shCoreEclipse.css 8KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
html.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
laydate.css 7KB
laydate.css 7KB
laydate.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
shCore.css 5KB
style.css 4KB
index.css 4KB
shThemeEclipse.css 3KB
shThemeDjango.css 2KB
shThemeFadeToGrey.css 2KB
login.css 2KB
shThemeVisualStudio.css 2KB
shThemeDefault.css 2KB
shThemeMidnight.css 2KB
shThemeRDark.css 2KB
shThemeEmacs.css 2KB
shThemeMDUltra.css 2KB
shThemeAppleScript.css 2KB
atrules.css 1KB
shequ.css 1KB
code.css 1KB
code.css 1KB
code.css 1KB
switch.css 904B
project.css 896B
line-numbers.css 753B
important.css 684B
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
毕业_设计
- 粉丝: 1921
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功