From 62498926d34fbfc604361000b0c710c28d701f8a Mon Sep 17 00:00:00 2001
From: nod_ <nod_@598310.no-reply.drupal.org>
Date: Fri, 12 Jan 2024 22:25:12 +0100
Subject: [PATCH] Issue #3400359 by el7cosmos, dinazaur, alexpott, ReINFaTe:
 External fonts cannot be loaded via add_css ajax command

---
 core/misc/ajax.js                                             | 3 ++-
 .../tests/modules/ajax_forms_test/ajax_forms_test.module      | 4 ++++
 .../Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php    | 2 ++
 3 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index bb8b46576b2c..4307d92e1cfc 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -1716,7 +1716,8 @@
 
       const allUniqueBundleIds = response.data.map(function (style) {
         const uniqueBundleId = style.href + ajax.instanceIndex;
-        loadjs(style.href, uniqueBundleId, {
+        // Force file to load as a CSS stylesheet using 'css!' flag.
+        loadjs(`css!${style.href}`, uniqueBundleId, {
           before(path, styleEl) {
             // This allows all attributes to be added, like media.
             Object.keys(style).forEach((attributeKey) => {
diff --git a/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module b/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
index 8313a9bb593d..96e0a3326607 100644
--- a/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
+++ b/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
@@ -207,6 +207,10 @@ function ajax_forms_test_advanced_commands_add_css_callback($form, FormStateInte
       'href' => 'my/file.css',
       'media' => 'all',
     ],
+    [
+      'href' => 'https://example.com/css?family=Open+Sans',
+      'media' => 'all',
+    ],
   ]));
   return $response;
 }
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php
index 969e7cc8671e..6010186c0b7d 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/CommandsTest.php
@@ -38,6 +38,8 @@ public function testAjaxCommands() {
     // Tests the 'add_css' command.
     $page->pressButton("AJAX 'add_css' command");
     $this->assertWaitPageContains('my/file.css');
+    $this->assertSession()->elementExists('css', 'link[href="my/file.css"]');
+    $this->assertSession()->elementExists('css', 'link[href="https://example.com/css?family=Open+Sans"]');
 
     // Tests the 'after' command.
     $page->pressButton("AJAX 'After': Click to put something after the div");
-- 
GitLab