@@ -171,6 +171,41 @@ public function sanitizeAttributeValue(string $value): string
171171
172172 public function refactorCondition (string $ condition ): string
173173 {
174+ $ refactoredCondition = '' ;
175+ $ charsCount = mb_strlen ($ condition , 'UTF-8 ' );
176+ $ quoteChar = null ;
177+ $ lastChar = null ;
178+ $ buffer = '' ;
179+
180+ for ($ i = 0 ; $ i < $ charsCount ; $ i ++) {
181+ $ char = mb_substr ($ condition , $ i , 1 , 'UTF-8 ' );
182+ if ($ quoteChar === null && ($ char === '" ' || $ char === '\'' )) {
183+ $ quoteChar = $ char ;
184+ if ($ buffer !== '' ) {
185+ $ refactoredCondition .= $ this ->refactorConditionPart ($ buffer );
186+ $ buffer = '' ;
187+ }
188+ $ refactoredCondition .= $ char ;
189+ } elseif ($ quoteChar === $ char && $ lastChar !== '\\' ) {
190+ $ quoteChar = null ;
191+ $ refactoredCondition .= $ char ;
192+ } else {
193+ if ($ quoteChar === null ) {
194+ $ buffer .= $ char ;
195+ } else {
196+ $ refactoredCondition .= $ char ;
197+ }
198+ }
199+ $ lastChar = $ char ;
200+ }
201+ if ($ buffer !== '' ) {
202+ $ refactoredCondition .= $ this ->refactorConditionPart ($ buffer );
203+ }
204+
205+ return $ refactoredCondition ;
206+ }
207+
208+ private function refactorConditionPart ($ condition ) {
174209 $ condition = str_replace ('=== ' , '== ' , $ condition );
175210 $ condition = str_replace ('!== ' , '!= ' , $ condition );
176211 $ condition = str_replace ('&& ' , 'and ' , $ condition );
@@ -179,6 +214,8 @@ public function refactorCondition(string $condition): string
179214 $ condition = str_replace ('.length ' , '|length ' , $ condition );
180215 $ condition = str_replace ('.trim ' , '|trim ' , $ condition );
181216
217+ // $condition = $this->convertConcat($condition);
218+
182219 foreach (Replacements::getConstants () as $ constant => $ value ) {
183220 $ condition = str_replace ($ value , Replacements::getSanitizedConstant ($ constant ), $ condition );
184221 }
@@ -188,9 +225,74 @@ public function refactorCondition(string $condition): string
188225
189226 public function refactorTextNode (string $ content ): string
190227 {
191- $ content = str_replace ('.length ' , '|length ' , $ content );
192- $ content = str_replace ('.trim ' , '|trim ' , $ content );
228+ $ refactoredContent = '' ;
229+ $ charsCount = mb_strlen ($ content , 'UTF-8 ' );
230+ $ open = false ;
231+ $ lastChar = null ;
232+ $ quoteChar = null ;
233+ $ buffer = '' ;
234+
235+ for ($ i = 0 ; $ i < $ charsCount ; $ i ++) {
236+ $ char = mb_substr ($ content , $ i , 1 , 'UTF-8 ' );
237+ if ($ open === false ) {
238+ $ refactoredContent .= $ char ;
239+ if ($ char === '{ ' && $ lastChar === '{ ' ) {
240+ $ open = true ;
241+ }
242+ } else {
243+ $ buffer .= $ char ;
244+ if ($ quoteChar === null && ($ char === '" ' || $ char === '\'' )) {
245+ $ quoteChar = $ char ;
246+ } elseif ($ quoteChar === $ char && $ lastChar !== '\\' ) {
247+ $ quoteChar = null ;
248+ }
249+ if ($ quoteChar === null && $ char === '} ' && $ lastChar === '} ' ) {
250+ $ open = false ;
251+ $ buffer = $ this ->convertTemplateString (trim ($ buffer , '} ' ));
252+ $ refactoredContent .= $ this ->refactorCondition ($ buffer ) . '}} ' ;
253+ $ buffer = '' ;
254+ }
255+ }
256+ $ lastChar = $ char ;
257+ }
193258
259+ return $ refactoredContent ;
260+ }
261+
262+ private function convertConcat ($ content ) {
263+ if (preg_match_all ('/(\S*)(\s*\+\s*(\S+))+/ ' , $ content , $ matches , PREG_SET_ORDER )) {
264+ foreach ($ matches as $ match ) {
265+ $ parts = explode ('+ ' , $ match [0 ]);
266+ $ lastPart = null ;
267+ $ convertedContent = '' ;
268+ foreach ($ parts as $ part ) {
269+ $ part = trim ($ part );
270+ if ($ lastPart !== null ) {
271+ if (is_numeric ($ lastPart ) && is_numeric ($ part )) {
272+ $ convertedContent .= ' + ' . $ part ;
273+ } else {
274+ $ convertedContent .= ' ~ ' . $ part ;
275+ }
276+ } else {
277+ $ convertedContent = $ part ;
278+ }
279+ $ lastPart = $ part ;
280+ }
281+ $ content = str_replace ($ match [0 ], $ convertedContent , $ content );
282+ }
283+ }
284+
285+ return $ content ;
286+ }
287+
288+ private function convertTemplateString ($ content ) {
289+ if (preg_match_all ('/\`([^\`]+)\`/ ' , $ content , $ matches , PREG_SET_ORDER )) {
290+ foreach ($ matches as $ match ) {
291+ $ match [1 ] = str_replace ('${ ' , '\' ~ ' , $ match [1 ]);
292+ $ match [1 ] = str_replace ('} ' , ' ~ \'' , $ match [1 ]);
293+ $ content = str_replace ($ match [0 ], '\'' . $ match [1 ] . '\'' , $ content );
294+ }
295+ }
194296 return $ content ;
195297 }
196298
0 commit comments