@@ -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
0 commit comments