Skip to content

Commit 7a15fab

Browse files
committed
fix background parser
1 parent fad0833 commit 7a15fab

2 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/Parse/ParseCommaList.php

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,56 @@ protected function parseSingleValue(string $value)
5959
}
6060
}
6161

62+
/**
63+
* a function to split multi backgrounds
64+
* by @chatgpt
65+
*
66+
* @param string $input
67+
* @return array
68+
*/
69+
protected function splitCssValues(string $input): array {
70+
preg_match('/\s*([^;]+)/', $input, $matches);
71+
72+
if (empty($matches)) {
73+
return [];
74+
}
75+
76+
$backgroundString = $matches[1];
77+
78+
$backgrounds = [];
79+
$level = 0;
80+
$current = '';
81+
82+
for ($i = 0; $i < strlen($backgroundString); $i++) {
83+
$char = $backgroundString[$i];
84+
85+
if ($char === '(') {
86+
$level ++;
87+
} elseif ($char === ')') {
88+
$level --;
89+
} elseif ($char === ',' && $level === 0) {
90+
$backgrounds[] = trim($current);
91+
$current = '';
92+
93+
continue;
94+
}
95+
96+
$current .= $char;
97+
}
98+
99+
if (!empty($current)) {
100+
$backgrounds[] = trim($current);
101+
}
102+
103+
return $backgrounds;
104+
}
105+
62106
/**
63107
* parse values and list
64108
*/
65109
public function parse(): void
66110
{
67-
$pattern = '/\s*([^,(]+(?:\([^()]*\))?[^,()]*(?:\([^()]*\)[^,()]*)*)\s*(?=,|$)/';
68-
69-
preg_match_all($pattern, $this->text, $matches);
70-
71-
$exp_comma = array_map('trim', $matches[0]);
111+
$exp_comma = $this->splitCssValues($this->text);
72112

73113
foreach ($exp_comma as $i) {
74114
$i_code = '.wapper { box-shadow: ' . $i . '; }';
@@ -80,9 +120,15 @@ public function parse(): void
80120
continue;
81121
}
82122

83-
$rule_value = $parse_tree->getContents()[0]->getRules()[0]->getValue() ?? null;
123+
if ($parse_tree->getContents()[0]) {
124+
$content = $parse_tree->getContents()[0];
84125

85-
$this->list->addListComponent($rule_value);
126+
if (isset($content->getRules()[0])) {
127+
$rule_value = $parse_tree->getContents()[0]->getRules()[0]->getValue() ?? '';
128+
129+
$this->list->addListComponent($rule_value);
130+
}
131+
}
86132
}
87133
}
88134

tests/data/bg.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@
5353
"should": "Should mirror horizontal position expressed in length unit (two-value syntax)",
5454
"expected": ".banner { background: right 20px top 30px url(\"topbanner.png\") #00d repeat-y fixed; }",
5555
"input": ".banner { background: 20px 30px url(topbanner.png) #00d repeat-y fixed; }"
56+
},
57+
{
58+
"should": "Should parse difficult backgrounds",
59+
"expected": "div {background: padding-box linear-gradient(var(--button-bg),var(--button-bg)),border-box linear-gradient(rgba(255,255,255,.08) 90%,rgba(0,0,0,.4) 100%),border-box var(--button-bg);}",
60+
"input": "div {background: padding-box linear-gradient(var(--button-bg),var(--button-bg)),border-box linear-gradient(rgba(255,255,255,.08) 90%,rgba(0,0,0,.4) 100%),border-box var(--button-bg);}"
5661
}
5762
]

0 commit comments

Comments
 (0)