# PHP CSS Parser
[![Build Status](https://github.com/sabberworm/PHP-CSS-Parser/workflows/CI/badge.svg?branch=master)](https://github.com/sabberworm/PHP-CSS-Parser/actions/)
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
```bash
composer 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($css);
```
To read a file, for example, you’d do the following:
```php
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
$cssDocument = $parser->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
$settings = \Sabberworm\CSS\Settings::create()
->withDefaultCharset('windows-1252');
$parser = new \Sabberworm\CSS\Parser($css, $settings);
```
#### Strict parsing
To have the parser choke on invalid rules, supply a thusly configured `\Sabberworm\CSS\Settings` object:
```php
$parser = 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 using this with input you have no control over as it’s not thoroughly covered by test cases.
```php
$settings = \Sabberworm\CSS\Settings::create()->withMultibyteSupport(false);
$parser = new \Sabberworm\CSS\Parser($css, $settings);
```
### 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 an output device matching the contained media query.
To access the items stored in a `CSSList` – like the document you got back when calling `$parser->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 $rule)`, `getRules()` and `removeRule($rule)` (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($type)`, `removeAttributesOfType($type)`)
* 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
$myId = "#my_id";
$parser = new \Sabberworm\CSS\Parser($css);
$cssDocument = $parser->parse();
foreach ($cssDocument->getAllDeclarationBlocks() as $block) {
foreach ($block->getSelectors() as $selector) {
// Loop over all selector parts (the comma-separated strings in a
// selector) and prepend the ID.
$selector->setSelector($myId.' '.$selector->getSelector());
}
}
```
### Shrink all absolute sizes to half
```php
$parser = new \Sabberworm\CSS\Parser($css);
$cssDocument = $parser->parse();
foreach ($cssDocument->getAllValues() as $value) {
if ($value instanceof CSSSize && !$value->isRelative()) {
$value->setSize($value->getSize() / 2);
}
}
```
### Remove unwanted rules
```php
$parser = new \Sabberworm\CSS\Parser($css);
$cssDocument = $parser->parse();
foreach($cssDocument->getAllRuleSets() as $oRuleSet) {
// 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('font-');
$oRuleSet->removeRule('cursor');
}
```
### Output
To output the entire CSS document into a variable, just use `->render()`:
```php
$parser = new \Sabberworm\CSS\Parser(file_get_contents('somefile.css'));
$cssDocument = $parser->parse();
print $cssDocument->render();
```
If you want to format the output, pass an instance of type `\Sabberworm\CSS\OutputFormat`:
```php
$format = \Sabberworm\CSS\OutputFormat::create()
->indentWithSpaces(4)->setSpaceBetweenRules("\n");
print $cssDocument->render($format);
```
Or use one of the predefined formats:
```php
print
没有合适的资源?快使用搜索试试~ 我知道了~
【WordPress插件】2022年最新版完整功能demo+插件v3.9.16.zip
共440个文件
php:311个
map:21个
afm:14个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 18 浏览量
2022-04-14
05:18:55
上传
评论
收藏 5.63MB ZIP 举报
温馨提示
"【WordPress插件】2022年最新版完整功能demo+插件v3.9.16 WooCommerce PDF Product Vouchers Woocommerce PDF产品凭证" ---------- 泰森云每天更新发布最新WordPress主题、HTML主题、WordPress插件、shopify主题、opencart主题、PHP项目源码、安卓项目源码、ios项目源码,更有超10000个资源可供选择,如有需要请站内联系。
资源推荐
资源详情
资源评论
收起资源包目录
【WordPress插件】2022年最新版完整功能demo+插件v3.9.16.zip (440个子文件)
Helvetica-Oblique.afm 76KB
Helvetica.afm 76KB
Helvetica-BoldOblique.afm 71KB
Helvetica-Bold.afm 71KB
Times-Italic.afm 68KB
Times-Bold.afm 65KB
Times-Roman.afm 62KB
Times-BoldItalic.afm 61KB
Courier-Oblique.afm 16KB
Courier-BoldOblique.afm 16KB
Courier-Bold.afm 15KB
Courier.afm 15KB
Symbol.afm 10KB
ZapfDingbats.afm 10KB
COPYING 7KB
wc-pdf-product-vouchers.min.css 25KB
html.css 8KB
wc-pdf-product-vouchers-customizer-controls.min.css 6KB
sv-wc-plugin-admin-setup-wizard.min.css 3KB
wc-pdf-product-vouchers.min.css 488B
wc-pdf-product-vouchers-customizer-preview.min.css 204B
ajax-loader.gif 885B
COPYING.GPL 34KB
mustRead.html 1KB
jsQR.js 248KB
jsQR.min.js 126KB
wc-pdf-product-vouchers.min.js 22KB
wc-pdf-product-vouchers-customizer-controls.min.js 10KB
wc-pdf-product-vouchers-customizer-preview.min.js 3KB
wc-pdf-product-vouchers-barcode-scan.min.js 3KB
sv-wp-admin-job-batch-handler.min.js 2KB
wc-pdf-product-vouchers.min.js 999B
sv-wc-plugin-admin-setup-wizard.min.js 978B
installed.json 10KB
bower.json 525B
LICENSE.LGPL 24KB
LICENSE 24KB
LICENSE 1KB
LICENSE 1KB
adobe-standard-encoding.map 8KB
cp1251.map 5KB
koi8-r.map 5KB
koi8-u.map 5KB
iso-8859-5.map 5KB
iso-8859-11.map 5KB
iso-8859-16.map 5KB
iso-8859-9.map 5KB
iso-8859-1.map 5KB
iso-8859-4.map 5KB
iso-8859-15.map 5KB
iso-8859-2.map 5KB
cp1250.map 5KB
cp1252.map 5KB
cp1254.map 5KB
cp1258.map 5KB
iso-8859-7.map 5KB
cp1257.map 5KB
cp1255.map 4KB
cp1253.map 4KB
cp874.map 4KB
README.md 20KB
CHANGELOG.md 8KB
README.md 8KB
CONTRIBUTING.md 3KB
README.md 1KB
README.md 1KB
woocommerce-pdf-product-vouchers-it_IT.mo 44KB
woocommerce-pdf-product-vouchers-de_DE_formal.mo 39KB
woocommerce-pdf-product-vouchers-de_DE.mo 38KB
woocommerce-plugin-framework-et.mo 19KB
Cpdf.php 214KB
TreeBuilder.php 191KB
CPdf.php 167KB
barcode.php 118KB
Tokenizer.php 113KB
Style.php 102KB
Stylesheet.php 63KB
class-wc-voucher.php 57KB
PDFLib.php 47KB
Dompdf.php 41KB
class-wc-pdf-product-vouchers-upgrade.php 36KB
class-sv-wc-plugin.php 36KB
abstract-sv-wc-plugin-admin-setup-wizard.php 34KB
CPDF.php 33KB
class-wc-pdf-product-vouchers.php 33KB
Block.php 33KB
Helpers.php 32KB
AbstractRenderer.php 32KB
class-wc-pdf-product-vouchers-redemption-handler.php 31KB
Frame.php 31KB
class-wc-pdf-product-vouchers-customizer.php 31KB
class-sv-wc-helper.php 30KB
DeclarationBlock.php 29KB
GD.php 29KB
class-wc-pdf-product-vouchers-handler.php 29KB
class-wc-pdf-product-vouchers-ajax.php 28KB
class-sv-wp-background-job-handler.php 27KB
class-wc-voucher-template.php 27KB
Options.php 26KB
Cellmap.php 23KB
共 440 条
- 1
- 2
- 3
- 4
- 5
资源评论
Lee达森
- 粉丝: 1192
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功