Skip to content

Commit 71f6c6f

Browse files
committed
document::parse() can now handle <![CDATA[ tags wrapping the CSS.
1 parent 7e06e7d commit 71f6c6f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/cssdoc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class cssdoc extends config implements \ArrayAccess, \Iterator {
1111
protected static array $tokens = [
1212
'whitespace' => '\s++',
1313
'comment' => '\\/\\*[\d\D]*?\\*\\/',
14+
'cdataopen' => '<\!\[CDATA\[',
15+
'cdataclose' => '\]\]>',
1416
'quotes' => '(?<!\\\\)(?:"(?:[^"\\\\]++|\\\\.)*+"|\'(?:[^\'\\\\]++|\\\\.)*+\')',
1517
'comparison' => '[\\^*$<>]?=', // comparison operators for media queries or attribute selectors
1618
'join' => '[>+~*\\/]|-(?!-)',

src/tokens/document.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ class document {
1515
*/
1616
public array $rules = [];
1717

18+
/**
19+
* @var bool Whether to wrap in CDATA tags
20+
*/
21+
public bool $cdata = false;
22+
1823
/**
1924
* Constructs the comment object
2025
*
@@ -36,6 +41,9 @@ public function parse(tokenise $tokens) : bool {
3641
// parse tokens
3742
while (($token = $tokens->next()) !== null) {
3843
switch ($token['type']) {
44+
case 'cdataopen':
45+
$this->cdata = true;
46+
break;
3947
case 'directive':
4048
$item = new directive($this->root);
4149
$item->parse($tokens);
@@ -46,6 +54,7 @@ public function parse(tokenise $tokens) : bool {
4654
break 2;
4755
case 'comment':
4856
case 'whitespace':
57+
case 'cdataclose':
4958
break;
5059
default:
5160
$item = new rule($this->root);
@@ -83,15 +92,15 @@ public function minify(array $minify) : void {
8392
*/
8493
public function compile(array $options) : string {
8594
$b = $options['style'] !== 'minify';
86-
$css = '';
95+
$css = $this->cdata ? '<![CDATA[' : '';
8796

8897
// compile selectors
8998
$join = '';
9099
foreach ($this->rules AS $item) {
91100
$css .= $join.$item->compile($options);
92101
$join = $b ? "\n\n" : '';
93102
}
94-
return $css;
103+
return $css.($this->cdata ? ']]>' : '');
95104
}
96105

97106
/**

0 commit comments

Comments
 (0)