From 16be1a94fe7f332223432304b501765811c66afa Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 17 Mar 2021 14:36:20 +0100 Subject: [PATCH] MOBILE-3645 siteplugins: CSS should be included in reliable order --- .../siteplugins/services/siteplugins-helper.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/core/features/siteplugins/services/siteplugins-helper.ts b/src/core/features/siteplugins/services/siteplugins-helper.ts index 7634174d6..fd637602c 100644 --- a/src/core/features/siteplugins/services/siteplugins-helper.ts +++ b/src/core/features/siteplugins/services/siteplugins-helper.ts @@ -431,7 +431,21 @@ export class CoreSitePluginsHelperProvider { styleEl.setAttribute('id', 'siteplugin-' + uniqueName); styleEl.innerHTML = cssCode; - document.head.appendChild(styleEl); + // To ensure consistency, insert in alphabetical order among other site plugin styles. + let lowestGreater: HTMLStyleElement | null = null; + Array.from(document.head.querySelectorAll('style')).forEach((other) => { + if (/^siteplugin-/.test(other.id) && other.id > styleEl.id) { + if (lowestGreater === null || other.id < lowestGreater.id) { + lowestGreater = other; + } + } + }); + + if (lowestGreater) { + document.head.insertBefore(styleEl, lowestGreater); + } else { + document.head.appendChild(styleEl); + } // Styles have been loaded, now treat the CSS. CoreUtils.ignoreErrors(