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
没有合适的资源?快使用搜索试试~ 我知道了~
百度小程序仿新闻类.rar
共2000个文件
php:1443个
js:254个
html:197个
4星 · 超过85%的资源 需积分: 11 32 下载量 66 浏览量
2019-12-29
11:29:41
上传
评论 1
收藏 14.85MB RAR 举报
温馨提示
百度小程序仿新闻类原生开发(百度小程序+后台管理+使用说明),版权归于本人,想下载的伙伴在此下载;本人开发亲自测试运行无误后上传此程序,版本PHP》5.5 开发
资源推荐
资源详情
资源评论
收起资源包目录
百度小程序仿新闻类.rar (2000个子文件)
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
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
CREDITS 341B
bootstrap.min.css 123KB
bootstrap.min.css 123KB
theme.min.css 121KB
font-awesome-ie7.min.css 59KB
font-awesome-ie7.min.css 46KB
ueditor.css 43KB
ueditor.min.css 34KB
font-awesome.min.css 30KB
font-awesome.min.css 27KB
animate.css 23KB
font-awesome.min.css 22KB
video-js.css 21KB
image.css 18KB
noty.css 17KB
video.css 15KB
layer.css 14KB
attachment.css 14KB
bootstrap-datetimepicker.css 12KB
simplebootadminindex.min.css 12KB
slippry.css 11KB
video-js.min.css 11KB
style.css 11KB
style.css 9KB
blue.css 8KB
shCoreEclipse.css 8KB
default.css 8KB
shCoreDjango.css 7KB
shCoreFadeToGrey.css 7KB
html.css 7KB
shCoreDefault.css 7KB
shCoreMidnight.css 7KB
shCoreRDark.css 7KB
shCoreEmacs.css 7KB
shCoreMDUltra.css 7KB
shCoreDefault.css 7KB
jquery.ui.plupload.css 6KB
shCore.css 5KB
layer.css 5KB
upload.css 4KB
style.css 4KB
jquery.plupload.queue.css 4KB
scrawl.css 4KB
wxParse.css 4KB
install.css 3KB
colorpicker.css 3KB
codemirror.css 3KB
style.css 3KB
charts.css 3KB
shThemeEclipse.css 3KB
background.css 2KB
shThemeDjango.css 2KB
shThemeFadeToGrey.css 2KB
shThemeVisualStudio.css 2KB
shThemeDefault.css 2KB
shThemeMidnight.css 2KB
shThemeRDark.css 2KB
shThemeEmacs.css 2KB
shThemeMDUltra.css 2KB
jquery.Jcrop.min.css 2KB
index.css 2KB
shThemeAppleScript.css 2KB
emotion.css 2KB
dialogbase.css 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- leejun5792021-01-25很有参考价值, 有用
smile_fire
- 粉丝: 9
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功