Allow additional attributes for included scripts

This commit is contained in:
Pablo Zmdl
2025-09-02 18:54:45 +02:00
committed by Pablo Zmdl
parent ea050faaad
commit fd8ac88643
3 changed files with 21 additions and 15 deletions

View File

@@ -568,10 +568,10 @@ class rcmail_output_html extends rcmail_output
}
}
foreach ($this->script_files as $position => $files) {
foreach ($files as $file) {
if (str_starts_with($file, 'plugins/jqueryui')) {
$script_files[$position][] = $file;
foreach ($this->script_files as $position => $scripts_list) {
foreach ($scripts_list as $script_attribs) {
if (str_starts_with($script_attribs['src'], 'plugins/jqueryui')) {
$script_files[$position][] = $script_attribs;
}
}
}
@@ -1924,10 +1924,12 @@ class rcmail_output_html extends rcmail_output
/**
* Link an external script file
*
* @param string $file File URL
* @param string $position Target position [head|head_bottom|foot]
* @param string $file File URL
* @param string $position Target position [head|head_bottom|foot]
* @param bool $add_path Whether to prepend `scripts_path` to the file path, and to append an mtime-based parameter to it, too
* @param array $tag_attributes Additional attributes for the script tag
*/
public function include_script($file, $position = 'head', $add_path = true)
public function include_script($file, $position = 'head', $add_path = true, $tag_attributes = [])
{
if ($add_path && !preg_match('|^https?://|i', $file) && $file[0] != '/') {
$file = $this->file_mod($this->scripts_path . $file);
@@ -1937,8 +1939,12 @@ class rcmail_output_html extends rcmail_output
$this->script_files[$position] = [];
}
if (!in_array($file, $this->script_files[$position])) {
$this->script_files[$position][] = $file;
$tag_attributes = ['src' => $file] + $tag_attributes;
// Sort the array so differently ordered keys don't cause a duplicatedly loaded script.
ksort($tag_attributes);
if (!in_array($tag_attributes, $this->script_files[$position])) {
$this->script_files[$position][] = $tag_attributes;
}
}
@@ -2002,8 +2008,8 @@ class rcmail_output_html extends rcmail_output
$is_empty = true;
}
$merge_script_files = static function ($output, $script) {
return $output . html::script($script);
$merge_script_files = static function ($output, $attributes) {
return $output . html::script($attributes);
};
$merge_scripts = static function ($output, $script) {

View File

@@ -308,9 +308,9 @@ abstract class rcube_plugin
*
* @param string $fn File path; absolute or relative to the plugin directory
*/
public function include_script($fn)
public function include_script($fn, $tag_attributes = [])
{
$this->api->include_script($this->resource_url($fn));
$this->api->include_script($this->resource_url($fn), $tag_attributes);
}
/**

View File

@@ -652,11 +652,11 @@ class rcube_plugin_api
*
* @param string $fn Path to script
*/
public function include_script($fn)
public function include_script($fn, $tag_attributes = [])
{
if (is_object($this->output) && $this->output->type == 'html') {
$src = $this->resource_url($fn);
$this->output->include_script($src, 'head_bottom', false);
$this->output->include_script($src, 'head_bottom', false, $tag_attributes);
}
}