luci-base: amend path() to handle arrays better
authorPaul Donald <newtwen+github@gmail.com>
Mon, 23 Dec 2024 17:14:40 +0000 (17:14 +0000)
committerPaul Donald <newtwen+github@gmail.com>
Mon, 23 Dec 2024 17:48:43 +0000 (17:48 +0000)
While the jsdoc for this function describes a string[] parameter, this
seemingly was not handled; consistent usage of various
L.(url|resource|etc) is multiple strings that become object properties.
E.g.:

 L.resource('lldpd/lldpd.css');
 L.url("admin", "system", "leds");

which become e.g.:

 Arguments { 0: "admin/network", 1: "routes", … }

Handle a string[] parameter, by re-entering the function with any
Array[] parameter.

follow-up fix to 7c2cde52dbb14d5e878ca775946db9b3f13dcf1d

This prevents:

TypeError
parts[i].startsWith is not a function

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
modules/luci-base/htdocs/luci-static/resources/luci.js

index 4107a9019b97bfe0b47a2a134be7d3d6b85516bc..1a5fecc8b370aa24fa83e143469a75b815377a81 100644 (file)
                path(prefix = '', parts) {
                        const url = [ prefix ];
 
-                       for (let i = 0; i < parts.length; i++)
-                               if (/^(?:[a-zA-Z0-9_.%,;-]+\/)*[a-zA-Z0-9_.%,;-]+$/.test(parts[i]) || /^\?[a-zA-Z0-9_.%=&;-]+$/.test(parts[i]))
-                                       url.push(parts[i].startsWith('?') ? parts[i] : '/' + parts[i]);
+                       for (let i = 0; i < parts.length; i++){                         
+                               const part = parts[i];
+                               if (Array.isArray(part))
+                                       url.push(this.path('', part));
+                               else
+                                       if (/^(?:[a-zA-Z0-9_.%,;-]+\/)*[a-zA-Z0-9_.%,;-]+$/.test(part) || /^\?[a-zA-Z0-9_.%=&;-]+$/.test(part))
+                                               url.push(part.startsWith('?') ? part : '/' + part);
+                       }
 
                        if (url.length === 1)
                                url.push('/');