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
没有合适的资源?快使用搜索试试~ 我知道了~
Thinkphp仿众图网图素材下载站源码
共2000个文件
php:2201个
png:337个
html:331个
需积分: 5 5 下载量 69 浏览量
2022-09-12
20:44:52
上传
评论
收藏 47.26MB ZIP 举报
温馨提示
Thinkphp仿众图网图素材下载站源码
资源推荐
资源详情
资源评论
收起资源包目录
Thinkphp仿众图网图素材下载站源码 (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
test.bmp 0B
ReaderWriter.cd 6KB
ReaderWriter.cd 6KB
Architecture.cd 2KB
Architecture.cd 2KB
CHANGELOG 1KB
Changelog 939B
nginx.conf 3KB
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
config 322B
config 293B
config 280B
config 263B
config 263B
config 252B
config 247B
config 241B
config 241B
config 239B
config 239B
config 238B
config 238B
config 237B
config 237B
config 236B
config 235B
config 71B
COPYING 7KB
CREDITS 341B
PHPExcel_Writer_Serialized.cs 2KB
PHPExcel_Writer_Serialized.cs 2KB
PHPExcel_Reader_Excel5.cs 1KB
PHPExcel_Reader_Excel5.cs 1KB
PHPExcel_Reader_Serialized.cs 972B
PHPExcel_Reader_Serialized.cs 972B
PHPExcel_IOFactory.cs 853B
PHPExcel_IOFactory.cs 853B
PHPExcel.cs 822B
PHPExcel.cs 822B
PHPExcel_Writer_Excel2007.cs 537B
PHPExcel_Writer_Excel2007.cs 537B
PHPExcel_Reader_Excel2007.cs 536B
PHPExcel_Reader_Excel2007.cs 536B
IWriter.cs 231B
IWriter.cs 231B
IReader.cs 230B
IReader.cs 230B
Worksheet.cs 181B
Worksheet.cs 181B
ClassDiagrams.csproj 3KB
ClassDiagrams.csproj 3KB
yunmasucaivip.css 352KB
bootstrap.css 134KB
bootstrap.min.css 123KB
bootstrap.min.css 123KB
bootstrap.min.css 123KB
bootstrap.min.css 122KB
theme.min.css 121KB
bootstrap.min.css 118KB
yunmasucai.com.css 87KB
common.css 62KB
font-awesome-ie7.min.css 59KB
font-awesome-ie7.min.css 59KB
font-awesome-ie7.min.css 46KB
font-awesome-ie7.min.css 46KB
ueditor.css 43KB
member.css 39KB
ueditor.min.css 34KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
杨亦涛
- 粉丝: 68
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于arduino开发的blockly图形化编程软件.zip
- LOGO检测-基于DETR目标检测算法实现的LOGO标志检测识别-附流程教程+项目源码-优质项目实战.zip
- bnaidr.plist
- 华中农业大学python-Day36-40.rar
- 华中农业大学pythonpython-Day31-35.rar
- 服务器概述服务器概述服务器概述服务器概述.txt
- 华中农业大学python实验题.txt
- 海康威视相机采图交叉编译示例程序,c++
- DETR-基于Tensorflow实现DETR目标检测算法-附流程教程+项目源码-优质项目实战.zip
- 3d激光slam地图发布程序,3d地图点云处理,c++程序
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功