From 033549c4652ef5edf27f727d4e8d26bab69ba257 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Mon, 18 Aug 2025 14:42:57 +0200 Subject: [PATCH 1/3] Set up immediately if document is already loaded --- code-input.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/code-input.js b/code-input.js index 42b5ae2..b3ea7a7 100644 --- a/code-input.js +++ b/code-input.js @@ -831,8 +831,12 @@ var codeInput = { if (this.templateObject != undefined) { // Template registered before loading this.classList.add("code-input_registered"); - // Children not yet present - wait until they are - window.addEventListener("DOMContentLoaded", this.setup.bind(this)) + if (document.readyState === 'loading') { + // Children not yet present - wait until they are + window.addEventListener("DOMContentLoaded", this.setup.bind(this)) + } else { + this.setup(); + } } } From 4cadc45d234c7fc79b155e1c8d7a10a12fb6e085 Mon Sep 17 00:00:00 2001 From: Mattias Buelens Date: Mon, 18 Aug 2025 14:44:23 +0200 Subject: [PATCH 2/3] Fix disconnectedCallback when called before setup --- code-input.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/code-input.js b/code-input.js index b3ea7a7..0d2081f 100644 --- a/code-input.js +++ b/code-input.js @@ -863,7 +863,9 @@ var codeInput = { } disconnectedCallback() { - this.mutationObserver.disconnect(); + if (this.mutationObserver) { + this.mutationObserver.disconnect(); + } } /** From 960fc70a5093a738eb275cdcb6c76e57bd40b466 Mon Sep 17 00:00:00 2001 From: Oliver Geer Date: Tue, 19 Aug 2025 12:03:14 +0100 Subject: [PATCH 3/3] Add couple of tests for code-input after load --- tests/tester.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/tester.js b/tests/tester.js index 80b54b3..7430990 100644 --- a/tests/tester.js +++ b/tests/tester.js @@ -217,6 +217,14 @@ console.log("I've got another line!", 2 < 3, "should be true.");`); console.log("I've got another line!", 2 < 3, "should be true."); `); // Extra newline so line numbers visible if enabled + const programmaticCodeInput = document.createElement("code-input"); + document.body.append(programmaticCodeInput); + programmaticCodeInput.focus(); + document.execCommand("insertText", false, "Hello, World!"); + assertEqual("Core", "Programmatically-created element JS-accessible value", programmaticCodeInput.value, "Hello, World!"); + await waitAsync(50); + assertEqual("Core", "Programmatically-created element rendered value", programmaticCodeInput.preElement.textContent, "Hello, World!\n"); + // Event Listener Tests // Function type listeners let numTimesInputCalled = 0;