From 155341c2bb6f115478e533d543127478d8c11d18 Mon Sep 17 00:00:00 2001 From: Micah Date: Tue, 31 Mar 2026 14:23:38 -0500 Subject: [PATCH 1/5] Wait for stylesheet load before adopting into shadow DOM --- crates/fe-web/assets/fe-code-block.js | 40 ++++++++++++++++++++------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/fe-web/assets/fe-code-block.js b/crates/fe-web/assets/fe-code-block.js index 50c9cf7ff8..cff4c51cf8 100644 --- a/crates/fe-web/assets/fe-code-block.js +++ b/crates/fe-web/assets/fe-code-block.js @@ -161,16 +161,7 @@ class FeCodeBlock extends HTMLElement { // Create shadow root once if (!this.shadowRoot) { this.attachShadow({ mode: "open" }); - var sheet = _getCodeBlockSheet(); - if (sheet) { - this.shadowRoot.adoptedStyleSheets = [sheet]; - } else { - // Fallback: clone page styles into shadow root - var pageStyles = document.querySelectorAll("style"); - for (var i = 0; i < pageStyles.length; i++) { - this.shadowRoot.appendChild(pageStyles[i].cloneNode(true)); - } - } + this._adoptStyles(); } this._render(); @@ -201,6 +192,35 @@ class FeCodeBlock extends HTMLElement { }); } + /** Adopt highlight styles into shadow root, waiting for stylesheet if needed. */ + _adoptStyles() { + var sheet = _getCodeBlockSheet(); + if (sheet) { + this.shadowRoot.adoptedStyleSheets = [sheet]; + return; + } + // Stylesheet not ready — find the that loads it and wait for load + var self = this; + var links = document.querySelectorAll('link[rel="stylesheet"]'); + for (var i = 0; i < links.length; i++) { + var href = links[i].getAttribute("href") || ""; + if (href.indexOf("highlight") !== -1) { + links[i].addEventListener("load", function onLoad() { + this.removeEventListener("load", onLoad); + _invalidateCodeBlockSheet(); + self._adoptStyles(); + self._render(); + }); + return; + } + } + // No highlight found — clone page