[TikiWiki-commits] [Git][tikiwiki/tiki][27.x] [BP][FIX] Fix undefined array key errors in IconGenerator class

"Victor Emanouilov \(@kroky\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <69ea1f91ad0b3_3818d10c6283@gitlab-sidekiq-low-urgency-cpu-bound-v2-75c97c766c-d6rmc.mail>

Victor Emanouilov pushed to branch 27.x at Tiki Wiki CMS Groupware / Tiki


Commits:
6457e4d4 by Adrien Mbuya Maloba at 2026-04-23T13:25:07+00:00
[BP][FIX] Fix undefined array key errors in IconGenerator class
---
* [FIX] Remove unnecessary null coalescing fallback when assigning
---
* [FIX] Remove unnecessary null coalescing fallback when assigning

See merge request tikiwiki/tiki!7910

* Fix undefined array key errors in IconGenerator class
---
* Refactor: improve unicodePattern readability, compress css before matching

* [FIX] Improve regex patterns for font-awesome icon extraction

* Use correct variable name in Exception message

$fontAwesomePattern => $fontAwesomeUnicodePattern

* Fix: Skip empty font-awesome Unicode values

* [FIX] Update Font Awesome regex for v6 format to restore icon detection

Font Awesome 6 changed its CSS structure, breaking the existing regex patterns
used in the icon generator. This caused a fatal error during the
step.

Updated the regex patterns to correctly match icon class names and unicodes in FA6.

* [ENH] IconGenerator.php:  Don't assume that the regexes work, make errors fatal.  Most likely the fontawesome css file format changed

* [FIX] Fix undefined array key errors in IconGenerator
- Add null coalescing operators to handle empty regex matches
- Protect array access when extracting FontAwesome unicode values
- Prevents crashes when CSS files don't contain expected icon patterns

See merge request tikiwiki/tiki!7632

See merge request tikiwiki/tiki!10066

- - - - -


1 changed file:

- lib/IconGenerator.php


Changes:

=====================================
lib/IconGenerator.php
=====================================
@@ -37,10 +37,17 @@ class IconGenerator
 
     public function generateIconArraysFromCss($bootstrapIcons, $fontAwesomeIcons): array
     {
+        //var_dump($fontAwesomeIcons);
         $bootstrapPattern = '/\.bi-([a-zA-Z0-9-]+)::before/';
-        $fontAwesomePattern = '/\.fa-([a-zA-Z0-9-]+)(::|:)before/';
-        //pattern to extract unicodes from file
-        $fontAwesomeUnicodePattern = '#content: "\\\(.*?)"; }#';
+        /**
+         * faClassPattern matches a selector starting with .fa- and faUnicodePattern
+         * asserts that within the upcoming block there exists a '--fa' property
+         * with the desired unicode value. We combine icon name and unicode value
+         * search to avoid non-icon classes from all.css as all of them start by .fa-
+        */
+        $faClassPattern = '\.fa-([\w-]+)';
+        $faUnicodePattern = '{--fa:["\']\\\\([a-f0-9]{2,5})["\'];}';
+        $fontAwesomePattern = "/{$faClassPattern}{$faUnicodePattern}/s";
 
         $bootstrapFinal = [];
         $fontAwesomeFinal = [];
@@ -50,7 +57,10 @@ class IconGenerator
         // Extract Bootstrap icons
         if ($bootstrapIcons) {
             $bootstrapMatches = [];
-            preg_match_all($bootstrapPattern, $bootstrapIcons, $bootstrapMatches);
+            $numMatches = preg_match_all($bootstrapPattern, $bootstrapIcons, $bootstrapMatches);
+            if ($numMatches < 1) {
+                throw new Exception("Unable to find any icons with pattern: $bootstrapPattern");
+            }
 
             $bootstrapResult = $bootstrapMatches[1];
             $bootstrapPhp = "<?php\n    global \$prefs; \n       \$prefs['bs_generated_icons'] = [";
@@ -66,28 +76,30 @@ class IconGenerator
         // Extract Font Awesome icons
         if ($fontAwesomeIcons) {
             $fontAwesomeMatches = [];
-            preg_match_all($fontAwesomePattern, $fontAwesomeIcons, $fontAwesomeMatches);
-            preg_match_all($fontAwesomeUnicodePattern, $fontAwesomeIcons, $fontAwesomeUnicodeMatches);
+
+            $numMatchesFA = preg_match_all($fontAwesomePattern, preg_replace('/\s+/', '', $fontAwesomeIcons), $fontAwesomeMatches);
+            if ($numMatchesFA < 1) {
+                throw new Exception("Unable to find any icons with pattern: $fontAwesomePattern");
+            }
+
             $fontAwesomeResult = $fontAwesomeMatches[1];
-            $fontAwesomeUnicodeResult = $fontAwesomeUnicodeMatches[1];
+            $fontAwesomeUnicodeResult = $fontAwesomeMatches[2];
+
             $fontAwesomePhp = "<?php\n    global \$prefs; \n      \$prefs['fa_generated_icons'] = [";
-            $line = 0;
-            foreach ($fontAwesomeResult as $value) {
-                $name = str_replace('-', '_', $value);
+            foreach ($fontAwesomeResult as $index => $iconName) {
+                $name = str_replace('-', '_', $iconName);
                 $fontAwesomeFinal[$name] = [
-                'id' => $value,
+                'id' => $iconName,
                 'prepend' => 'fas fa-',
-                'codeValue' => '#x' . $fontAwesomeUnicodeResult[$line]
+                'codeValue' => '#x' . ($fontAwesomeUnicodeResult[$index])
                 ];
                 $fontAwesomePhp .= "       '$name' => [ 
-                    'id' => '$value',
+                    'id' => '$iconName',
                     'prepend' => 'fas fa-'
                 ],\n";
-                $line++;
             }
             $fontAwesomePhp .= "];";
         }
-
         return [json_encode($bootstrapFinal), $bootstrapPhp, json_encode($fontAwesomeFinal), $fontAwesomePhp];
     }
 



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/6457e4d473b6313d757e785f5cc4263d278adc84

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/6457e4d473b6313d757e785f5cc4263d278adc84
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.