-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSSParser.php
More file actions
89 lines (76 loc) · 2.21 KB
/
CSSParser.php
File metadata and controls
89 lines (76 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
require_once 'PEG.php';
require_once dirname(__FILE__) . '/CSSParser/CSSPEG.php';
require_once dirname(__FILE__) . '/CSSParser/Locator.php';
require_once dirname(__FILE__) . '/CSSParser/Block.php';
require_once dirname(__FILE__) . '/CSSParser/NodeCreater.php';
require_once dirname(__FILE__) . '/CSSParser/Node.php';
require_once dirname(__FILE__) . '/CSSParser/RuleSet.php';
require_once dirname(__FILE__) . '/CSSParser/AtRule.php';
require_once dirname(__FILE__) . '/CSSParser/FontFace.php';
require_once dirname(__FILE__) . '/CSSParser/Page.php';
require_once dirname(__FILE__) . '/CSSParser/Error.php';
require_once dirname(__FILE__) . '/CSSParser/IValidate.php';
require_once dirname(__FILE__) . '/CSSParser/AValidate.php';
class CSSParser
{
static protected $css;
/**
* cssをパースして構造木を返す
*
* @param string $css string
*
* @return CSSParser_Node
*/
static function parse($css)
{
self::$css = self::cssClean($css);
$result = CSSParser_Locator::it()->parser->parse(CSSPEG::context(self::$css));
return $result;
}
/**
* cssをパースして、有効なものだけを返す。
*
* @param string $css css
* @param string $type type
*
* @return CSSParser_Node
*/
/*static function validate($css, $type = null)
{
$node = self::parse($css);
$validator = self::factory($type);
return $validator->validate($node);
}*/
/**
* 指定した種類のインスタンスを返す。
* 指定しなければ、css2.1のパーサインスタンスを返す。
*
* @param string $type string
*
* @return object
*/
static protected function factory($type = null)
{
$type = $type === null ? 'CSS21' : $type;
$dirPath = dirname(__FILE__);
$subDirName = __CLASS__;
$subDirPath = $dirPath . '/' . $subDirName;
if (!file_exists($subDirPath . '/' . $type . '.php')) throw new InvalidArgumentException;
if (!include_once $subDirPath . '/' . $type . '.php') {
throw new Exception('fail load file');
}
return $o = new $type(self::$css);
}
/**
* cssの改行コード等を統一する
*
* @param string $css css
*
* @return string
*/
static public function cssClean($css)
{
return rtrim(str_replace(array("\r\n", "\r"), "\n", $css));
}
}